aboutsummaryrefslogtreecommitdiffhomepage
path: root/helper/args.go
blob: f85d57ed649d15f417af698470799b545b5754f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package helper

import (
	"errors"
	"io"
	"strings"
)

var (
	ErrContainsNull = errors.New("argument contains null character")
)

type argsWt []string

// checks whether any element contains the null character
// must be called before args use and args must not be modified after call
func (a argsWt) check() error {
	for _, arg := range a {
		for _, b := range arg {
			if b == '\x00' {
				return ErrContainsNull
			}
		}
	}

	return nil
}

func (a argsWt) WriteTo(w io.Writer) (int64, error) {
	// assuming already checked

	nt := 0
	// write null terminated arguments
	for _, arg := range a {
		n, err := w.Write([]byte(arg + "\x00"))
		nt += n

		if err != nil {
			return int64(nt), err
		}
	}

	return int64(nt), nil
}

func (a argsWt) String() string {
	return strings.Join(a, " ")
}

// NewCheckedArgs returns a checked argument writer for args.
// Callers must not retain any references to args.
func NewCheckedArgs(args []string) (io.WriterTo, error) {
	a := argsWt(args)
	return a, a.check()
}

// MustNewCheckedArgs returns a checked argument writer for args and panics if check fails.
// Callers must not retain any references to args.
func MustNewCheckedArgs(args []string) io.WriterTo {
	a, err := NewCheckedArgs(args)
	if err != nil {
		panic(err.Error())
	}

	return a
}