Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

"I'm not afraid of dying, I just don't want to be there when it happens." -- Woody Allen


devel / comp.lang.c / unsafe C

SubjectAuthor
* unsafe CThiago Adams
+* Re: unsafe CThiago Adams
|+- Re: unsafe CKeith Thompson
|+* Re: unsafe CMeredith Montgomery
||`* Re: unsafe CStefan Ram
|| `* Re: unsafe CMeredith Montgomery
||  `- Re: unsafe CMeredith Montgomery
|`- Re: unsafe CAndrey Tarasevich
+* Re: unsafe CKeith Thompson
|+- Re: unsafe CThiago Adams
|`* Re: unsafe CPeter 'Shaggy' Haywood
| `- Re: unsafe CKeith Thompson
`* Re: unsafe CBen Bacarisse
 `- Re: unsafe CThiago Adams

1
unsafe C

<d8227134-6e0b-486e-af91-aeece50a466bn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19492&group=comp.lang.c#19492

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:6214:238e:: with SMTP id fw14mr3892806qvb.86.1639772005163;
Fri, 17 Dec 2021 12:13:25 -0800 (PST)
X-Received: by 2002:a05:6214:1d2b:: with SMTP id f11mr4025778qvd.7.1639772004992;
Fri, 17 Dec 2021 12:13:24 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Fri, 17 Dec 2021 12:13:24 -0800 (PST)
Injection-Info: google-groups.googlegroups.com; posting-host=189.6.248.114; posting-account=xFcAQAoAAAAoWlfpQ6Hz2n-MU9fthxbY
NNTP-Posting-Host: 189.6.248.114
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <d8227134-6e0b-486e-af91-aeece50a466bn@googlegroups.com>
Subject: unsafe C
From: thiago.a...@gmail.com (Thiago Adams)
Injection-Date: Fri, 17 Dec 2021 20:13:25 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 14
 by: Thiago Adams - Fri, 17 Dec 2021 20:13 UTC

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

Re: unsafe C

<54ebbb14-cb1e-4dd5-a4ec-9dd086e0eaa3n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19493&group=comp.lang.c#19493

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a37:8883:: with SMTP id k125mr2978615qkd.464.1639772182829;
Fri, 17 Dec 2021 12:16:22 -0800 (PST)
X-Received: by 2002:a05:620a:d93:: with SMTP id q19mr2969929qkl.9.1639772182671;
Fri, 17 Dec 2021 12:16:22 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Fri, 17 Dec 2021 12:16:22 -0800 (PST)
In-Reply-To: <d8227134-6e0b-486e-af91-aeece50a466bn@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=189.6.248.114; posting-account=xFcAQAoAAAAoWlfpQ6Hz2n-MU9fthxbY
NNTP-Posting-Host: 189.6.248.114
References: <d8227134-6e0b-486e-af91-aeece50a466bn@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <54ebbb14-cb1e-4dd5-a4ec-9dd086e0eaa3n@googlegroups.com>
Subject: Re: unsafe C
From: thiago.a...@gmail.com (Thiago Adams)
Injection-Date: Fri, 17 Dec 2021 20:16:22 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 27
 by: Thiago Adams - Fri, 17 Dec 2021 20:16 UTC

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.

Re: unsafe C

<87tuf76l17.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19494&group=comp.lang.c#19494

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: unsafe C
Date: Fri, 17 Dec 2021 12:20:20 -0800
Organization: None to speak of
Lines: 36
Message-ID: <87tuf76l17.fsf@nosuchdomain.example.com>
References: <d8227134-6e0b-486e-af91-aeece50a466bn@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="17e45197bd43a11ab6e05b77d4ced81f";
logging-data="28558"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/OvtJxPianTjZqmFPjEqRV"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:1hSfuMsjAhcbhzhqyMovzcQFoh0=
sha1:XLdiKMTD6IY510pZeUcrxzNCg6I=
 by: Keith Thompson - Fri, 17 Dec 2021 20:20 UTC

Thiago Adams <thiago.adams@gmail.com> writes:
> 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

It's a little tricky, but it's not unsafe.

