bug-gsl
[Top][All Lists]
Advanced

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

Re: [Bug-gsl] Undefined behavior in vector/test_source.c


From: Patrick Alken
Subject: Re: [Bug-gsl] Undefined behavior in vector/test_source.c
Date: Thu, 26 Mar 2015 15:10:38 -0600
User-agent: Mozilla/5.0 (X11; Linux i686 on x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0

Thanks for your report. The patch looks ok to me, I've added it to the git repository

On 03/26/2015 01:03 PM, Hal Finkel wrote:
Hello,

Currently, several of the tests in vector/test_source.c don't pass when 
compiled with Clang/LLVM on some targets because vector/test_source.c contains 
undefined behavior. I've attached a patch which corrects the problem, which is 
essentially this:

   if (FUNCTION (gsl_vector, get) (v, i) != (ATOMIC) (i*2.0))

The current code assumes that, when ATOMIC is unsigned char, for example, 
(unsigned char) (i*2.0) will have the same behavior as (unsigned char) (i*2), 
but it does not (the value is not guaranteed to wrap in the same way). The C 
standard says (6.3.1.4p1):

When a finite value of real floating type is converted to an integer type other 
than _Bool,

the fractional part is discarded (i.e., the value is truncated toward zero). If 
the value of

the integral part cannot be represented by the integer type, the behavior is 
undefined.

There are two problematic statements in this regard, and in the patch I've added 
some extra casts so that we're not doing an fp -> int conversion with an 
out-of-range integer.

Thanks in advance,
Hal





reply via email to

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