History

Fix crash on invalid code

like on 'enum myenum { L = -1 } L;'. It's a bit tedious as there are two paths (for global vs local symbols), and because the scope and enum_val share same storage.

Michael Matz committed 7 years ago

Commit ef0397c1 parent b0826593 files · +22 −2Browse files

Unified · Split

3 files
  1. tccgen.c +10 −2
  2. tests/tests2/60_errors_and_warnings.c +6 −0
  3. tests/tests2/60_errors_and_warnings.expect +6 −0
tccgen.c+10−2
@@ -571,6 +571,14 @@ ST_INLN Sym *sym_find(int v) · 570 unmodified lines
571571 return table_ident[v]->sym_identifier;
572572 }
573573
574+static int sym_scope(Sym *s)
575+{
576+ if (IS_ENUM_VAL (s->type.t))
577+ return s->type.ref->sym_scope;
578+ else
579+ return s->sym_scope;
580+}
581+
574582 /* push a given symbol on the symbol stack */
575583 ST_FUNC Sym *sym_push(int v, CType *type, int r, int c)
576584 {
@@ -596,7 +604,7 @@ ST_FUNC Sym *sym_push(int v, CType *type, int r, int c) · 19 unmodified lines
596604 s->prev_tok = *ps;
597605 *ps = s;
598606 s->sym_scope = local_scope;
599− if (s->prev_tok && s->prev_tok->sym_scope == s->sym_scope)
607+ if (s->prev_tok && sym_scope(s->prev_tok) == s->sym_scope)
600608 tcc_error("redeclaration of '%s'",
601609 get_tok_str(v & ~SYM_STRUCT, NULL));
602610 }
@@ -919,7 +927,7 @@ static void merge_attr(AttributeDef *ad, AttributeDef *ad1) · 316 unmodified lines
919927 /* Merge some type attributes. */
920928 static void patch_type(Sym *sym, CType *type)
921929 {
922− if (!(type->t & VT_EXTERN)) {
930+ if (!(type->t & VT_EXTERN) || IS_ENUM_VAL(sym->type.t)) {
923931 if (!(sym->type.t & VT_EXTERN))
924932 tcc_error("redefinition of '%s'", get_tok_str(sym->v, NULL));
925933 sym->type.t &= ~VT_EXTERN;
tests/tests2/60_errors_and_warnings.c+6−0
@@ -124,4 +124,10 @@ void f() { _Generic((int const *[]){0}, int:0); } · 123 unmodified lines
124124 void f() { _Generic((int (*(*)(float,char))(double,int)){0}, int:0); }
125125 #elif defined test_array_to_str
126126 void f() { _Generic((int(*)[3]){0}, int:0); }
127+#elif defined test_duplicate_def_1
128+static enum myenum { L = -1 } L;
129+#elif defined test_duplicate_def_2
130+void foo(void) {
131+static enum myenum { L = -1 } L;
132+}
127133 #endif
tests/tests2/60_errors_and_warnings.expect+6−0
@@ -53,3 +53,9 @@ · 52 unmodified lines
5353
5454 [test_array_to_str]
5555 60_errors_and_warnings.c:126: error: type 'int (*)[3]' does not match any association
56+
57+[test_duplicate_def_1]
58+60_errors_and_warnings.c:128: error: redefinition of 'L'
59+
60+[test_duplicate_def_2]
61+60_errors_and_warnings.c:131: error: redeclaration of 'L'

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t