From d042e71e9f96cd2635e9d18622c4a2aa007821a4 Mon Sep 17 00:00:00 2001 From: Michael Matz Date: Tue, 18 Oct 2016 03:31:14 +0200 Subject: Fix miscompile with dead switches In certain very specific situations (involving switches with asms inside dead statement expressions) we could generate invalid code (clobbering the buffer so much that we generated invalid instructions). Don't emit the decision table if the switch itself is dead. --- tccgen.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'tccgen.c') diff --git a/tccgen.c b/tccgen.c index b490c04..2cc57bd 100644 --- a/tccgen.c +++ b/tccgen.c @@ -5692,13 +5692,15 @@ static void block(int *bsym, int *csym, int is_expr) a = gjmp(a); /* add implicit break */ /* case lookup */ gsym(b); - qsort(sw.p, sw.n, sizeof(void*), case_cmp); - for (b = 1; b < sw.n; b++) - if (sw.p[b - 1]->v2 >= sw.p[b]->v1) - tcc_error("duplicate case value"); - gcase(sw.p, sw.n, c, &a); - if (sw.def_sym) - gjmp_addr(sw.def_sym); + if (!nocode_wanted) { + qsort(sw.p, sw.n, sizeof(void*), case_cmp); + for (b = 1; b < sw.n; b++) + if (sw.p[b - 1]->v2 >= sw.p[b]->v1) + tcc_error("duplicate case value"); + gcase(sw.p, sw.n, c, &a); + if (sw.def_sym) + gjmp_addr(sw.def_sym); + } dynarray_reset(&sw.p, &sw.n); cur_switch = saved; /* break label */ -- cgit v1.3.1