Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #163929

Re: unsafe C

From Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups comp.lang.c
Subject Re: unsafe C
Date 2021-12-17 12:25 -0800
Organization None to speak of
Message-ID <87pmpv6kss.fsf@nosuchdomain.example.com> (permalink)
References <d8227134-6e0b-486e-af91-aeece50a466bn@googlegroups.com> <54ebbb14-cb1e-4dd5-a4ec-9dd086e0eaa3n@googlegroups.com>

Show all headers | View raw


Thiago Adams <thiago.adams@gmail.com> writes:
> On Friday, December 17, 2021 at 5:13:32 PM UTC-3, Thiago Adams wrote:
>> I realized strtoull is unsafe. 
>> 
>> #include <stdlib.h> 
>> #include <stdio.h> 
>> 
>> int main() { 
>> char* begin = "123456789011121314151617181920"; 
>> unsigned long long value = strtoull(begin, NULL, 10); 
>> printf("%s\n", begin); 
>> printf("%llu\n", value); 
>> } 
>> it prints 
>> 123456789011121314151617181920 
>> 18446744073709551615
>
> The gcc compiler uses (internally) a better function:

I don't know what gcc uses internally, but it could well be strtoull().

> unsigned long long value = 123456789011121314151617181920;
>
>  warning: integer constant is too large for its type
>   unsigned long long value = 123456789011121314151617181920;
>                                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> it is funny that this is a  warning and not an error.

That's a constraint violation (N1570 6.4.4p2), but I'm not surprised
that gcc issues a mere warning.  A non-fatal warning is a diagnostic, so
it satisfies the standard's requirement.  gcc does that by default in a
lot of cases.  Use "-pedantic-errors" to get a fatal error.

(My personal preference would be for constraint violations to be treated
as fatal errors by defaut, but the standard doesn't require that.)

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

unsafe C Thiago Adams <thiago.adams@gmail.com> - 2021-12-17 12:13 -0800
  Re: unsafe C Thiago Adams <thiago.adams@gmail.com> - 2021-12-17 12:16 -0800
    Re: unsafe C Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-12-17 12:25 -0800
    Re: unsafe C Meredith Montgomery <mmontgomery@levado.to> - 2021-12-18 12:40 -0300
      Re: unsafe C Meredith Montgomery <mmontgomery@levado.to> - 2021-12-25 21:41 -0300
        Re: unsafe C Meredith Montgomery <mmontgomery@levado.to> - 2021-12-25 21:42 -0300
    Re: unsafe C Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-12-18 12:29 -0800
  Re: unsafe C Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-12-17 12:20 -0800
    Re: unsafe C Thiago Adams <thiago.adams@gmail.com> - 2021-12-17 12:29 -0800
    Re: unsafe C Peter 'Shaggy' Haywood <phaywood@alphalink.com.au> - 2021-12-19 17:16 +1100
      Re: unsafe C Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-12-19 13:32 -0800
  Re: unsafe C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-12-17 20:30 +0000
    Re: unsafe C Thiago Adams <thiago.adams@gmail.com> - 2021-12-17 12:35 -0800

csiph-web