The strtol, strtoll, strtoul, and strtoull functions return the
converted value, if any. If no conversion could be performed, zero
is returned. If the correct value is outside the range of
representable values, LONG_MIN, LONG_MAX, LLONG_MIN, LLONG_MAX,
ULONG_MAX, or ULLONG_MAX is returned (according to the return type
and sign of the value, if any), and the value of the macro ERANGE is
stored in errno.

If you set errno to 0 before calling strtoull() and check its value
after the call (before calling anything else that might set errno),
you'll find it's been set to ERANGE to indicate the error.

(18446744073709551615 is LLONG_MAX, assuming 64 bits.)

--
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 */

Re: unsafe C

<87pmpv6kss.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19495&group=comp.lang.c#19495

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: unsafe C
Date: Fri, 17 Dec 2021 12:25:23 -0800
Organization: None to speak of
Lines: 40
Message-ID: <87pmpv6kss.fsf@nosuchdomain.example.com>
References: <d8227134-6e0b-486e-af91-aeece50a466bn@googlegroups.com>
<54ebbb14-cb1e-4dd5-a4ec-9dd086e0eaa3n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="17e45197bd43a11ab6e05b77d4ced81f";
logging-data="28558"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+1JDcJgrwqQRfEVcR7AJ88"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:7lV2gm1TgeKQN7LSB6q2ZlBQ2vI=
sha1:RYM1WMpt/oIn2PvzkdEINuq5bDg=
 by: Keith Thompson - Fri, 17 Dec 2021 20:25 UTC

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 */

Re: unsafe C

<928dd852-efe3-43af-91cf-acdbac8b4005n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19496&group=comp.lang.c#19496

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:620a:a09:: with SMTP id i9mr2973644qka.768.1639772971913;
Fri, 17 Dec 2021 12:29:31 -0800 (PST)
X-Received: by 2002:a05:620a:440d:: with SMTP id v13mr2895814qkp.597.1639772971675;
Fri, 17 Dec 2021 12:29:31 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Fri, 17 Dec 2021 12:29:31 -0800 (PST)
In-Reply-To: <87tuf76l17.fsf@nosuchdomain.example.com>
Injection-Info: google-groups.googlegroups.com; posting-host=189.6.248.114; posting-account=xFcAQAoAAAAoWlfpQ6Hz2n-MU9fthxbY
NNTP-Posting-Host: 189.6.248.114
References: <d8227134-6e0b-486e-af91-aeece50a466bn@googlegroups.com> <87tuf76l17.fsf@nosuchdomain.example.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <928dd852-efe3-43af-91cf-acdbac8b4005n@googlegroups.com>
Subject: Re: unsafe C
From: thiago.a...@gmail.com (Thiago Adams)
Injection-Date: Fri, 17 Dec 2021 20:29:31 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 53
 by: Thiago Adams - Fri, 17 Dec 2021 20:29 UTC

On Friday, December 17, 2021 at 5:20:34 PM UTC-3, Keith Thompson wrote:
> Thiago Adams <thiago...@gmail.com> writes:
> > 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
> It's a little tricky, but it's not unsafe.
>
> The strtol, strtoll, strtoul, and strtoull functions return the
> converted value, if any. If no conversion could be performed, zero
> is returned. If the correct value is outside the range of
> representable values, LONG_MIN, LONG_MAX, LLONG_MIN, LLONG_MAX,
> ULONG_MAX, or ULLONG_MAX is returned (according to the return type
> and sign of the value, if any), and the value of the macro ERANGE is
> stored in errno.
>
> If you set errno to 0 before calling strtoull() and check its value
> after the call (before calling anything else that might set errno),
> you'll find it's been set to ERANGE to indicate the error.
>
> (18446744073709551615 is LLONG_MAX, assuming 64 bits.)
>
> --
> Keith Thompson (The_Other_Keith) Keith.S.T...@gmail.com
> Working, but not speaking, for Philips
> void Void(void) { Void(); } /* The recursive call of the void */

wow.. I wasn't unexpecting that I didn't check the documentation
thanks.
In any case I think I will implement a safer version then I can have
a warning in my c front end.

C23 will add some checked function.

