Skip to content

Commit cab7b52

Browse files
committed
C front-end: fix promotion order for types ranking lower than int
The ranks specified in the enum are only correct if sizeof(int) > sizeof(short) and sizeof(int) > sizeof(char). The code was supposed to fix this up, but none of the branches was feasible as max_type would always be at least INT. Fixes: diffblue#1748
1 parent c450328 commit cab7b52

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

regression/cbmc/Promotion4/main.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
int main(int argc, char *argv[])
2+
{
3+
__CPROVER_assert(sizeof(unsigned int) == 2, "[--16] size of int is 16 bit");
4+
__CPROVER_assert(
5+
sizeof(unsigned long) == 4, "[--LP32] size of long is 32 bit");
6+
7+
unsigned int k = non_det_unsigned();
8+
__CPROVER_assume(k < 1);
9+
__CPROVER_assert(k != 0xffff, "false counter example 16Bit?");
10+
11+
return 0;
12+
}

regression/cbmc/Promotion4/test.desc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.c
3+
--16
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring

src/ansi-c/c_typecast.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -418,16 +418,15 @@ c_typecastt::c_typet c_typecastt::minimum_promotion(
418418
c_typet max_type=std::max(c_type, INT); // minimum promotion
419419

420420
// The second case can arise if we promote any unsigned type
421-
// that is as large as unsigned int.
422-
423-
if(config.ansi_c.short_int_width==config.ansi_c.int_width &&
424-
max_type==USHORT)
421+
// that is as large as unsigned int. In this case the promotion configuration
422+
// via the enum is actually wrong, and we need to fix this up.
423+
if(
424+
config.ansi_c.short_int_width == config.ansi_c.int_width &&
425+
c_type == USHORT)
425426
max_type=UINT;
426-
else if(config.ansi_c.char_width==config.ansi_c.int_width &&
427-
max_type==UCHAR)
427+
else if(
428+
config.ansi_c.char_width == config.ansi_c.int_width && c_type == UCHAR)
428429
max_type=UINT;
429-
else
430-
max_type=std::max(max_type, INT);
431430

432431
if(max_type==UINT &&
433432
type.id()==ID_c_bit_field &&

0 commit comments

Comments
 (0)