Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

In computing, the mean time to failure keeps getting shorter.


devel / comp.lang.c / uint8_t overflow in for ... loop

SubjectAuthor
* uint8_t overflow in for ... loopHowerd Oakford
+* Re: uint8_t overflow in for ... loopBart
|`* Re: uint8_t overflow in for ... loopHowerd Oakford
| `* Re: uint8_t overflow in for ... loopThiago Adams
|  `* Re: uint8_t overflow in for ... loopHowerd Oakford
|   +* Re: uint8_t overflow in for ... loopBen Bacarisse
|   |`- Re: uint8_t overflow in for ... loopGuillaume
|   `- Re: uint8_t overflow in for ... loopÖö Tiib
+* Re: uint8_t overflow in for ... loopDavid Brown
|+* Re: uint8_t overflow in for ... loopHowerd Oakford
||+* Re: uint8_t overflow in for ... loopDavid Brown
|||`- Re: uint8_t overflow in for ... loopHowerd Oakford
||+- Re: uint8_t overflow in for ... loopKeith Thompson
||`- Re: uint8_t overflow in for ... loopBen Bacarisse
|+* Re: uint8_t overflow in for ... loopMeredith Montgomery
||+* Re: uint8_t overflow in for ... loopScott Lurndal
|||+* Re: uint8_t overflow in for ... loopManfred
||||`* Re: uint8_t overflow in for ... loopTim Rentsch
|||| `* Re: uint8_t overflow in for ... loopManfred
||||  +- Re: uint8_t overflow in for ... loopKeith Thompson
||||  +- Re: uint8_t overflow in for ... loopDavid Brown
||||  `- Re: uint8_t overflow in for ... loopTim Rentsch
|||+* Re: uint8_t overflow in for ... loopKeith Thompson
||||`- Re: uint8_t overflow in for ... loopDavid Brown
|||`- Re: uint8_t overflow in for ... loopBonita Montero
||`* Re: uint8_t overflow in for ... loopDavid Brown
|| `* Re: uint8_t overflow in for ... loopMeredith Montgomery
||  `- Re: uint8_t overflow in for ... loopDavid Brown
|+* Re: uint8_t overflow in for ... loopManfred
||`- Re: uint8_t overflow in for ... loopDavid Brown
|`* Re: uint8_t overflow in for ... loopKeith Thompson
| `- Re: uint8_t overflow in for ... loopDavid Brown
`- Re: uint8_t overflow in for ... loopGuillaume

Pages:12
uint8_t overflow in for ... loop

<704c07ac-5bf1-4256-a82c-61088d1e00aan@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:620a:2903:: with SMTP id m3mr48826095qkp.452.1636369864267;
Mon, 08 Nov 2021 03:11:04 -0800 (PST)
X-Received: by 2002:a05:620a:2909:: with SMTP id m9mr62284711qkp.161.1636369864015;
Mon, 08 Nov 2021 03:11:04 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Mon, 8 Nov 2021 03:11:03 -0800 (PST)
Injection-Info: google-groups.googlegroups.com; posting-host=2a01:c22:bd89:ce00:51c2:ed23:8a4:ceac;
posting-account=o1zNBwoAAABP2KiH5g6nPQp_sYWidVdt
NNTP-Posting-Host: 2a01:c22:bd89:ce00:51c2:ed23:8a4:ceac
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <704c07ac-5bf1-4256-a82c-61088d1e00aan@googlegroups.com>
Subject: uint8_t overflow in for ... loop
From: howe...@gmail.com (Howerd Oakford)
Injection-Date: Mon, 08 Nov 2021 11:11:04 +0000
Content-Type: text/plain; charset="UTF-8"
 by: Howerd Oakford - Mon, 8 Nov 2021 11:11 UTC

Hi C-folks,

Is the code listed below valid C?
Specifically, is "i < 256" valid?

I ask this because i is an 8 bit unsigned integer, and I would like to know if it is legal to compare it with a value that is outside the range of i?

Normally, I expect a for ... loop to count up to the value in the middle section, but in the case below it creates an infinite loop.

I tried it out on GCC ARM, GCC ARM cross compiler using Visual Studio Code, and MS Visual Studio - all three run for ever.

None of the compilers gave a warning...

Does this phenomenon have a name?
Thanks in advance,
Howerd

#include <stdio.h> /* printf */
#include <stdlib.h>
#include <stdint.h>

int main()
{ uint8_t i;

// below is an infinite loop :
for (i = 0; i < 256; i++)
{
if ((i % 16) == 0) printf("\n");
printf("%i ", i);
}
}

Re: uint8_t overflow in for ... loop

<smb1a5$r7v$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.nntp4.net!aioe.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: uint8_t overflow in for ... loop
Date: Mon, 8 Nov 2021 11:22:47 +0000
Organization: A noiseless patient Spider
Lines: 45
Message-ID: <smb1a5$r7v$1@dont-email.me>
References: <704c07ac-5bf1-4256-a82c-61088d1e00aan@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 8 Nov 2021 11:22:45 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="5aecade92803dad329ae52332fcb0c0e";
logging-data="27903"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/cqZAEUKg3No0xjAN2j0OcJgmqRjj42rA="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.0
Cancel-Lock: sha1:MGBS7Mik4TUpGoTmxm0HhbDJg58=
In-Reply-To: <704c07ac-5bf1-4256-a82c-61088d1e00aan@googlegroups.com>
 by: Bart - Mon, 8 Nov 2021 11:22 UTC

On 08/11/2021 11:11, Howerd Oakford wrote:
> Hi C-folks,
>
> Is the code listed below valid C?
> Specifically, is "i < 256" valid?
>
> I ask this because i is an 8 bit unsigned integer, and I would like to know if it is legal to compare it with a value that is outside the range of i?
>
> Normally, I expect a for ... loop to count up to the value in the middle section, but in the case below it creates an infinite loop.
>
> I tried it out on GCC ARM, GCC ARM cross compiler using Visual Studio Code, and MS Visual Studio - all three run for ever.
>
> None of the compilers gave a warning...
>
> Does this phenomenon have a name?
>
> Thanks in advance,
> Howerd
>
> #include <stdio.h> /* printf */
> #include <stdlib.h>
> #include <stdint.h>
>
> int main()
> {
> uint8_t i;
>
> // below is an infinite loop :
> for (i = 0; i < 256; i++)
> {
> if ((i % 16) == 0) printf("\n");
> printf("%i ", i);
> }
> }
>

The experts will shortly be along to tell you what's what.

But is there any reason to define i as an 8-bit value rather than an
ordinary int?

It causes problems here as it cannot represent values above 255, so will
ALWAYS be less than 256. The loop cannot terminate, as written.

Just use an int.

Re: uint8_t overflow in for ... loop

<4c160b19-cf27-4fee-b76f-65067ef4c573n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ac8:7d8e:: with SMTP id c14mr71546348qtd.209.1636373158847;
Mon, 08 Nov 2021 04:05:58 -0800 (PST)
X-Received: by 2002:ac8:5888:: with SMTP id t8mr83355875qta.121.1636373158577;
Mon, 08 Nov 2021 04:05:58 -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: Mon, 8 Nov 2021 04:05:58 -0800 (PST)
In-Reply-To: <smb1a5$r7v$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=2a01:c22:bd89:ce00:51c2:ed23:8a4:ceac;
posting-account=o1zNBwoAAABP2KiH5g6nPQp_sYWidVdt
NNTP-Posting-Host: 2a01:c22:bd89:ce00:51c2:ed23:8a4:ceac
References: <704c07ac-5bf1-4256-a82c-61088d1e00aan@googlegroups.com> <smb1a5$r7v$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <4c160b19-cf27-4fee-b76f-65067ef4c573n@googlegroups.com>
Subject: Re: uint8_t overflow in for ... loop
From: howe...@gmail.com (Howerd Oakford)
Injection-Date: Mon, 08 Nov 2021 12:05:58 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 79
 by: Howerd Oakford - Mon, 8 Nov 2021 12:05 UTC

On Monday, November 8, 2021 at 12:22:59 PM UTC+1, Bart wrote:
> On 08/11/2021 11:11, Howerd Oakford wrote:
> > Hi C-folks,
> >
> > Is the code listed below valid C?
> > Specifically, is "i < 256" valid?
> >
> > I ask this because i is an 8 bit unsigned integer, and I would like to know if it is legal to compare it with a value that is outside the range of i?
> >
> > Normally, I expect a for ... loop to count up to the value in the middle section, but in the case below it creates an infinite loop.
> >
> > I tried it out on GCC ARM, GCC ARM cross compiler using Visual Studio Code, and MS Visual Studio - all three run for ever.
> >
> > None of the compilers gave a warning...
> >
> > Does this phenomenon have a name?
> >
> > Thanks in advance,
> > Howerd
> >
> > #include <stdio.h> /* printf */
> > #include <stdlib.h>
> > #include <stdint.h>
> >
> > int main()
> > {
> > uint8_t i;
> >
> > // below is an infinite loop :
> > for (i = 0; i < 256; i++)
> > {
> > if ((i % 16) == 0) printf("\n");
> > printf("%i ", i);
> > }
> > }
> >
> The experts will shortly be along to tell you what's what.
>
> But is there any reason to define i as an 8-bit value rather than an
> ordinary int?
>
> It causes problems here as it cannot represent values above 255, so will
> ALWAYS be less than 256. The loop cannot terminate, as written.
>
> Just use an int.

Hi Bart,

Thanks for your quick response.

Yes - the fix is easy - use a bigger integer, or a smaller array :-)
But the real issues here are :
1. that this is a disastrous bug in code that I wrote, and
2. that was not detected by the compiler or any Linting tools that we are using (Understand and Eclair).

The actual code is parsing a file one line at a time. I allocated a 256 byte array for the line, and a uint8_t for the pointer.
I was implicitly using the wraparound from 255 to 0 to make sure that the pointer never strays outside the array.

To find that if we receive a file with 256 or more bytes on a line it will cause an infinite loop crash is bad enough, but that this is not noticed by any of the tools we are using is simply astonishing. A colleague spotted this by doing a very thorough unit test on the code.

Presumably this is legal code - I await some expert judgements on this ...

Cheers,
Howerd

Re: uint8_t overflow in for ... loop

<63576f6c-0f43-42ec-bd05-7628ae306db4n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:622a:1745:: with SMTP id l5mr14283846qtk.169.1636373851647;
Mon, 08 Nov 2021 04:17:31 -0800 (PST)
X-Received: by 2002:a05:6214:12d:: with SMTP id w13mr55528065qvs.39.1636373851385;
Mon, 08 Nov 2021 04:17:31 -0800 (PST)
Path: i2pn2.org!i2pn.org!paganini.bofh.team!news.dns-netz.com!news.freedyn.net!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!feeder1.cambriumusenet.nl!feed.tweak.nl!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Mon, 8 Nov 2021 04:17:31 -0800 (PST)
In-Reply-To: <4c160b19-cf27-4fee-b76f-65067ef4c573n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=189.6.249.78; posting-account=xFcAQAoAAAAoWlfpQ6Hz2n-MU9fthxbY
NNTP-Posting-Host: 189.6.249.78
References: <704c07ac-5bf1-4256-a82c-61088d1e00aan@googlegroups.com>
<smb1a5$r7v$1@dont-email.me> <4c160b19-cf27-4fee-b76f-65067ef4c573n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <63576f6c-0f43-42ec-bd05-7628ae306db4n@googlegroups.com>
Subject: Re: uint8_t overflow in for ... loop
From: thiago.a...@gmail.com (Thiago Adams)
Injection-Date: Mon, 08 Nov 2021 12:17:31 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
 by: Thiago Adams - Mon, 8 Nov 2021 12:17 UTC

On Monday, November 8, 2021 at 9:06:06 AM UTC-3, how...@gmail.com wrote:
> On Monday, November 8, 2021 at 12:22:59 PM UTC+1, Bart wrote:
> > On 08/11/2021 11:11, Howerd Oakford wrote:
> > > Hi C-folks,
> > >
> > > Is the code listed below valid C?
> > > Specifically, is "i < 256" valid?
> > >
> > > I ask this because i is an 8 bit unsigned integer, and I would like to know if it is legal to compare it with a value that is outside the range of i?
> > >
> > > Normally, I expect a for ... loop to count up to the value in the middle section, but in the case below it creates an infinite loop.
> > >
> > > I tried it out on GCC ARM, GCC ARM cross compiler using Visual Studio Code, and MS Visual Studio - all three run for ever.
> > >
> > > None of the compilers gave a warning...
> > >
> > > Does this phenomenon have a name?
> > >
> > > Thanks in advance,
> > > Howerd
> > >
> > > #include <stdio.h> /* printf */
> > > #include <stdlib.h>
> > > #include <stdint.h>
> > >
> > > int main()
> > > {
> > > uint8_t i;
> > >
> > > // below is an infinite loop :
> > > for (i = 0; i < 256; i++)
> > > {
> > > if ((i % 16) == 0) printf("\n");
> > > printf("%i ", i);
> > > }
> > > }
> > >
> > The experts will shortly be along to tell you what's what.
> >
> > But is there any reason to define i as an 8-bit value rather than an
> > ordinary int?
> >
> > It causes problems here as it cannot represent values above 255, so will
> > ALWAYS be less than 256. The loop cannot terminate, as written.
> >
> > Just use an int.
> Hi Bart,
>
> Thanks for your quick response.
>
> Yes - the fix is easy - use a bigger integer, or a smaller array :-)
> But the real issues here are :
> 1. that this is a disastrous bug in code that I wrote, and
> 2. that was not detected by the compiler or any Linting tools that we are using (Understand and Eclair).
>
> The actual code is parsing a file one line at a time. I allocated a 256 byte array for the line, and a uint8_t for the pointer.
> I was implicitly using the wraparound from 255 to 0 to make sure that the pointer never strays outside the array.
>
> To find that if we receive a file with 256 or more bytes on a line it will cause an infinite loop crash is bad enough, but that this is not noticed by any of the tools we are using is simply astonishing. A colleague spotted this by doing a very thorough unit test on the code.
>
> Presumably this is legal code - I await some expert judgements on this ....

This kind of bug is easily detected by lint programs.

I also tried in clang:

warning: result of comparison of constant 256 with expression of type
'uint8_t' (aka 'unsigned char') is always true [-Wtautological-constant-out-of-range-compare]

Re: uint8_t overflow in for ... loop

<smb6us$840$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: uint8_t overflow in for ... loop
Date: Mon, 8 Nov 2021 13:59:07 +0100
Organization: A noiseless patient Spider
Lines: 73
Message-ID: <smb6us$840$1@dont-email.me>
References: <704c07ac-5bf1-4256-a82c-61088d1e00aan@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 8 Nov 2021 12:59:08 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="8d4c8ed7efb99dba549c7b1d68bd2d39";
logging-data="8320"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX195dsNIikGXd5ndPcZG2B8TFMRQmar+oek="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:axM0oTggtcVhxJ78c/CZQMawXEc=
In-Reply-To: <704c07ac-5bf1-4256-a82c-61088d1e00aan@googlegroups.com>
Content-Language: en-GB
 by: David Brown - Mon, 8 Nov 2021 12:59 UTC

On 08/11/2021 12:11, Howerd Oakford wrote:
> Hi C-folks,
>
> Is the code listed below valid C?
> Specifically, is "i < 256" valid?

It is valid, it is just not helpful. A uint8_t variable will /always/
be less than 256, so the check is always "true".

>
> I ask this because i is an 8 bit unsigned integer, and I would like to know if it is legal to compare it with a value that is outside the range of i?
>
> Normally, I expect a for ... loop to count up to the value in the middle section, but in the case below it creates an infinite loop.
>

The uint8_t variable counts up to 255, then wraps to 0 - overflow is
always wrapping for unsigned types (unlike for signed types).
Basically, you loop is like asking to run something every hour until 13
o'clock.

> I tried it out on GCC ARM, GCC ARM cross compiler using Visual Studio Code, and MS Visual Studio - all three run for ever.
>

Good.

> None of the compilers gave a warning...
>

gcc will give you a warning if you ask it. The flag is "-Wtype-limits",
and it is enabled with "-Wextra". It's a good idea to have "-Wall
-Wextra" enabled while you are getting the hang of things, then perhaps
disable some warnings if you find they conflict with your style and give
false positives.

> Does this phenomenon have a name?
>
> Thanks in advance,
> Howerd
>
> #include <stdio.h> /* printf */
> #include <stdlib.h>
> #include <stdint.h>
>
> int main()

In c.l.c., we have a long tradition of complaining when people declare
"main" incorrectly. In C (until C23 comes out), if you want to declare
"main" with no parameters, you must write "int main(void)" - that's the
usual form for embedded programming.

> {
> uint8_t i;
>
> // below is an infinite loop :
> for (i = 0; i < 256; i++)

It's generally neater to put the variable declaration in the "for" :

for (uint8_t i = 0; i < 256; i++) {

Of course, you'll want to change that type - or the limit!

> {
> if ((i % 16) == 0) printf("\n");
> printf("%i ", i);
> }
> }
>

The big question is, why are you using "uint8_t" at all here? If you
were programming an AVR, I could understand it. But for the ARM you
generally get most efficient code using int32_t or uint32_t for local
variables.

Re: uint8_t overflow in for ... loop

<0d0a7819-ceea-42d3-a710-1cd4d9f4fd98n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:620a:2903:: with SMTP id m3mr49364400qkp.452.1636376603307;
Mon, 08 Nov 2021 05:03:23 -0800 (PST)
X-Received: by 2002:a05:6214:1c86:: with SMTP id ib6mr75567744qvb.26.1636376603055;
Mon, 08 Nov 2021 05:03:23 -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: Mon, 8 Nov 2021 05:03:22 -0800 (PST)
In-Reply-To: <63576f6c-0f43-42ec-bd05-7628ae306db4n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=2a01:c22:bd89:ce00:51c2:ed23:8a4:ceac;
posting-account=o1zNBwoAAABP2KiH5g6nPQp_sYWidVdt
NNTP-Posting-Host: 2a01:c22:bd89:ce00:51c2:ed23:8a4:ceac
References: <704c07ac-5bf1-4256-a82c-61088d1e00aan@googlegroups.com>
<smb1a5$r7v$1@dont-email.me> <4c160b19-cf27-4fee-b76f-65067ef4c573n@googlegroups.com>
<63576f6c-0f43-42ec-bd05-7628ae306db4n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <0d0a7819-ceea-42d3-a710-1cd4d9f4fd98n@googlegroups.com>
Subject: Re: uint8_t overflow in for ... loop
From: howe...@gmail.com (Howerd Oakford)
Injection-Date: Mon, 08 Nov 2021 13:03:23 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 97
 by: Howerd Oakford - Mon, 8 Nov 2021 13:03 UTC

On Monday, November 8, 2021 at 1:17:39 PM UTC+1, Thiago Adams wrote:
> On Monday, November 8, 2021 at 9:06:06 AM UTC-3, how...@gmail.com wrote:
> > On Monday, November 8, 2021 at 12:22:59 PM UTC+1, Bart wrote:
> > > On 08/11/2021 11:11, Howerd Oakford wrote:
> > > > Hi C-folks,
> > > >
> > > > Is the code listed below valid C?
> > > > Specifically, is "i < 256" valid?
> > > >
> > > > I ask this because i is an 8 bit unsigned integer, and I would like to know if it is legal to compare it with a value that is outside the range of i?
> > > >
> > > > Normally, I expect a for ... loop to count up to the value in the middle section, but in the case below it creates an infinite loop.
> > > >
> > > > I tried it out on GCC ARM, GCC ARM cross compiler using Visual Studio Code, and MS Visual Studio - all three run for ever.
> > > >
> > > > None of the compilers gave a warning...
> > > >
> > > > Does this phenomenon have a name?
> > > >
> > > > Thanks in advance,
> > > > Howerd
> > > >
> > > > #include <stdio.h> /* printf */
> > > > #include <stdlib.h>
> > > > #include <stdint.h>
> > > >
> > > > int main()
> > > > {
> > > > uint8_t i;
> > > >
> > > > // below is an infinite loop :
> > > > for (i = 0; i < 256; i++)
> > > > {
> > > > if ((i % 16) == 0) printf("\n");
> > > > printf("%i ", i);
> > > > }
> > > > }
> > > >
> > > The experts will shortly be along to tell you what's what.
> > >
> > > But is there any reason to define i as an 8-bit value rather than an
> > > ordinary int?
> > >
> > > It causes problems here as it cannot represent values above 255, so will
> > > ALWAYS be less than 256. The loop cannot terminate, as written.
> > >
> > > Just use an int.
> > Hi Bart,
> >
> > Thanks for your quick response.
> >
> > Yes - the fix is easy - use a bigger integer, or a smaller array :-)
> > But the real issues here are :
> > 1. that this is a disastrous bug in code that I wrote, and
> > 2. that was not detected by the compiler or any Linting tools that we are using (Understand and Eclair).
> >
> > The actual code is parsing a file one line at a time. I allocated a 256 byte array for the line, and a uint8_t for the pointer.
> > I was implicitly using the wraparound from 255 to 0 to make sure that the pointer never strays outside the array.
> >
> > To find that if we receive a file with 256 or more bytes on a line it will cause an infinite loop crash is bad enough, but that this is not noticed by any of the tools we are using is simply astonishing. A colleague spotted this by doing a very thorough unit test on the code.
> >
> > Presumably this is legal code - I await some expert judgements on this ....
> This kind of bug is easily detected by lint programs.
>
> I also tried in clang:
>
> warning: result of comparison of constant 256 with expression of type
> 'uint8_t' (aka 'unsigned char') is always true [-Wtautological-constant-out-of-range-compare]

Hi Thiago,

Thanks - I think we will have to take a look at clang!!!

Maybe we have some settings wrong in Understand and Eclair...

Does the warning from clang mean that the code is "illegal", or just "not recommended"?

In this case, the code is seriously wrong - causing a system crash if a file contains a single long line.

Cheers,
Howerd

Re: uint8_t overflow in for ... loop

<4d3da41b-483d-4f1f-b169-b9bb76b23ca0n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:620a:d96:: with SMTP id q22mr64041967qkl.219.1636377546801;
Mon, 08 Nov 2021 05:19:06 -0800 (PST)
X-Received: by 2002:a05:6214:8c8:: with SMTP id da8mr17888304qvb.23.1636377546626;
Mon, 08 Nov 2021 05:19:06 -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: Mon, 8 Nov 2021 05:19:06 -0800 (PST)
In-Reply-To: <smb6us$840$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=2a01:c22:bd89:ce00:51c2:ed23:8a4:ceac;
posting-account=o1zNBwoAAABP2KiH5g6nPQp_sYWidVdt
NNTP-Posting-Host: 2a01:c22:bd89:ce00:51c2:ed23:8a4:ceac
References: <704c07ac-5bf1-4256-a82c-61088d1e00aan@googlegroups.com> <smb6us$840$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <4d3da41b-483d-4f1f-b169-b9bb76b23ca0n@googlegroups.com>
Subject: Re: uint8_t overflow in for ... loop
From: howe...@gmail.com (Howerd Oakford)
Injection-Date: Mon, 08 Nov 2021 13:19:06 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 81
 by: Howerd Oakford - Mon, 8 Nov 2021 13:19 UTC

On Monday, November 8, 2021 at 1:59:20 PM UTC+1, David Brown wrote:
> On 08/11/2021 12:11, Howerd Oakford wrote:
> > Hi C-folks,
> >
> > Is the code listed below valid C?
> > Specifically, is "i < 256" valid?
> It is valid, it is just not helpful. A uint8_t variable will /always/
> be less than 256, so the check is always "true".
> >
> > I ask this because i is an 8 bit unsigned integer, and I would like to know if it is legal to compare it with a value that is outside the range of i?
> >
> > Normally, I expect a for ... loop to count up to the value in the middle section, but in the case below it creates an infinite loop.
> >
> The uint8_t variable counts up to 255, then wraps to 0 - overflow is
> always wrapping for unsigned types (unlike for signed types).
> Basically, you loop is like asking to run something every hour until 13
> o'clock.
> > I tried it out on GCC ARM, GCC ARM cross compiler using Visual Studio Code, and MS Visual Studio - all three run for ever.
> >
> Good.
> > None of the compilers gave a warning...
> >
> gcc will give you a warning if you ask it. The flag is "-Wtype-limits",
> and it is enabled with "-Wextra". It's a good idea to have "-Wall
> -Wextra" enabled while you are getting the hang of things, then perhaps
> disable some warnings if you find they conflict with your style and give
> false positives.
> > Does this phenomenon have a name?
> >
> > Thanks in advance,
> > Howerd
> >
> > #include <stdio.h> /* printf */
> > #include <stdlib.h>
> > #include <stdint.h>
> >
> > int main()
> In c.l.c., we have a long tradition of complaining when people declare
> "main" incorrectly. In C (until C23 comes out), if you want to declare
> "main" with no parameters, you must write "int main(void)" - that's the
> usual form for embedded programming.
> > {
> > uint8_t i;
> >
> > // below is an infinite loop :
> > for (i = 0; i < 256; i++)
> It's generally neater to put the variable declaration in the "for" :
>
> for (uint8_t i = 0; i < 256; i++) {
>
> Of course, you'll want to change that type - or the limit!
> > {
> > if ((i % 16) == 0) printf("\n");
> > printf("%i ", i);
> > }
> > }
> >
> The big question is, why are you using "uint8_t" at all here? If you
> were programming an AVR, I could understand it. But for the ARM you
> generally get most efficient code using int32_t or uint32_t for local
> variables.

Hi David,

Thanks! I added -Wall -Wextra and I get a warning now :-)

> The big question is, why are you using "uint8_t" at all here?
Company policy.

> Basically, your loop is like asking to run something every hour until 13 o'clock.
I like the analogy :-)

> you must write "int main(void)" - that's the usual form for embedded programming.
Sorry - typed in haste ;-)

One correction : Eclair does give a warning now, too.

Thanks for your response,

Cheers,
Howerd

Re: uint8_t overflow in for ... loop

<878rxy22a8.fsf@bsb.me.uk>

  copy mid

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

  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: uint8_t overflow in for ... loop
Date: Mon, 08 Nov 2021 13:41:03 +0000
Organization: A noiseless patient Spider
Lines: 24
Message-ID: <878rxy22a8.fsf@bsb.me.uk>
References: <704c07ac-5bf1-4256-a82c-61088d1e00aan@googlegroups.com>
<smb1a5$r7v$1@dont-email.me>
<4c160b19-cf27-4fee-b76f-65067ef4c573n@googlegroups.com>
<63576f6c-0f43-42ec-bd05-7628ae306db4n@googlegroups.com>
<0d0a7819-ceea-42d3-a710-1cd4d9f4fd98n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="ae245c3fc7f87ec92e88ec1c6c67efcd";
logging-data="24173"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/ndGoGjxvnoItSfcpyiEQHQYJyPJ1bUzM="
Cancel-Lock: sha1:vUppYpQs4p6zM+yp+UuPiLyHhVk=
sha1:nKsjyIbD+2SvyVFVs2SEOCec164=
X-BSB-Auth: 1.43a835f87334ff025b83.20211108134103GMT.878rxy22a8.fsf@bsb.me.uk
 by: Ben Bacarisse - Mon, 8 Nov 2021 13:41 UTC

Howerd Oakford <howerdo@gmail.com> writes:

> On Monday, November 8, 2021 at 1:17:39 PM UTC+1, Thiago Adams wrote:
>> On Monday, November 8, 2021 at 9:06:06 AM UTC-3, how...@gmail.com wrote:
<snip>
>> > .. that this
>> > is not noticed by any of the tools we are using is simply
>> > astonishing. A colleague spotted this by doing a very thorough unit
>> > test on the code.

>> I also tried in clang:
>>
>> warning: result of comparison of constant 256 with expression of type
>> 'uint8_t' (aka 'unsigned char') is always true
>> [-Wtautological-constant-out-of-range-compare]
>
> Thanks - I think we will have to take a look at clang!!!

gcc warns as well, but you have to ask (it's one of the -Wextra
warnings). Since for most uses I have gcc "turned up to 11" I get that
sort of warning as a matter of course.

--
Ben.

Re: uint8_t overflow in for ... loop

<861r3q21lq.fsf@levado.to>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!3D/X0sRaL/x9kZJBo2b3aA.user.46.165.242.75.POSTED!not-for-mail
From: mmontgom...@levado.to (Meredith Montgomery)
Newsgroups: comp.lang.c
Subject: Re: uint8_t overflow in for ... loop
Date: Mon, 08 Nov 2021 10:55:45 -0300
Organization: Aioe.org NNTP Server
Message-ID: <861r3q21lq.fsf@levado.to>
References: <704c07ac-5bf1-4256-a82c-61088d1e00aan@googlegroups.com>
<smb6us$840$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="14005"; posting-host="3D/X0sRaL/x9kZJBo2b3aA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
Cancel-Lock: sha1:rvxl6IREdnT5HCWVHX/MkTvwoHA=
 by: Meredith Montgomery - Mon, 8 Nov 2021 13:55 UTC

David Brown <david.brown@hesbynett.no> writes:

[...]

> The uint8_t variable counts up to 255, then wraps to 0 - overflow is
> always wrapping for unsigned types (unlike for signed types).

Technically the word is not ``overflow''.

--8<---------------cut here---------------start------------->8---
A computation involving unsigned operands can never overflow, because a
result that cannot be represented by the resulting unsigned integer type
is reduced modulo the number that is one greater than the largest value
that can be represented by the resulting unsigned integer type. [C89,
seção 3.1.2.5]
--8<---------------cut here---------------end--------------->8---

Re: uint8_t overflow in for ... loop

<smbcop$1jkt$1@gioia.aioe.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!Puiiztk9lHEEQC0y3uUjRA.user.46.165.242.75.POSTED!not-for-mail
From: non...@add.invalid (Manfred)
Newsgroups: comp.lang.c
Subject: Re: uint8_t overflow in for ... loop
Date: Mon, 8 Nov 2021 15:38:16 +0100
Organization: Aioe.org NNTP Server
Message-ID: <smbcop$1jkt$1@gioia.aioe.org>
References: <704c07ac-5bf1-4256-a82c-61088d1e00aan@googlegroups.com>
<smb6us$840$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="52893"; posting-host="Puiiztk9lHEEQC0y3uUjRA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.0
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: en-US
 by: Manfred - Mon, 8 Nov 2021 14:38 UTC

On 11/8/2021 1:59 PM, David Brown wrote:
> On 08/11/2021 12:11, Howerd Oakford wrote:
>> Hi C-folks,
>>
>> Is the code listed below valid C?
>> Specifically, is "i < 256" valid?
>
> It is valid, it is just not helpful. A uint8_t variable will /always/
> be less than 256, so the check is always "true".
>
>>
>> I ask this because i is an 8 bit unsigned integer, and I would like to know if it is legal to compare it with a value that is outside the range of i?
>>
>> Normally, I expect a for ... loop to count up to the value in the middle section, but in the case below it creates an infinite loop.
>>
>
> The uint8_t variable counts up to 255, then wraps to 0 - overflow is
> always wrapping for unsigned types (unlike for signed types).
> Basically, you loop is like asking to run something every hour until 13
> o'clock.
>
>> I tried it out on GCC ARM, GCC ARM cross compiler using Visual Studio Code, and MS Visual Studio - all three run for ever.
>>
>
> Good.
>
>> None of the compilers gave a warning...
>>
>
> gcc will give you a warning if you ask it. The flag is "-Wtype-limits",
> and it is enabled with "-Wextra". It's a good idea to have "-Wall
> -Wextra" enabled while you are getting the hang of things, then perhaps
> disable some warnings if you find they conflict with your style and give
> false positives.

Maybe this is worth an extra comment. Compilers, includong gcc, can
easily detect this because the expression "i < 256" is easy to check at
compile time. The issue would be a bit harder if it were "i < N" where N
would be an unsigned int with value 256 set somewhere else.
For this more general case the advice would be to give extra care to
comparisons between variables of different size and/or signedness.
In some cases compilers warn about this too, but, as an example, the
following is undetected by gcc -Wall -Wextra:

#include <stdio.h> /* printf */
#include <stdlib.h>
#include <stdint.h>

unsigned int imax()
{ return 256;
}

int main()
{ uint8_t i;

// below is an infinite loop :
for (i = 0; i < imax(); i++)
{
if ((i % 16) == 0) printf("\n");
printf("%i ", i);
}
}

[...]
>
> The big question is, why are you using "uint8_t" at all here? If you
> were programming an AVR, I could understand it. But for the ARM you
> generally get most efficient code using int32_t or uint32_t for local
> variables.
>

Or just a plain '[unsigned] int', which defaults to the machine word,
and is meant to be the general purpose integer type that is optimized
for performance in the absence of specific size requirements.
The standard guarantees that 'int' and 'unsigned int' are both at least
16 bit in size.

Re: uint8_t overflow in for ... loop

<JGaiJ.21677$Gco3.19158@fx01.iad>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!paganini.bofh.team!news.dns-netz.com!news.freedyn.net!newsreader4.netcologne.de!news.netcologne.de!peer02.ams1!peer.ams1.xlned.com!news.xlned.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx01.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: sco...@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: uint8_t overflow in for ... loop
Newsgroups: comp.lang.c
References: <704c07ac-5bf1-4256-a82c-61088d1e00aan@googlegroups.com> <smb6us$840$1@dont-email.me> <861r3q21lq.fsf@levado.to>
Lines: 13
Message-ID: <JGaiJ.21677$Gco3.19158@fx01.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Mon, 08 Nov 2021 14:41:45 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Mon, 08 Nov 2021 14:41:45 GMT
X-Received-Bytes: 1147
 by: Scott Lurndal - Mon, 8 Nov 2021 14:41 UTC

Meredith Montgomery <mmontgomery@levado.to> writes:
>David Brown <david.brown@hesbynett.no> writes:
>
>[...]
>
>> The uint8_t variable counts up to 255, then wraps to 0 - overflow is
>> always wrapping for unsigned types (unlike for signed types).
>
>Technically the word is not ``overflow''.
>

Technically, the processor sets the "overflow" flag. If that's not
an overflow, I'm not sure what it.

Re: uint8_t overflow in for ... loop

<smbef2$fa9$1@gioia.aioe.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!Puiiztk9lHEEQC0y3uUjRA.user.46.165.242.75.POSTED!not-for-mail
From: non...@add.invalid (Manfred)
Newsgroups: comp.lang.c
Subject: Re: uint8_t overflow in for ... loop
Date: Mon, 8 Nov 2021 16:07:13 +0100
Organization: Aioe.org NNTP Server
Message-ID: <smbef2$fa9$1@gioia.aioe.org>
References: <704c07ac-5bf1-4256-a82c-61088d1e00aan@googlegroups.com>
<smb6us$840$1@dont-email.me> <861r3q21lq.fsf@levado.to>
<JGaiJ.21677$Gco3.19158@fx01.iad>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="15689"; posting-host="Puiiztk9lHEEQC0y3uUjRA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.0
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: en-US
 by: Manfred - Mon, 8 Nov 2021 15:07 UTC

On 11/8/2021 3:41 PM, Scott Lurndal wrote:
> Meredith Montgomery <mmontgomery@levado.to> writes:
>> David Brown <david.brown@hesbynett.no> writes:
>>
>> [...]
>>
>>> The uint8_t variable counts up to 255, then wraps to 0 - overflow is
>>> always wrapping for unsigned types (unlike for signed types).
>>
>> Technically the word is not ``overflow''.
>>
>
> Technically, the processor sets the "overflow" flag. If that's not
> an overflow, I'm not sure what it.
>

The quote from the standard, that you stripped, is accurate:

> A computation involving unsigned operands can never overflow, because a
> result that cannot be represented by the resulting unsigned integer type
> is reduced modulo the number that is one greater than the largest value
> that can be represented by the resulting unsigned integer type. [C89,
> seção 3.1.2.5]

BTW is is almost identical in n2596, 6.2.5 clause 9.

If it "can never overflow", it's a statement that is clear enough.
I'd say the correct term is wrapping.

Re: uint8_t overflow in for ... loop

<smbf9n$a59$2@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: uint8_t overflow in for ... loop
Date: Mon, 8 Nov 2021 16:21:27 +0100
Organization: A noiseless patient Spider
Lines: 91
Message-ID: <smbf9n$a59$2@dont-email.me>
References: <704c07ac-5bf1-4256-a82c-61088d1e00aan@googlegroups.com>
<smb6us$840$1@dont-email.me>
<4d3da41b-483d-4f1f-b169-b9bb76b23ca0n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 8 Nov 2021 15:21:27 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="8d4c8ed7efb99dba549c7b1d68bd2d39";
logging-data="10409"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/jCmnclKQ4ExwajwjElPUTIhvFhLcV+xk="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:mLeeNy/NuFUQjofU4TAIURyLrOE=
In-Reply-To: <4d3da41b-483d-4f1f-b169-b9bb76b23ca0n@googlegroups.com>
Content-Language: en-GB
 by: David Brown - Mon, 8 Nov 2021 15:21 UTC

On 08/11/2021 14:19, Howerd Oakford wrote:
> On Monday, November 8, 2021 at 1:59:20 PM UTC+1, David Brown wrote:
>> On 08/11/2021 12:11, Howerd Oakford wrote:
>>> Hi C-folks,
>>>
>>> Is the code listed below valid C?
>>> Specifically, is "i < 256" valid?
>> It is valid, it is just not helpful. A uint8_t variable will /always/
>> be less than 256, so the check is always "true".
>>>
>>> I ask this because i is an 8 bit unsigned integer, and I would like to know if it is legal to compare it with a value that is outside the range of i?
>>>
>>> Normally, I expect a for ... loop to count up to the value in the middle section, but in the case below it creates an infinite loop.
>>>
>> The uint8_t variable counts up to 255, then wraps to 0 - overflow is
>> always wrapping for unsigned types (unlike for signed types).
>> Basically, you loop is like asking to run something every hour until 13
>> o'clock.
>>> I tried it out on GCC ARM, GCC ARM cross compiler using Visual Studio Code, and MS Visual Studio - all three run for ever.
>>>
>> Good.
>>> None of the compilers gave a warning...
>>>
>> gcc will give you a warning if you ask it. The flag is "-Wtype-limits",
>> and it is enabled with "-Wextra". It's a good idea to have "-Wall
>> -Wextra" enabled while you are getting the hang of things, then perhaps
>> disable some warnings if you find they conflict with your style and give
>> false positives.
>>> Does this phenomenon have a name?
>>>
>>> Thanks in advance,
>>> Howerd
>>>
>>> #include <stdio.h> /* printf */
>>> #include <stdlib.h>
>>> #include <stdint.h>
>>>
>>> int main()
>> In c.l.c., we have a long tradition of complaining when people declare
>> "main" incorrectly. In C (until C23 comes out), if you want to declare
>> "main" with no parameters, you must write "int main(void)" - that's the
>> usual form for embedded programming.
>>> {
>>> uint8_t i;
>>>
>>> // below is an infinite loop :
>>> for (i = 0; i < 256; i++)
>> It's generally neater to put the variable declaration in the "for" :
>>
>> for (uint8_t i = 0; i < 256; i++) {
>>
>> Of course, you'll want to change that type - or the limit!
>>> {
>>> if ((i % 16) == 0) printf("\n");
>>> printf("%i ", i);
>>> }
>>> }
>>>
>> The big question is, why are you using "uint8_t" at all here? If you
>> were programming an AVR, I could understand it. But for the ARM you
>> generally get most efficient code using int32_t or uint32_t for local
>> variables.
>
> Hi David,
>
> Thanks! I added -Wall -Wextra and I get a warning now :-)
>
>> The big question is, why are you using "uint8_t" at all here?
> Company policy.

Your company policy is /bad/. If you have any influence on it, complain.

Using uint8_t here makes your code incorrect - and it is most certainly
not an appropriate way to make sure you don't access the array out of
bounds.

>
>> Basically, your loop is like asking to run something every hour until 13 o'clock.
> I like the analogy :-)
>
>> you must write "int main(void)" - that's the usual form for embedded programming.
> Sorry - typed in haste ;-)
>
> One correction : Eclair does give a warning now, too.
>
> Thanks for your response,
>
> Cheers,
> Howerd
>

Re: uint8_t overflow in for ... loop

<smbff0$a59$3@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: uint8_t overflow in for ... loop
Date: Mon, 8 Nov 2021 16:24:16 +0100
Organization: A noiseless patient Spider
Lines: 17
Message-ID: <smbff0$a59$3@dont-email.me>
References: <704c07ac-5bf1-4256-a82c-61088d1e00aan@googlegroups.com>
<smb6us$840$1@dont-email.me> <861r3q21lq.fsf@levado.to>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 8 Nov 2021 15:24:16 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="8d4c8ed7efb99dba549c7b1d68bd2d39";
logging-data="10409"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/CAVQ/azI3bn3QqxNNAFJMvzHttBsf55E="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:rTDuNxhR5h/yTexmhkDXSuO+SoQ=
In-Reply-To: <861r3q21lq.fsf@levado.to>
Content-Language: en-GB
 by: David Brown - Mon, 8 Nov 2021 15:24 UTC

On 08/11/2021 14:55, Meredith Montgomery wrote:
> David Brown <david.brown@hesbynett.no> writes:
>
> [...]
>
>> The uint8_t variable counts up to 255, then wraps to 0 - overflow is
>> always wrapping for unsigned types (unlike for signed types).
>
> Technically the word is not ``overflow''.
>

Yes - you are technically correct. But at the the level of discussion
that is most suitable for a thread like this, it is a useful term to
describe what is happening. I don't think quoting the standard will
help the OP.

Re: uint8_t overflow in for ... loop

<smbfmg$egt$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: uint8_t overflow in for ... loop
Date: Mon, 8 Nov 2021 16:28:16 +0100
Organization: A noiseless patient Spider
Lines: 21
Message-ID: <smbfmg$egt$1@dont-email.me>
References: <704c07ac-5bf1-4256-a82c-61088d1e00aan@googlegroups.com>
<smb6us$840$1@dont-email.me> <smbcop$1jkt$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 8 Nov 2021 15:28:17 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="8d4c8ed7efb99dba549c7b1d68bd2d39";
logging-data="14877"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/GuK+Mfr0HsZ3mJ7RHt6gP3t0mNif/wSI="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:IjTj/BHhfXCq1ENG1NACu1YbJcQ=
In-Reply-To: <smbcop$1jkt$1@gioia.aioe.org>
Content-Language: en-GB
 by: David Brown - Mon, 8 Nov 2021 15:28 UTC

On 08/11/2021 15:38, Manfred wrote:
> On 11/8/2021 1:59 PM, David Brown wrote:
>>
>> The big question is, why are you using "uint8_t" at all here?  If you
>> were programming an AVR, I could understand it.  But for the ARM you
>> generally get most efficient code using int32_t or uint32_t for local
>> variables.
>>
>
> Or just a plain '[unsigned] int', which defaults to the machine word,
> and is meant to be the general purpose integer type that is optimized
> for performance in the absence of specific size requirements.
> The standard guarantees that 'int' and 'unsigned int' are both at least
> 16 bit in size.
>

He is doing embedded programming, and it is very common for such code to
be explicit about sizes. Anyone who uses "uint8_t" as a local counter
is likely to prefer "int32_t" to "int" in many cases. But of course
both "int" and "unsigned int" would have been suitable, as well as less
commonly used types such as "(u)int_fast16_t".

Re: uint8_t overflow in for ... loop

<5eea9cba-ef10-4c9b-b013-59f39c64bc24n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:620a:103c:: with SMTP id a28mr198640qkk.271.1636387758894;
Mon, 08 Nov 2021 08:09:18 -0800 (PST)
X-Received: by 2002:a05:620a:141b:: with SMTP id d27mr184273qkj.233.1636387758751;
Mon, 08 Nov 2021 08:09:18 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.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: Mon, 8 Nov 2021 08:09:18 -0800 (PST)
In-Reply-To: <0d0a7819-ceea-42d3-a710-1cd4d9f4fd98n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=84.50.190.130; posting-account=pysjKgkAAACLegAdYDFznkqjgx_7vlUK
NNTP-Posting-Host: 84.50.190.130
References: <704c07ac-5bf1-4256-a82c-61088d1e00aan@googlegroups.com>
<smb1a5$r7v$1@dont-email.me> <4c160b19-cf27-4fee-b76f-65067ef4c573n@googlegroups.com>
<63576f6c-0f43-42ec-bd05-7628ae306db4n@googlegroups.com> <0d0a7819-ceea-42d3-a710-1cd4d9f4fd98n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <5eea9cba-ef10-4c9b-b013-59f39c64bc24n@googlegroups.com>
Subject: Re: uint8_t overflow in for ... loop
From: oot...@hot.ee (Öö Tiib)
Injection-Date: Mon, 08 Nov 2021 16:09:18 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 107
 by: Öö Tiib - Mon, 8 Nov 2021 16:09 UTC

On Monday, 8 November 2021 at 15:03:31 UTC+2, how...@gmail.com wrote:
> On Monday, November 8, 2021 at 1:17:39 PM UTC+1, Thiago Adams wrote:
> > On Monday, November 8, 2021 at 9:06:06 AM UTC-3, how...@gmail.com wrote:
> > > On Monday, November 8, 2021 at 12:22:59 PM UTC+1, Bart wrote:
> > > > On 08/11/2021 11:11, Howerd Oakford wrote:
> > > > > Hi C-folks,
> > > > >
> > > > > Is the code listed below valid C?
> > > > > Specifically, is "i < 256" valid?
> > > > >
> > > > > I ask this because i is an 8 bit unsigned integer, and I would like to know if it is legal to compare it with a value that is outside the range of i?
> > > > >
> > > > > Normally, I expect a for ... loop to count up to the value in the middle section, but in the case below it creates an infinite loop.
> > > > >
> > > > > I tried it out on GCC ARM, GCC ARM cross compiler using Visual Studio Code, and MS Visual Studio - all three run for ever.
> > > > >
> > > > > None of the compilers gave a warning...
> > > > >
> > > > > Does this phenomenon have a name?
> > > > >
> > > > > Thanks in advance,
> > > > > Howerd
> > > > >
> > > > > #include <stdio.h> /* printf */
> > > > > #include <stdlib.h>
> > > > > #include <stdint.h>
> > > > >
> > > > > int main()
> > > > > {
> > > > > uint8_t i;
> > > > >
> > > > > // below is an infinite loop :
> > > > > for (i = 0; i < 256; i++)
> > > > > {
> > > > > if ((i % 16) == 0) printf("\n");
> > > > > printf("%i ", i);
> > > > > }
> > > > > }
> > > > >
> > > > The experts will shortly be along to tell you what's what.
> > > >
> > > > But is there any reason to define i as an 8-bit value rather than an
> > > > ordinary int?
> > > >
> > > > It causes problems here as it cannot represent values above 255, so will
> > > > ALWAYS be less than 256. The loop cannot terminate, as written.
> > > >
> > > > Just use an int.
> > > Hi Bart,
> > >
> > > Thanks for your quick response.
> > >
> > > Yes - the fix is easy - use a bigger integer, or a smaller array :-)
> > > But the real issues here are :
> > > 1. that this is a disastrous bug in code that I wrote, and
> > > 2. that was not detected by the compiler or any Linting tools that we are using (Understand and Eclair).
> > >
> > > The actual code is parsing a file one line at a time. I allocated a 256 byte array for the line, and a uint8_t for the pointer.
> > > I was implicitly using the wraparound from 255 to 0 to make sure that the pointer never strays outside the array.
> > >
> > > To find that if we receive a file with 256 or more bytes on a line it will cause an infinite loop crash is bad enough, but that this is not noticed by any of the tools we are using is simply astonishing. A colleague spotted this by doing a very thorough unit test on the code.
> > >
> > > Presumably this is legal code - I await some expert judgements on this ...
> > This kind of bug is easily detected by lint programs.
> >
> > I also tried in clang:
> >
> > warning: result of comparison of constant 256 with expression of type
> > 'uint8_t' (aka 'unsigned char') is always true [-Wtautological-constant-out-of-range-compare]
> Hi Thiago,
>
> Thanks - I think we will have to take a look at clang!!!
>
> Maybe we have some settings wrong in Understand and Eclair...
>
> Does the warning from clang mean that the code is "illegal", or just "not recommended"?

It is not illegal to have conditions that are always true (or always false).. The
likelihood that it is not what programmer did want to write is still high.
If it was deliberate then why programmer did not simply write 1 (or 0)
instead of more complex expression that is also always true? That is why
tools warn.

> In this case, the code is seriously wrong - causing a system crash if a file contains a single long line.

C compilers are only on very rare cases outright required to stop
translating the code. So it is mostly about setting those to give
more diagnostics.

Re: uint8_t overflow in for ... loop

<9ffd4534-2379-4643-9cae-8f2299b5976bn@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ac8:488b:: with SMTP id i11mr1361170qtq.208.1636393309406;
Mon, 08 Nov 2021 09:41:49 -0800 (PST)
X-Received: by 2002:a05:620a:424d:: with SMTP id w13mr758099qko.308.1636393309118;
Mon, 08 Nov 2021 09:41:49 -0800 (PST)
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Mon, 8 Nov 2021 09:41:48 -0800 (PST)
In-Reply-To: <smbf9n$a59$2@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=2a01:c22:bd89:ce00:51c2:ed23:8a4:ceac;
posting-account=o1zNBwoAAABP2KiH5g6nPQp_sYWidVdt
NNTP-Posting-Host: 2a01:c22:bd89:ce00:51c2:ed23:8a4:ceac
References: <704c07ac-5bf1-4256-a82c-61088d1e00aan@googlegroups.com>
<smb6us$840$1@dont-email.me> <4d3da41b-483d-4f1f-b169-b9bb76b23ca0n@googlegroups.com>
<smbf9n$a59$2@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <9ffd4534-2379-4643-9cae-8f2299b5976bn@googlegroups.com>
Subject: Re: uint8_t overflow in for ... loop
From: howe...@gmail.com (Howerd Oakford)
Injection-Date: Mon, 08 Nov 2021 17:41:49 +0000
Content-Type: text/plain; charset="UTF-8"
 by: Howerd Oakford - Mon, 8 Nov 2021 17:41 UTC

On Monday, November 8, 2021 at 4:21:39 PM UTC+1, David Brown wrote:
> On 08/11/2021 14:19, Howerd Oakford wrote:
> > On Monday, November 8, 2021 at 1:59:20 PM UTC+1, David Brown wrote:
> >> On 08/11/2021 12:11, Howerd Oakford wrote:
> >>> Hi C-folks,
> >>>
> >>> Is the code listed below valid C?
> >>> Specifically, is "i < 256" valid?
> >> It is valid, it is just not helpful. A uint8_t variable will /always/
> >> be less than 256, so the check is always "true".
> >>>
> >>> I ask this because i is an 8 bit unsigned integer, and I would like to know if it is legal to compare it with a value that is outside the range of i?
> >>>
> >>> Normally, I expect a for ... loop to count up to the value in the middle section, but in the case below it creates an infinite loop.
> >>>
> >> The uint8_t variable counts up to 255, then wraps to 0 - overflow is
> >> always wrapping for unsigned types (unlike for signed types).
> >> Basically, you loop is like asking to run something every hour until 13
> >> o'clock.
> >>> I tried it out on GCC ARM, GCC ARM cross compiler using Visual Studio Code, and MS Visual Studio - all three run for ever.
> >>>
> >> Good.
> >>> None of the compilers gave a warning...
> >>>
> >> gcc will give you a warning if you ask it. The flag is "-Wtype-limits",
> >> and it is enabled with "-Wextra". It's a good idea to have "-Wall
> >> -Wextra" enabled while you are getting the hang of things, then perhaps
> >> disable some warnings if you find they conflict with your style and give
> >> false positives.
> >>> Does this phenomenon have a name?
> >>>
> >>> Thanks in advance,
> >>> Howerd
> >>>
> >>> #include <stdio.h> /* printf */
> >>> #include <stdlib.h>
> >>> #include <stdint.h>
> >>>
> >>> int main()
> >> In c.l.c., we have a long tradition of complaining when people declare
> >> "main" incorrectly. In C (until C23 comes out), if you want to declare
> >> "main" with no parameters, you must write "int main(void)" - that's the
> >> usual form for embedded programming.
> >>> {
> >>> uint8_t i;
> >>>
> >>> // below is an infinite loop :
> >>> for (i = 0; i < 256; i++)
> >> It's generally neater to put the variable declaration in the "for" :
> >>
> >> for (uint8_t i = 0; i < 256; i++) {
> >>
> >> Of course, you'll want to change that type - or the limit!
> >>> {
> >>> if ((i % 16) == 0) printf("\n");
> >>> printf("%i ", i);
> >>> }
> >>> }
> >>>
> >> The big question is, why are you using "uint8_t" at all here? If you
> >> were programming an AVR, I could understand it. But for the ARM you
> >> generally get most efficient code using int32_t or uint32_t for local
> >> variables.
> >
> > Hi David,
> >
> > Thanks! I added -Wall -Wextra and I get a warning now :-)
> >
> >> The big question is, why are you using "uint8_t" at all here?
> > Company policy.
> Your company policy is /bad/. If you have any influence on it, complain.
>
> Using uint8_t here makes your code incorrect - and it is most certainly
> not an appropriate way to make sure you don't access the array out of
> bounds.
> >
> >> Basically, your loop is like asking to run something every hour until 13 o'clock.
> > I like the analogy :-)
> >
> >> you must write "int main(void)" - that's the usual form for embedded programming.
> > Sorry - typed in haste ;-)
> >
> > One correction : Eclair does give a warning now, too.
> >
> > Thanks for your response,
> >
> > Cheers,
> > Howerd
> >

Hi David,

> Your company policy is /bad/. If you have any influence on it, complain.
if (unit8_t influence > 256 ) complain(); // ;-)

> Using uint8_t here makes your code incorrect
Yes, it does :-(

I have enabled warnings, and will pay attention to them more than ever now.

Thanks to you and everyone else on c.l.c for your responses :-)

Cheers,
Howerd

Re: uint8_t overflow in for ... loop

<86a6iebiqg.fsf@linuxsc.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: uint8_t overflow in for ... loop
Date: Mon, 08 Nov 2021 10:33:11 -0800
Organization: A noiseless patient Spider
Lines: 48
Message-ID: <86a6iebiqg.fsf@linuxsc.com>
References: <704c07ac-5bf1-4256-a82c-61088d1e00aan@googlegroups.com> <smb6us$840$1@dont-email.me> <861r3q21lq.fsf@levado.to> <JGaiJ.21677$Gco3.19158@fx01.iad> <smbef2$fa9$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="774f026e5366f2651c3ce55fa394a774";
logging-data="8872"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19rDe7V1wqrQb3SOlNSBRr0GkiF0Q5gEHU="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:u/S1XHpQcFFhrIvxxU79kQBo/pc=
sha1:u0tACkQNoeza3WJCWU/4vCEntM4=
 by: Tim Rentsch - Mon, 8 Nov 2021 18:33 UTC

Manfred <noname@add.invalid> writes:

> On 11/8/2021 3:41 PM, Scott Lurndal wrote:
>
>> Meredith Montgomery <mmontgomery@levado.to> writes:
>>
>>> David Brown <david.brown@hesbynett.no> writes:
>>>
>>> [...]
>>>
>>>> The uint8_t variable counts up to 255, then wraps to 0 - overflow is
>>>> always wrapping for unsigned types (unlike for signed types).
>>>
>>> Technically the word is not ``overflow''.
>>
>> Technically, the processor sets the "overflow" flag. If that's not
>> an overflow, I'm not sure what it.
>
> The quote from the standard, that you stripped, is accurate:
>
>> A computation involving unsigned operands can never overflow, because a
>> result that cannot be represented by the resulting unsigned integer type
>> is reduced modulo the number that is one greater than the largest value
>> that can be represented by the resulting unsigned integer type. [...]

(That exact text appears in C90 section 6.1.2.5, paragraph 5.)

> BTW is is almost identical in n2596, 6.2.5 clause 9.

The C90 text was revised slightly in C99 and is unchanged up to n2596.

> If it "can never overflow", it's a statement that is clear enough.

Note that the original code doesn't have any computations
involving unsigned operands, only signed operands. The code does
have /conversions/ from one integer type to another, but
conversions have their own rules, and no conversion between
integer types, either signed or unsigned or both, is "overflow"
in the sense that the C standard uses the term. Converting to
an unsigned type (from another integer type) is always exactly
defined by the C standard, depending only on the value being
converted and the implementation-defined width of the unsigned
type that is the target type of the conversion.

> I'd say the correct term is wrapping.

It's a common term and one that is often used, but I wouldn't
go so far as to call it "the correct" term.

Re: uint8_t overflow in for ... loop

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

  copy mid

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

  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: uint8_t overflow in for ... loop
Date: Mon, 08 Nov 2021 10:41:06 -0800
Organization: None to speak of
Lines: 23
Message-ID: <87bl2uxzgd.fsf@nosuchdomain.example.com>
References: <704c07ac-5bf1-4256-a82c-61088d1e00aan@googlegroups.com>
<smb6us$840$1@dont-email.me> <861r3q21lq.fsf@levado.to>
<JGaiJ.21677$Gco3.19158@fx01.iad>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="8f220a377d54f8dc9b1bb1b71322e466";
logging-data="10584"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/crvOKKs7TFVFV5EX3+DA5"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:iv3CzbituMpf4IcbLPGYOp+AkK4=
sha1:OkWRd4ZrAYpG73Z3tn37lyN8Eos=
 by: Keith Thompson - Mon, 8 Nov 2021 18:41 UTC

scott@slp53.sl.home (Scott Lurndal) writes:
> Meredith Montgomery <mmontgomery@levado.to> writes:
>>David Brown <david.brown@hesbynett.no> writes:
>>
>>[...]
>>
>>> The uint8_t variable counts up to 255, then wraps to 0 - overflow is
>>> always wrapping for unsigned types (unlike for signed types).
>>
>>Technically the word is not ``overflow''.
>
> Technically, the processor sets the "overflow" flag. If that's not
> an overflow, I'm not sure what it.

Which processor? Who says it even has an overflow flag?

In this context, "overflow" is determined by the C standard, not by the
CPU.

--
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: uint8_t overflow in for ... loop

<smbrft$18rg$1@gioia.aioe.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!Puiiztk9lHEEQC0y3uUjRA.user.46.165.242.75.POSTED!not-for-mail
From: non...@add.invalid (Manfred)
Newsgroups: comp.lang.c
Subject: Re: uint8_t overflow in for ... loop
Date: Mon, 8 Nov 2021 19:49:32 +0100
Organization: Aioe.org NNTP Server
Message-ID: <smbrft$18rg$1@gioia.aioe.org>
References: <704c07ac-5bf1-4256-a82c-61088d1e00aan@googlegroups.com>
<smb6us$840$1@dont-email.me> <861r3q21lq.fsf@levado.to>
<JGaiJ.21677$Gco3.19158@fx01.iad> <smbef2$fa9$1@gioia.aioe.org>
<86a6iebiqg.fsf@linuxsc.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="41840"; posting-host="Puiiztk9lHEEQC0y3uUjRA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.0
Content-Language: en-US
X-Notice: Filtered by postfilter v. 0.9.2
 by: Manfred - Mon, 8 Nov 2021 18:49 UTC

On 11/8/2021 7:33 PM, Tim Rentsch wrote:
> Manfred <noname@add.invalid> writes:

>
>> If it "can never overflow", it's a statement that is clear enough.
>
> Note that the original code doesn't have any computations
> involving unsigned operands, only signed operands.

On 08/11/2021 12:11, Howerd Oakford wrote:

> uint8_t i;

The code does
> have /conversions/ from one integer type to another, but
> conversions have their own rules, and no conversion between
> integer types, either signed or unsigned or both, is "overflow"
> in the sense that the C standard uses the term. Converting to
> an unsigned type (from another integer type) is always exactly
> defined by the C standard, depending only on the value being
> converted and the implementation-defined width of the unsigned
> type that is the target type of the conversion.
>

Re: uint8_t overflow in for ... loop

<smbsb5$1lo0$1@gioia.aioe.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!5Eb5Kjev2xuaeX7DMmY97A.user.46.165.242.75.POSTED!not-for-mail
From: mess...@bottle.org (Guillaume)
Newsgroups: comp.lang.c
Subject: Re: uint8_t overflow in for ... loop
Date: Mon, 8 Nov 2021 20:04:03 +0100
Organization: Aioe.org NNTP Server
Message-ID: <smbsb5$1lo0$1@gioia.aioe.org>
References: <704c07ac-5bf1-4256-a82c-61088d1e00aan@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="55040"; posting-host="5Eb5Kjev2xuaeX7DMmY97A.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.0
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: fr
 by: Guillaume - Mon, 8 Nov 2021 19:04 UTC

Le 08/11/2021 à 12:11, Howerd Oakford a écrit :
> Is the code listed below valid C?
> Specifically, is "i < 256" valid?

It is.

> I ask this because i is an 8 bit unsigned integer, and I would like to know if it is legal to compare it with a value that is outside the range of i?

In C, it is.
You get no warning because C implicitly does "integer promotion". Thus
here i is promoted to an int in the 'i < 256' expression. The expression
is perfectly "legal". But i itself is in the [0..255] range, and C rules
are such that arithmetics on i can be considered modulo 256.

You can consider that it's the equivalent of:

int i;
for (i = 0; (i % 256) < 256; i++) { ... }

or, to be more exact:
for (i = 0; i < 256; i = (i + 1) % 256) { ... }

Neither forms will give a compiler warning either.
And of course, it's an infinite loop.

Yes C arithmetics is full of "traps", so you should definitely learn
about integer promotion and integer arithmetics in C.

So for such a loop, you can either use a wider int type for i, or, if i
should absolutely be a uint8_t for some reason, you have no other choice
than doing the following:

uint8_t i;
for (i = 0; ; i++)
{
if ((i % 16) == 0) printf("\n");
printf("%i ", i);

if (i == 255) break;
}

As you can see, the condition in the for statement is omitted here, as
no expression using 'i' can work properly as one.

Re: uint8_t overflow in for ... loop

<smbshp$ml9$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: uint8_t overflow in for ... loop
Date: Mon, 8 Nov 2021 20:07:37 +0100
Organization: A noiseless patient Spider
Lines: 19
Message-ID: <smbshp$ml9$1@dont-email.me>
References: <704c07ac-5bf1-4256-a82c-61088d1e00aan@googlegroups.com>
<smb6us$840$1@dont-email.me> <861r3q21lq.fsf@levado.to>
<JGaiJ.21677$Gco3.19158@fx01.iad>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 8 Nov 2021 19:07:37 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="f53b7c49aee267beb3dcbd0afb842ca5";
logging-data="23209"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19bsyavbxAR+QCspEEDeQwAcEkAVpOGxYs="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.0
Cancel-Lock: sha1:cMhN3RoEh9jfBCKXnqwkG1tvn0s=
In-Reply-To: <JGaiJ.21677$Gco3.19158@fx01.iad>
Content-Language: de-DE
 by: Bonita Montero - Mon, 8 Nov 2021 19:07 UTC

Am 08.11.2021 um 15:41 schrieb Scott Lurndal:
> Meredith Montgomery <mmontgomery@levado.to> writes:
>> David Brown <david.brown@hesbynett.no> writes:
>>
>> [...]
>>
>>> The uint8_t variable counts up to 255, then wraps to 0 - overflow is
>>> always wrapping for unsigned types (unlike for signed types).
>>
>> Technically the word is not ``overflow''.
>>
>
> Technically, the processor sets the "overflow" flag.

No, it sets the carry-flag. The overflow-flag is set when
the sign changes without the carry being set, i.e. the
value won't fit in the destination-width.

Re: uint8_t overflow in for ... loop

<smbsqf$1t46$1@gioia.aioe.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!5Eb5Kjev2xuaeX7DMmY97A.user.46.165.242.75.POSTED!not-for-mail
From: mess...@bottle.org (Guillaume)
Newsgroups: comp.lang.c
Subject: Re: uint8_t overflow in for ... loop
Date: Mon, 8 Nov 2021 20:12:13 +0100
Organization: Aioe.org NNTP Server
Message-ID: <smbsqf$1t46$1@gioia.aioe.org>
References: <704c07ac-5bf1-4256-a82c-61088d1e00aan@googlegroups.com>
<smb1a5$r7v$1@dont-email.me>
<4c160b19-cf27-4fee-b76f-65067ef4c573n@googlegroups.com>
<63576f6c-0f43-42ec-bd05-7628ae306db4n@googlegroups.com>
<0d0a7819-ceea-42d3-a710-1cd4d9f4fd98n@googlegroups.com>
<878rxy22a8.fsf@bsb.me.uk>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="62598"; posting-host="5Eb5Kjev2xuaeX7DMmY97A.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.0
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: fr
 by: Guillaume - Mon, 8 Nov 2021 19:12 UTC

Le 08/11/2021 à 14:41, Ben Bacarisse a écrit :
> gcc warns as well, but you have to ask (it's one of the -Wextra
> warnings).

Ah, you're right. I had tried only with -Wall.

Interestingly, it will give a warning if 'i' is uint8_t here, but not
for the other 'equivalent' forms I gave in my other post using a '%'
operator.

so for:

int i;
for (i = 0; (i % 256) < 256; i++) {}

GCC doesn't spot any problem. I guess analyzing expressions is a bit
much to ask here.

But as I said, this is still all valid C. The warning here is for
convenience.

Re: uint8_t overflow in for ... loop

<877ddixxqd.fsf@nosuchdomain.example.com>

  copy mid

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

  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: uint8_t overflow in for ... loop
Date: Mon, 08 Nov 2021 11:18:18 -0800
Organization: None to speak of
Lines: 56
Message-ID: <877ddixxqd.fsf@nosuchdomain.example.com>
References: <704c07ac-5bf1-4256-a82c-61088d1e00aan@googlegroups.com>
<smb6us$840$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="8f220a377d54f8dc9b1bb1b71322e466";
logging-data="30573"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+dxjgPs+srxewMyzDuj2uR"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:ZigncyVhpJWIitw40n3roB3lxGQ=
sha1:hh8eJcJX76BH7AiTrtgJVaZkfaE=
 by: Keith Thompson - Mon, 8 Nov 2021 19:18 UTC

David Brown <david.brown@hesbynett.no> writes:
> On 08/11/2021 12:11, Howerd Oakford wrote:
>> Hi C-folks,
>>
>> Is the code listed below valid C?
>> Specifically, is "i < 256" valid?
>
> It is valid, it is just not helpful. A uint8_t variable will /always/
> be less than 256, so the check is always "true".
>
>>
>> I ask this because i is an 8 bit unsigned integer, and I would like
>> to know if it is legal to compare it with a value that is outside the
>> range of i?
>>
>> Normally, I expect a for ... loop to count up to the value in the
>> middle section, but in the case below it creates an infinite loop.
>
> The uint8_t variable counts up to 255, then wraps to 0 - overflow is
> always wrapping for unsigned types (unlike for signed types).
> Basically, you loop is like asking to run something every hour until 13
> o'clock.

That's essentially correct, but it's a bit more complicated for integer
types narrower than int and unsigned int.

You have:

uint8_t i;
...
i++

C has no arithmetic operators for types narrower than int and unsigned
int (and uint8_t is definitely narrower than int, since int is at least
16 bits).

The expression `i++` is equivalent to `i = i + 1`. In `i + 1`, the
value of i is promoted to int. The addition is then down on two
operands of type int, and the result is assigned to i. Assigning an int
value to a uint8_t object requires an implicit conversion. If the
converted value can be represented in the target type (i.e., it's in the
range 0..255), the conversion retains the same value. Otherwise, since
the target type is an unsigned integer type, the conversion converts 256
to 0. (It gets more complicated if the target type is a signed type
like int8_t).

The final result is the same as if it were operating directly on 8-bit
values -- and the compiler might well generate code that does that if
it's more efficient.

[...]

--
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: uint8_t overflow in for ... loop

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

  copy mid

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

  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: uint8_t overflow in for ... loop
Date: Mon, 08 Nov 2021 11:19:52 -0800
Organization: None to speak of
Lines: 14
Message-ID: <87zgqewj3b.fsf@nosuchdomain.example.com>
References: <704c07ac-5bf1-4256-a82c-61088d1e00aan@googlegroups.com>
<smb6us$840$1@dont-email.me>
<4d3da41b-483d-4f1f-b169-b9bb76b23ca0n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="8f220a377d54f8dc9b1bb1b71322e466";
logging-data="30573"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1//NWp7HkmsYxGxFsPAYgdG"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:jH0JhfaSWBTBAgTtEIexvYid+cM=
sha1:75gnuYynYGrwNhCiiFgAPvCvFB0=
 by: Keith Thompson - Mon, 8 Nov 2021 19:19 UTC

Howerd Oakford <howerdo@gmail.com> writes:
> On Monday, November 8, 2021 at 1:59:20 PM UTC+1, David Brown wrote:
[...]
>> The big question is, why are you using "uint8_t" at all here?
> Company policy.

Can you elaborate on what the policy says? I presume it doesn't require
you to write incorrect code. I also presume it doesn't require you to
use 8-bit types for loop control variables in all cases.

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

Pages:12
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor