From hp@ge@@on@g|thub @end|ng |rom gm@||@com  Fri Sep 10 23:14:39 2021
From: hp@ge@@on@g|thub @end|ng |rom gm@||@com (=?UTF-8?B?SGVydsOpIFBhZ8Oocw==?=)
Date: Fri, 10 Sep 2021 14:14:39 -0700
Subject: [Rd] 
 Spurious warnings in coercion from double/complex/character to raw
In-Reply-To: <1604597188.364742.1631303584179@mail.yahoo.com>
References: <8b1874d6-b41b-8909-7ddb-2da0459618e2@gmail.com>
 <2b1460bb-a8e8-42ee-6623-b07a5858b332@gmail.com>
 <fe49f407-ab9a-2cac-7fed-15cc4a6e18b3@gmail.com>
 <1604597188.364742.1631303584179@mail.yahoo.com>
Message-ID: <ad5994f2-1e6b-6059-52aa-6c0f4c0d04b1@gmail.com>



On 10/09/2021 12:53, brodie gaslam wrote:
> 
>> On Friday, September 10, 2021, 03:13:54 PM EDT, Herv? Pag?s <hpages.on.github at gmail.com> wrote:
>>
>> Good catch, thanks!
>>
>> Replacing
>>
>>  ???? if(ISNAN(vi) || (tmp = (int) vi) < 0 || tmp > 255) {
>>  ???????? tmp = 0;
>>
>>  ???????? warn |= WARN_RAW;
>>
>>  ???? }
>>  ???? pa[i] = (Rbyte) tmp;
>>
>> with
>>
>>  ???? if(ISNAN(vi) || vi <= -1.0 || vi >= 256.0)
>>  ?? {
>>  ???????? tmp = 0;
>>
>>  ???????? warn |= WARN_RAW;
>>
>>  ???? } else {
>>  ???????? tmp = (int) vi;
>>  ???? }
>>  ???? pa[i] = (Rbyte) tmp;
>>
>> should address that.
>>
>> FWIW IntegerFromReal() has a similar risk of int overflow
>> (src/main/coerce.c, lines 128-138):
>>
>>
>>  ?? int attribute_hidden
>>
>>  ?? IntegerFromReal(double x, int *warn)
>>
>>  ?? {
>>
>>  ?????? if (ISNAN(x))
>>
>>  ?????????? return NA_INTEGER;
>>
>>  ?????? else if (x >= INT_MAX+1. || x <= INT_MIN ) {
>>
>>  ?????????? *warn |= WARN_INT_NA;
>>
>>  ?????????? return NA_INTEGER;
>>
>>  ?????? }
>>
>>  ?????? return (int) x;
>>
>>  ?? }
>>
>>
>>
>> The cast to int will also be an int overflow situation if x is > INT_MAX
>> and < INT_MAX+1 so the risk is small!
> 
> I might be being dense, but it feels this isn't a problem?? Quoting C99
> 6.3.1.4 again (emph added):
> 
>> 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.50)
> 
> Does the "fractional part is discarded" not save us here?

I think it does. Thanks for clarifying and sorry for the false positive!

H.

> 
> Best,
> 
> B.
> 
> 

-- 
Herv? Pag?s

Bioconductor Core Team
hpages.on.github at gmail.com



[Rd] Spurious warnings in coercion from double/complex/character to raw
=?UTF-8?B?SGVydsOpIFBhZ8Oocw==?=