#include <stdckdint.h>

bool ckd_add(type1 *result, type2 a, type3 b);
bool ckd_sub(type1 *result, type2 a, type3 b);
bool ckd_mul(type1 *result, type2 a, type3 b);

http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2792.pdf

I can try to implement a safer strtoull using these functions.
and of course I will have to find a implementation of ckd_mul and ckd_add

Re: unsafe C

<87fsqrrn2s.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19497&group=comp.lang.c#19497

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: unsafe C
Date: Fri, 17 Dec 2021 20:30:35 +0000
Organization: A noiseless patient Spider
Lines: 23
Message-ID: <87fsqrrn2s.fsf@bsb.me.uk>
References: <d8227134-6e0b-486e-af91-aeece50a466bn@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="28408566c190b4dbe1f1624967533111";
logging-data="13541"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/fsYKpVTEps08KLKuUPrYSNCxsWUtFFA4="
Cancel-Lock: sha1:x9G+b1Qf9Rwm7U5bpLVwrnRdVYI=
sha1:7+0iDAylQlZJJjmQY+wi4HS+UL0=
X-BSB-Auth: 1.7effec3e09750ae80d9a.20211217203035GMT.87fsqrrn2s.fsf@bsb.me.uk
 by: Ben Bacarisse - Fri, 17 Dec 2021 20:30 UTC

Thiago Adams <thiago.adams@gmail.com> writes:

> 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

You didn't listen to what strtoull had to say! When the result would be
out of range for the return type, the strto* functions return the
maximum or minimum representable value and set errno to ERANGE.

--
Ben.

Re: unsafe C

<06df9dc6-8d5a-4567-9ac6-0327d56e63a0n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19498&group=comp.lang.c#19498

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ac8:5e47:: with SMTP id i7mr4067657qtx.600.1639773325379;
Fri, 17 Dec 2021 12:35:25 -0800 (PST)
X-Received: by 2002:ac8:7d46:: with SMTP id h6mr4126600qtb.379.1639773325232;
Fri, 17 Dec 2021 12:35:25 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Fri, 17 Dec 2021 12:35:25 -0800 (PST)
In-Reply-To: <87fsqrrn2s.fsf@bsb.me.uk>
Injection-Info: google-groups.googlegroups.com; posting-host=189.6.248.114; posting-account=xFcAQAoAAAAoWlfpQ6Hz2n-MU9fthxbY
NNTP-Posting-Host: 189.6.248.114
References: <d8227134-6e0b-486e-af91-aeece50a466bn@googlegroups.com> <87fsqrrn2s.fsf@bsb.me.uk>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <06df9dc6-8d5a-4567-9ac6-0327d56e63a0n@googlegroups.com>
Subject: Re: unsafe C
From: thiago.a...@gmail.com (Thiago Adams)
Injection-Date: Fri, 17 Dec 2021 20:35:25 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 28
 by: Thiago Adams - Fri, 17 Dec 2021 20:35 UTC

On Friday, December 17, 2021 at 5:30:46 PM UTC-3, Ben Bacarisse wrote:
> Thiago Adams <thiago...@gmail.com> writes:
>
> > 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
> You didn't listen to what strtoull had to say! When the result would be
> out of range for the return type, the strto* functions return the
> maximum or minimum representable value and set errno to ERANGE.

Yes, I can use strtoull to emit this warnings. You are right.
I don't need to implement a new one just check the result. The same for gcc,
they can use strtoull internally.

And I can use the unsigned long long version to check overflow for small integers
like int or char.

char c = 12345;

Re: unsafe C

<86wnk153br.fsf@levado.to>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19525&group=comp.lang.c#19525

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!w1hPHPb/YrPziOBjHD1tGA.user.46.165.242.75.POSTED!not-for-mail
From: mmontgom...@levado.to (Meredith Montgomery)
Newsgroups: comp.lang.c
Subject: Re: unsafe C
Date: Sat, 18 Dec 2021 12:40:24 -0300
Organization: Aioe.org NNTP Server
Message-ID: <86wnk153br.fsf@levado.to>
References: <d8227134-6e0b-486e-af91-aeece50a466bn@googlegroups.com>
<54ebbb14-cb1e-4dd5-a4ec-9dd086e0eaa3n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: gioia.aioe.org; logging-data="27233"; posting-host="w1hPHPb/YrPziOBjHD1tGA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
Cancel-Lock: sha1:cBGhv3HHbFuITkYYHXBJuUADeOM=
 by: Meredith Montgomery - Sat, 18 Dec 2021 15:40 UTC

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---

Re: unsafe C

<splgb5$v7n$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19541&group=comp.lang.c#19541

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: andreyta...@hotmail.com (Andrey Tarasevich)
Newsgroups: comp.lang.c
Subject: Re: unsafe C
Date: Sat, 18 Dec 2021 12:29:23 -0800
Organization: A noiseless patient Spider
Lines: 54
Message-ID: <splgb5$v7n$1@dont-email.me>
References: <d8227134-6e0b-486e-af91-aeece50a466bn@googlegroups.com>
<54ebbb14-cb1e-4dd5-a4ec-9dd086e0eaa3n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 18 Dec 2021 20:29:25 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="b9da1621abdb876b8305b5b5621fdcfc";
logging-data="31991"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18h2KbrwWx0rnkkwxonm3Rg"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.4.0
Cancel-Lock: sha1:yDkfokId/DZpySLKiYMug0KRabE=
In-Reply-To: <54ebbb14-cb1e-4dd5-a4ec-9dd086e0eaa3n@googlegroups.com>
Content-Language: en-US
 by: Andrey Tarasevich - Sat, 18 Dec 2021 20:29 UTC

On 12/17/2021 12:16 PM, Thiago Adams wrote:
> 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.
>

By default GCC works in a sort of "backward compatibility" mode. It
attempts to marry two seemingly incompatible requirements:

1. Properly supporting modern C standards
2. Compiling legacy code, which often contains what's considered
constraint violations (i.e. hard errors) by modern C standards

In other to achieve that GCC uses a "loophole" purposely designed into
the standard: a compiler is required to issue a diagnostic message for a
constraint violation, but is not required to stop translation or refuse
to produce some sort of "result".

So, this is what GCC often does: it barks a "warning" for a hard error,
and just continues translation following some sort of amalgamation of
legacy C standards. This is what many GCC "warnings" are: hard errors,
which just happen to be reported as non-fatal warnings.

In your specific example you are looking at a constant that is too long
to fit into the largest integer type. In C89/90 this situation was not
considered a constraint violation. It was handed over to undefined
behavior. Starting from C99 this became a constraint violation. This
disparity between C89/90 and later C standards is what persuades GCC to
report this error as a "warning".

--
Best regards,
Andrey Tarasevich

Re: unsafe C

<pos-20211219061641@ram.dialup.fu-berlin.de>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19547&group=comp.lang.c#19547

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram...@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.c
Subject: Re: unsafe C
Date: 19 Dec 2021 05:19:24 GMT
Organization: Stefan Ram
Lines: 17
Expires: 1 Mar 2022 11:59:58 GMT
Message-ID: <pos-20211219061641@ram.dialup.fu-berlin.de>
References: <d8227134-6e0b-486e-af91-aeece50a466bn@googlegroups.com> <54ebbb14-cb1e-4dd5-a4ec-9dd086e0eaa3n@googlegroups.com> <86wnk153br.fsf@levado.to>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de 92r54Jy0LdRq2BESD9p8owcdT2rPgvru1/LYLZ75JT59Fk
X-Copyright: (C) Copyright 2021 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Accept-Language: de-DE, en-US, it, fr-FR
 by: Stefan Ram - Sun, 19 Dec 2021 05:19 UTC

Meredith Montgomery <mmontgomery@levado.to> writes:
>> it is funny that this is a warning and not an error.

>int scan_ulong(register char *s, register unsigned long *u)
>{
> register unsigned int pos;
....
> return pos;
>}

It returns the /unsigned/ int "pos", but its return type is
declared /signed/ int?

/u/ is accessed at most once in the function, but declared
"register"?

Re: unsafe C

