Skip to content

Commit 77e7ae0

Browse files
committed
C front-end: reject code containing duplicate labels
We previously silently accepted such code, maintaining the labels and attaching them to multiple instructions. `goto` resolved to the first such occurrence of the label. GCC firmly rejects such code.
1 parent d42839d commit 77e7ae0

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
int main()
2+
{
3+
int x;
4+
label:
5+
x = 1;
6+
goto label;
7+
label:
8+
x = 2;
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CORE
2+
main.c
3+
4+
^EXIT=(1|64)$
5+
^SIGNAL=0$
6+
error: duplicate label 'label'
7+
^CONVERSION ERROR$
8+
--
9+
^warning: ignoring

src/ansi-c/c_typecheck_code.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,12 @@ void c_typecheck_baset::typecheck_for(codet &code)
505505
void c_typecheck_baset::typecheck_label(code_labelt &code)
506506
{
507507
// record the label
508-
labels_defined[code.get_label()]=code.source_location();
508+
if(!labels_defined.emplace(code.get_label(), code.source_location()).second)
509+
{
510+
error().source_location = code.source_location();
511+
error() << "duplicate label '" << code.get_label() << "'" << eom;
512+
throw 0;
513+
}
509514

510515
typecheck_code(code.code());
511516
}

0 commit comments

Comments
 (0)