-
Notifications
You must be signed in to change notification settings - Fork 17.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/asm: fix several panics with erroneous input
The parser tries to read as much information as possible, issuing some errors when needed. Errors generally do not stop the parsing. With some pathological input, it may result in various panics when the error message itself is built, or when the next operand is parsed. It happens while parsing pseudo-instructions. For instance, the following lines all generate a panic: TEXT TEXT% TEXT 1,1 TEXT $"toto", 0, $1 FUNCDATA DATA 0 DATA(0),1 FUNCDATA(SB GLOBL 0, 1 PCDATA 1 Added corresponding tests. Introduced a writer in the parser to capture error messages for testing purpose. It defaults to os.Stderr. Added an explicit check when symbol names cannot be displayed. Interrupted parsing early when the number of operands is wrong for pseudo-instructions. Note that the last point is a change of behavior, because some operands will not get parsed anymore in case of early error. IMO, it is acceptable, because only the first error of the line is considered anyway. If it is not acceptable, it can probably be improved at the price of a more verbose CL. Fixes #11765 Fixes #11760 Fixes #11759 Change-Id: I9602a848132e358a1bccad794d7555e0823970dd Reviewed-on: https://go-review.googlesource.com/13925 Reviewed-by: Rob Pike <[email protected]>
- Loading branch information
Showing
3 changed files
with
103 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright 2015 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package asm | ||
|
||
import ( | ||
"bytes" | ||
"strings" | ||
"testing" | ||
|
||
"cmd/asm/internal/arch" | ||
"cmd/asm/internal/lex" | ||
) | ||
|
||
func tokenize(s string) [][]lex.Token { | ||
res := [][]lex.Token{} | ||
if len(s) == 0 { | ||
return res | ||
} | ||
for _, o := range strings.Split(s, ",") { | ||
res = append(res, lex.Tokenize(o)) | ||
} | ||
return res | ||
} | ||
|
||
func TestErroneous(t *testing.T) { | ||
|
||
tests := []struct { | ||
pseudo string | ||
operands string | ||
expected string | ||
}{ | ||
{"TEXT", "", "expect two or three operands for TEXT"}, | ||
{"TEXT", "%", "expect two or three operands for TEXT"}, | ||
{"TEXT", "1, 1", "TEXT symbol \"<erroneous symbol>\" must be a symbol(SB)"}, | ||
{"TEXT", "$\"foo\", 0, $1", "TEXT symbol \"<erroneous symbol>\" must be a symbol(SB)"}, | ||
{"FUNCDATA", "", "expect two operands for FUNCDATA"}, | ||
{"FUNCDATA", "(SB ", "expect two operands for FUNCDATA"}, | ||
{"DATA", "", "expect two operands for DATA"}, | ||
{"DATA", "0", "expect two operands for DATA"}, | ||
{"DATA", "(0), 1", "expect /size for DATA argument"}, | ||
{"GLOBL", "", "expect two or three operands for GLOBL"}, | ||
{"GLOBL", "0,1", "GLOBL symbol \"<erroneous symbol>\" must be a symbol(SB)"}, | ||
{"PCDATA", "", "expect two operands for PCDATA"}, | ||
{"PCDATA", "1", "expect two operands for PCDATA"}, | ||
} | ||
|
||
// Note these errors should be independent of the architecture. | ||
// Just run the test with amd64. | ||
parser := newParser("amd64") | ||
var buf bytes.Buffer | ||
parser.errorWriter = &buf | ||
|
||
for _, test := range tests { | ||
parser.errorCount = 0 | ||
parser.lineNum++ | ||
parser.histLineNum++ | ||
op, ok := arch.Pseudos[test.pseudo] | ||
if !ok { | ||
t.Fatalf("Wrong pseudo-instruction: %s", test.pseudo) | ||
} | ||
parser.pseudo(op, test.pseudo, tokenize(test.operands)) | ||
errorLine := buf.String() | ||
if test.expected != errorLine { | ||
t.Errorf("Unexpected error %q; expected %q", errorLine, test.expected) | ||
} | ||
buf.Reset() | ||
} | ||
|
||
} |