lightsystemdarkgreen
@ 261118.2

261118.2.1 619d76958069dc7875966d58a14f05c3004b4f51 Keep assignment values whole and never treat split expansion fields as envs

ParseWithEnvs split the value of an assignment like FOO=$X (X="a BAR=bar") into FOO=a and BAR=bar, so BAR=bar became the command. A shell does not split assignment values, so keep the value whole while still in assignment position.

Also, a field split off an expansion such as ${X}FOO=foo (X=" ") was reset to plain text and taken as an assignment. Keep it marked so it stays an ordinary word.

reading declarations

shellwords.go

@@ -131,6 +131,9 @@ func (p *Parser) parse(line string, withQuotedAt bool) ([]string, []int, error) 			quotedAt = len(buf) 		} 	}+	// Whether every word so far was an assignment, so the pending token is+	// still in assignment position. Only tracked when withQuotedAt is true.+	assignPos := withQuotedAt 	// Whether the previous character was an unquoted, unescaped '$'. 	afterDollar := false 	// Byte offset in line up to which characters were already consumed.@@ -146,6 +149,9 @@ func (p *Parser) parse(line string, withQuotedAt bool) ([]string, []int, error) 			args = append(args, string(buf)) 			if withQuotedAt { 				quotedAts = append(quotedAts, quotedAt)+				if assignPos && !isAssignment(string(buf), quotedAt) {+					assignPos = false+				} 			} 		} 		buf = buf[:0]@@ -371,10 +377,13 @@ loop: 				} 				skip = idx + 1 + n 				value := getenv(name)+				// A shell does not split the value of an assignment, so+				// FOO=$X stays one word even if X contains spaces.+				inAssignment := assignPos && isAssignment(string(buf), quotedAt) 				// Even an empty expansion keeps the word from being an 				// assignment, as in a shell. 				markQuoted()-				if doubleQuoted {+				if doubleQuoted || inAssignment { 					buf = append(buf, value...) 				} else { 					// Split the value into fields, but never interpret its@@ -382,6 +391,10 @@ loop: 					for _, c := range value { 						if isSpace(c) && !p.isExcluded(c) { 							flush()+							// The rest of the token still comes from a word+							// with an expansion before it, so it can never+							// be an assignment: ${X}A=1 with X=" ".+							quotedAt = 0 							continue 						} 						markQuoted()@@ -432,9 +445,7 @@ func (p *Parser) ParseWithEnvs(line string) (envs []string, args []string, err e 	args = []string{} 	parsingEnv := true 	for i, arg := range _args {-		// Only a word whose name and '=' are plain text is an assignment;-		// "A=1", A\=1 or the result of an expansion is an ordinary word.-		if parsingEnv && isEnv(arg) && (quotedAts[i] < 0 || quotedAts[i] > strings.IndexByte(arg, '=')) {+		if parsingEnv && isAssignment(arg, quotedAts[i]) { 			envs = append(envs, arg) 		} else { 			if parsingEnv {@@ -446,6 +457,14 @@ func (p *Parser) ParseWithEnvs(line string) (envs []string, args []string, err e 	return envs, args, nil } +// isAssignment reports whether word, whose first quoted, escaped or expanded+// byte is at quotedAt (-1 if none), is a NAME=value assignment. Only a word+// whose name and '=' are plain text is one; "A=1", A\=1 or the result of an+// expansion is an ordinary word.+func isAssignment(word string, quotedAt int) bool {+	return isEnv(word) && (quotedAt < 0 || quotedAt > strings.IndexByte(word, '='))+}+ func isEnv(arg string) bool { 	i := strings.IndexByte(arg, '=') 	if i <= 0 {

shellwords_test.go

@@ -1148,7 +1148,7 @@ func TestParseWithEnvs(t *testing.T) { }  func TestParseWithEnvsExpansion(t *testing.T) {-	env := map[string]string{"ASSIGN": "FOO=foo", "TWO": "FOO=foo BAR=bar", "EMPTY": "", "ONE": "x", "VAL": "x y"}+	env := map[string]string{"ASSIGN": "FOO=foo", "TWO": "FOO=foo BAR=bar", "EMPTY": "", "ONE": "x", "VAL": "x y", "SP": " ", "SPLIT": "a BAR=bar"} 	tests := []struct { 		line               string 		wantEnvs, wantArgs []string@@ -1162,6 +1162,16 @@ func TestParseWithEnvsExpansion(t *testing.T) { 		// An expansion in the value is fine. 		{"FOO=$ONE BAR=a$EMPTY cmd", []string{"FOO=x", "BAR=a"}, []string{"cmd"}}, 		{`FOO="$VAL" cmd`, []string{"FOO=x y"}, []string{"cmd"}},+		// Fields split off an expansion are never assignments either.+		{"${SP}FOO=foo cmd", []string{}, []string{"FOO=foo", "cmd"}},+		{"${SP}FOO=foo BAR=bar cmd", []string{}, []string{"FOO=foo", "BAR=bar", "cmd"}},+		// The value of an assignment is not split, as in a shell.+		{"FOO=$SPLIT cmd", []string{"FOO=a BAR=bar"}, []string{"cmd"}},+		{"FOO=x$VAL BAR=$SPLIT cmd", []string{"FOO=xx y", "BAR=a BAR=bar"}, []string{"cmd"}},+		// ... but once the command starts, words are split again.+		{"cmd FOO=$SPLIT", []string{}, []string{"cmd", "FOO=a", "BAR=bar"}},+		{`"FOO=x" BAR=$SPLIT`, []string{}, []string{"FOO=x", "BAR=a", "BAR=bar"}},+		{"FOO\\=$SPLIT", []string{}, []string{"FOO=a", "BAR=bar"}}, 	} 	for _, tt := range tests { 		t.Run(tt.line, func(t *testing.T) {