<8av29i-dr1.ln1@aretha.foo>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19553&group=comp.lang.c#19553

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!buffer2.nntp.dca1.giganews.com!news.giganews.com.POSTED!not-for-mail
NNTP-Posting-Date: Sun, 19 Dec 2021 07:08:37 -0600
Message-Id: <8av29i-dr1.ln1@aretha.foo>
From: phayw...@alphalink.com.au (Peter 'Shaggy' Haywood)
Subject: Re: unsafe C
Newsgroups: comp.lang.c
Date: Sun, 19 Dec 2021 17:16:08 +1100
References: <d8227134-6e0b-486e-af91-aeece50a466bn@googlegroups.com> <87tuf76l17.fsf@nosuchdomain.example.com>
User-Agent: KNode/0.10.9
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7Bit
Lines: 15
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-d3gCHVTMXDFhpfd4m23yM/A92Qyo9OZBGhghBUITt2/3Z9r0yJZu6fNVMPPVf/Ii9+F5faPA5Xu+DCg!H4svFlFjnUjb8VWyr4ygnE6jSs5KGbrNNfZLamEnvM+riVLaOQ==
X-Complaints-To: abuse@giganews.com
X-DMCA-Notifications: http://www.giganews.com/info/dmca.html
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
X-Original-Bytes: 1372
 by: Peter 'Shaggy&# - Sun, 19 Dec 2021 06:16 UTC

Groovy hepcat Keith Thompson was jivin' in comp.lang.c on Sat, 18 Dec
2021 07:20 am. It's a cool scene! Dig it.

> (18446744073709551615 is LLONG_MAX, assuming 64 bits.)

ULLONG_MAX, no?

--

----- Dig the NEW and IMPROVED news sig!! -----

-------------- Shaggy was here! ---------------
Ain't I'm a dawg!!

Re: unsafe C

<875yrk702n.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19566&group=comp.lang.c#19566

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: unsafe C
Date: Sun, 19 Dec 2021 13:32:16 -0800
Organization: None to speak of
Lines: 14
Message-ID: <875yrk702n.fsf@nosuchdomain.example.com>
References: <d8227134-6e0b-486e-af91-aeece50a466bn@googlegroups.com>
<87tuf76l17.fsf@nosuchdomain.example.com> <8av29i-dr1.ln1@aretha.foo>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="673c625a844bfdb5f5ee20e169bd0beb";
logging-data="20489"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/5mdGJlUTvAHvszhb9b12N"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:OXNpuicfCFJsh1pEBuvDpPALUDQ=
sha1:5uZh8wdyNoPPDSa0SkBtZx+HEio=
 by: Keith Thompson - Sun, 19 Dec 2021 21:32 UTC

Peter 'Shaggy' Haywood <phaywood@alphalink.com.au> writes:
> Groovy hepcat Keith Thompson was jivin' in comp.lang.c on Sat, 18 Dec
> 2021 07:20 am. It's a cool scene! Dig it.
>
>> (18446744073709551615 is LLONG_MAX, assuming 64 bits.)
>
> ULLONG_MAX, no?

ULLONG_MAX, yes.

--
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 */

Re: unsafe C

<86sfug19l8.fsf@levado.to>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19645&group=comp.lang.c#19645

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!Q29r0ETj/jjLPjDuGSMcjw.user.46.165.242.75.POSTED!not-for-mail
From: mmontgom...@levado.to (Meredith Montgomery)
Newsgroups: comp.lang.c
Subject: Re: unsafe C
Date: Sat, 25 Dec 2021 21:41:23 -0300
Organization: Aioe.org NNTP Server
Message-ID: <86sfug19l8.fsf@levado.to>
References: <d8227134-6e0b-486e-af91-aeece50a466bn@googlegroups.com>
<54ebbb14-cb1e-4dd5-a4ec-9dd086e0eaa3n@googlegroups.com>
<86wnk153br.fsf@levado.to>
<pos-20211219061641@ram.dialup.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: gioia.aioe.org; logging-data="21226"; posting-host="Q29r0ETj/jjLPjDuGSMcjw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
Cancel-Lock: sha1:FdPt5vRJMpz7+Q4VI7P4phfIYgc=
X-Notice: Filtered by postfilter v. 0.9.2
 by: Meredith Montgomery - Sun, 26 Dec 2021 00:41 UTC

