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
571 return table_ident[v]->sym_identifier;571 return table_ident[v]->sym_identifier;
572}572}
573 573
574static 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
574/* push a given symbol on the symbol stack */582/* push a given symbol on the symbol stack */
575ST_FUNC Sym *sym_push(int v, CType *type, int r, int c)583ST_FUNC Sym *sym_push(int v, CType *type, int r, int c)
576{584{
@@ -596,7 +604,7 @@ ST_FUNC Sym *sym_push(int v, CType *type, int r, int c) · 19 unmodified lines
596 s->prev_tok = *ps;604 s->prev_tok = *ps;
597 *ps = s;605 *ps = s;
598 s->sym_scope = local_scope;606 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)
600 tcc_error("redeclaration of '%s'",608 tcc_error("redeclaration of '%s'",
601 get_tok_str(v & ~SYM_STRUCT, NULL));609 get_tok_str(v & ~SYM_STRUCT, NULL));
602 }610 }
@@ -919,7 +927,7 @@ static void merge_attr(AttributeDef *ad, AttributeDef *ad1) · 316 unmodified lines
919/* Merge some type attributes. */927/* Merge some type attributes. */
920static void patch_type(Sym *sym, CType *type)928static void patch_type(Sym *sym, CType *type)
921{929{
922 if (!(type->t & VT_EXTERN)) {930 if (!(type->t & VT_EXTERN) || IS_ENUM_VAL(sym->type.t)) {
923 if (!(sym->type.t & VT_EXTERN))931 if (!(sym->type.t & VT_EXTERN))
924 tcc_error("redefinition of '%s'", get_tok_str(sym->v, NULL));932 tcc_error("redefinition of '%s'", get_tok_str(sym->v, NULL));
925 sym->type.t &= ~VT_EXTERN;933 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
124void f() { _Generic((int (*(*)(float,char))(double,int)){0}, int:0); }124void f() { _Generic((int (*(*)(float,char))(double,int)){0}, int:0); }
125#elif defined test_array_to_str125#elif defined test_array_to_str
126void f() { _Generic((int(*)[3]){0}, int:0); }126void f() { _Generic((int(*)[3]){0}, int:0); }
127#elif defined test_duplicate_def_1
128static enum myenum { L = -1 } L;
129#elif defined test_duplicate_def_2
130void foo(void) {
131static enum myenum { L = -1 } L;
132}
127#endif133#endif
tests/tests2/60_errors_and_warnings.expect+6−0
@@ -53,3 +53,9 @@ · 52 unmodified lines
53 53
54[test_array_to_str]54[test_array_to_str]
5560_errors_and_warnings.c:126: error: type 'int (*)[3]' does not match any association5560_errors_and_warnings.c:126: error: type 'int (*)[3]' does not match any association
56
57[test_duplicate_def_1]
5860_errors_and_warnings.c:128: error: redefinition of 'L'
59
60[test_duplicate_def_2]
6160_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