[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

integer overflow in muli instruction

From: Bruno Haible
Subject: integer overflow in muli instruction
Date: Fri, 19 Feb 2021 18:22:58 +0100
User-agent: KMail/5.1.3 (Linux/4.4.0-201-generic; KDE/5.18.0; x86_64; ; )

On Ubuntu 16.04 (x86_64, with CC="gcc -ftrapv") I get 4 test suite failures.
One of these is

FAIL: /media/develdata/devel/build/poke-0.90/build-64/testsuite/../poke/poke 
killed: 11472 exp34 0 0 CHILDKILLED SIGABRT SIGABRT

To reproduce it:

(poke) atoi ("deadbeef", 16)

In the debugger:

(poke) atoi ("deadbeef", 16)

Thread 1 "poke" received signal SIGABRT, Aborted.
0x00007ffff6e2c438 in __GI_raise (sig=sig@entry=6) at 
54      ../sysdeps/unix/sysv/linux/raise.c: Datei oder Verzeichnis nicht 
(gdb) up
#1  0x00007ffff6e2e03a in __GI_abort () at abort.c:89
89      abort.c: Datei oder Verzeichnis nicht gefunden.
(gdb) up
#2  0x00007ffff7ba43f2 in __mulvsi3 () from 
(gdb) up
#3  0x00007ffff7b8ab28 in pvm_execute_or_initialize (jitter_initialize=56, 
    jitter_original_state=0x632310) at ../../libpoke/pvm.jitter:2242
2242        PVM_BINOP (INT, INT, INT, *);
(gdb) list
2237    #
2238    # Stack: ( INT INT -- INT INT INT )
2240    instruction muli ()
2241      code
2242        PVM_BINOP (INT, INT, INT, *);
2243      end
2244    end
2246    # Instruction: muliu

So, apparently the atoi invocation ends up doing a 'muli' VM instruction,
and this instruction does a signed integer multiplication that overflows.
Since signed integer overflow is undefined behaviour in ISO C, 'gcc -ftrapv'
is right in aborting here.

I would suggest to use checked integer multiplication, for example by using
the macro INT_MULTIPLY_OVERFLOW from intprops.h (Gnulib module 'intprops').


reply via email to

[Prev in Thread] Current Thread [Next in Thread]