Re: Clang UndefinedBehaviorSanitize (Postgres14) Detected undefined-behavior - Mailing list pgsql-hackers

From Ranier Vilela
Subject Re: Clang UndefinedBehaviorSanitize (Postgres14) Detected undefined-behavior
Date
Msg-id CAEudQArZaGP8Sr7EvTMLL47AQU8Ei0yZqX=zGLRg592LUuc92A@mail.gmail.com
Whole thread Raw
In response to Re: Clang UndefinedBehaviorSanitize (Postgres14) Detected undefined-behavior  (Alvaro Herrera <alvherre@2ndquadrant.com>)
Responses Re: Clang UndefinedBehaviorSanitize (Postgres14) Detected undefined-behavior  (Ranier Vilela <ranier.vf@gmail.com>)
List pgsql-hackers
Em seg., 31 de ago. de 2020 às 14:00, Alvaro Herrera <alvherre@2ndquadrant.com> escreveu:
On 2020-Aug-31, Ranier Vilela wrote:

> More troubles with undefined-behavior.
>
> This type of code can leaves overflow:
> var = (cast) (expression);
> diff = (int32) (id1 - id2);
>
> See:
>     diff64 =  ((long int) d1 - (long int) d2);
>     diff64=-4294901760

Did you compile this with gcc -fwrapv?
gcc 10.2 -O2  -fwrapv
bool test1()
{
    unsigned int d1 = 3;
    unsigned int d2 = 4294901763;
    long int diff64 = 0;

    diff64 =  ((long int) d1 - (long int) d2);

    return (diff64 < 0);
}
 
output:
mov     eax1
        ret

What is a workaround for msvc 2019 (64 bits) and clang 64 bits (linux)?
transam.c:311:22: runtime error: unsigned integer overflow: 3 - 4294901763 cannot be represented in type 'unsigned int'

Ranier Vilela

pgsql-hackers by date:

Previous
From: Tom Lane
Date:
Subject: Re: Get rid of runtime handling of AlternativeSubPlan?
Next
From: Tom Lane
Date:
Subject: Re: Ideas about a better API for postgres_fdw remote estimates