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


Groups > comp.lang.c > #163959

Re: unsafe C

From Meredith Montgomery <mmontgomery@levado.to>
Newsgroups comp.lang.c
Subject Re: unsafe C
Date 2021-12-18 12:40 -0300
Organization Aioe.org NNTP Server
Message-ID <86wnk153br.fsf@levado.to> (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:
>
> 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.

Lol.  So true.

When I read an integer from a numeric-string, I usually use a procedure
that I once saw in some library written by Daniel J. Bernstein.  The
code below is my typing of his code, so I might have changed variable
names, spacing, added or removed comments or something like that.

--8<---------------cut here---------------start------------->8---
#include <limits.h>
#include <inttypes.h>

int scan_ulong(register char *s, register unsigned long *u) 
{
  register unsigned int pos;
  register unsigned long r;
  register unsigned long c;

  pos = 0; r = 0;

  for ( ;; ) {
    c = (unsigned long) (unsigned char) (s[pos] - '0');
    if (c < 10) {
      if( ((ULONG_MAX - c) / 10) >= r) 
        r = r * 10 + c;
      else return -1; /* lack of space */
      ++pos; continue;
    }
    break;
  }

  *u = r;
  return pos;
}
--8<---------------cut here---------------end--------------->8---

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