ram@zedat.fu-berlin.de (Stefan Ram) writes:

> Meredith Montgomery <mmontgomery@levado.to> writes:
>>> it is funny that this is a warning and not an error.
>
>>int scan_ulong(register char *s, register unsigned long *u)
>>{
>> register unsigned int pos;
> ...
>> return pos;
>>}
>
> It returns the /unsigned/ int "pos", but its return type is
> declared /signed/ int?
>
> /u/ is accessed at most once in the function, but declared
> "register"?

Let's see. I don't think I would've changed that much, but I might.
Lol. I did. I can't even copy properly! Thanks for spotting that.

Hm, wait! The check for overflow is not his. It's mine. Alas, memory
failing me. Daniel J. Bernstein, sorry about that.

--8<---------------cut here---------------start------------->8---
/* Public domain. */

#include "scan.h"

unsigned int scan_ulong(register const char *s,register unsigned long *u)
{ register unsigned int pos = 0;
register unsigned long result = 0;
register unsigned long c;
while ((c = (unsigned long) (unsigned char) (s[pos] - '0')) < 10) {
result = result * 10 + c;
++pos;
}
*u = result;
return pos;
} --8<---------------cut here---------------end--------------->8---

But, yes, he did qualify u with ``register''.

Re: unsafe C

<86lf0819k7.fsf@levado.to>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19646&group=comp.lang.c#19646

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!Q29r0ETj/jjLPjDuGSMcjw.user.46.165.242.75.POSTED!not-for-mail
From: mmontgom...@levado.to (Meredith Montgomery)
Newsgroups: comp.lang.c
Subject: Re: unsafe C
Date: Sat, 25 Dec 2021 21:42:00 -0300
Organization: Aioe.org NNTP Server
Message-ID: <86lf0819k7.fsf@levado.to>
References: <d8227134-6e0b-486e-af91-aeece50a466bn@googlegroups.com>
<54ebbb14-cb1e-4dd5-a4ec-9dd086e0eaa3n@googlegroups.com>
<86wnk153br.fsf@levado.to>
<pos-20211219061641@ram.dialup.fu-berlin.de>
<86sfug19l8.fsf@levado.to>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: gioia.aioe.org; logging-data="21226"; posting-host="Q29r0ETj/jjLPjDuGSMcjw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
Cancel-Lock: sha1:PpsWez++/V2zEb0zvcf3OeEG5f8=
 by: Meredith Montgomery - Sun, 26 Dec 2021 00:42 UTC

Meredith Montgomery <mmontgomery@levado.to> writes:

> ram@zedat.fu-berlin.de (Stefan Ram) writes:
>
>> Meredith Montgomery <mmontgomery@levado.to> writes:
>>>> it is funny that this is a warning and not an error.
>>
>>>int scan_ulong(register char *s, register unsigned long *u)
>>>{
>>> register unsigned int pos;
>> ...
>>> return pos;
>>>}
>>
>> It returns the /unsigned/ int "pos", but its return type is
>> declared /signed/ int?
>>
>> /u/ is accessed at most once in the function, but declared
>> "register"?
>
> Let's see. I don't think I would've changed that much, but I might.
> Lol. I did. I can't even copy properly! Thanks for spotting that.
>
> Hm, wait! The check for overflow is not his. It's mine. Alas, memory
> failing me. Daniel J. Bernstein, sorry about that.
>
> /* Public domain. */
>
> #include "scan.h"
>
> unsigned int scan_ulong(register const char *s,register unsigned long *u)
> {
> register unsigned int pos = 0;
> register unsigned long result = 0;
> register unsigned long c;
> while ((c = (unsigned long) (unsigned char) (s[pos] - '0')) < 10) {
> result = result * 10 + c;
> ++pos;
> }
> *u = result;
> return pos;
> }
>
> But, yes, he did qualify u with ``register''.

By the way, that's src/scan_ulong.c in daemontools 0.76.

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor