Skip to content

Commit 980cb0c

Browse files
committed
cmd/asm: fix crash triggered by nested #define
A panic was in place for an impossible condition that turned out to be possible if one used a macro to define a macro. Another go-fuzz "win". Fixes #12654. Change-Id: I0a7bb0f0eabb260c986bf7a2288860c78d8db1af Reviewed-on: https://go-review.googlesource.com/14777 Reviewed-by: Andrew Gerrand <[email protected]>
1 parent bca70a6 commit 980cb0c

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

src/cmd/asm/internal/lex/lex_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,35 @@ var lexTests = []lexTest{
226226
),
227227
"C.\n",
228228
},
229+
{
230+
"nested #define",
231+
lines(
232+
"#define A #define B THIS",
233+
"A",
234+
"B",
235+
),
236+
"THIS.\n",
237+
},
238+
{
239+
"nested #define with args",
240+
lines(
241+
"#define A #define B(x) x",
242+
"A",
243+
"B(THIS)",
244+
),
245+
"THIS.\n",
246+
},
247+
/* This one fails. See comment in Slice.Col.
248+
{
249+
"nested #define with args",
250+
lines(
251+
"#define A #define B (x) x",
252+
"A",
253+
"B(THIS)",
254+
),
255+
"x.\n",
256+
},
257+
*/
229258
}
230259

231260
func TestLex(t *testing.T) {

src/cmd/asm/internal/lex/slice.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,16 @@ func (s *Slice) Line() int {
4444
}
4545

4646
func (s *Slice) Col() int {
47-
// Col is only called when defining a macro, which can't reach here.
48-
panic("cannot happen: slice col")
47+
// TODO: Col is only called when defining a macro and all it cares about is increasing
48+
// position to discover whether there is a blank before the parenthesis.
49+
// We only get here if defining a macro inside a macro.
50+
// This imperfect implementation means we cannot tell the difference between
51+
// #define A #define B(x) x
52+
// and
53+
// #define A #define B (x) x
54+
// The first has definition of B has an argument, the second doesn't. Because we let
55+
// text/scanner strip the blanks for us, this is extremely rare, hard to fix, and not worth it.
56+
return s.pos
4957
}
5058

5159
func (s *Slice) SetPos(line int, file string) {

0 commit comments

Comments
 (0)