public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/115570] New: array subscript 'long unsigned int[0]' is partly outside array bounds of 'unsigned char[4]'
@ 2024-06-21  0:15 Curt.Blank at curtronics dot com
  2024-06-21  0:22 ` [Bug middle-end/115570] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Curt.Blank at curtronics dot com @ 2024-06-21  0:15 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115570

            Bug ID: 115570
           Summary: array subscript 'long unsigned int[0]' is partly
                    outside array bounds of 'unsigned char[4]'
           Product: gcc
           Version: 13.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: Curt.Blank at curtronics dot com
  Target Milestone: ---

I just ran into a compile error on code that has been just fine for 18 years.
The only reason I'm compiling it is just to be sure on a new computer/Linux
install.

With gcc 4.8.3 I do not get the warnings, with gcc 13.3.0 I get the warnings
shown below the code below. The char string being converted is hex, data
returned from a solar inverter.

And like I said it's been fine and I suspect the program will run fine just
cannot fully test it yet until data collect is moved to this new server.


*--------------------------------------------------------------------------
    szCvrtLong
    Converts a 4 char string to a long.
----------------------------------------------------------------------------*/
unsigned long szCvrtLong(char *Buffer)
{
    unsigned char cValue[4];
    unsigned long *value = 0;

    if (! bSwapEndian) {
        cValue[0] = Buffer[aParam4] & 0xff;
        cValue[1] = Buffer[aParam3] & 0xff;
        cValue[2] = Buffer[aParam2] & 0xff;
        cValue[3] = Buffer[aParam1] & 0xff;
    } else {
        cValue[0] = Buffer[aParam1] & 0xff;
        cValue[1] = Buffer[aParam2] & 0xff;
        cValue[2] = Buffer[aParam3] & 0xff;
        cValue[3] = Buffer[aParam4] & 0xff;
    }

    value = (unsigned long *)cValue;
    if (bVerbose) fprintf(stderr, "szCvrtLong   %12lu
0x%02x%02x%02x%02x\n",*value &
0xffffffff,cValue[3],cValue[2],cValue[1],cValue[0]);

    return(*value & 0xffffffff);
}

-----------

comm.c: In function 'szCvrtLong.constprop':
comm.c:1906:77: warning: array subscript 'long unsigned int[0]' is partly
outside array bounds of 'unsigned char[4]' [-Warray-bounds=]
 1906 |     if (bVerbose) fprintf(stderr, "szCvrtLong   %12lu
0x%02x%02x%02x%02x\n",*value &
0xffffffff,cValue[3],cValue[2],cValue[1],cValue[0]);
      |                                                                        
    ^~~~~~
comm.c:1890:19: note: object 'cValue' of size 4
 1890 |     unsigned char cValue[4];
      |                   ^~~~~~
comm.c:1908:12: warning: array subscript 'long unsigned int[0]' is partly
outside array bounds of 'unsigned char[4]' [-Warray-bounds=]
 1908 |     return(*value & 0xffffffff);
      |            ^~~~~~
comm.c:1890:19: note: object 'cValue' of size 4
 1890 |     unsigned char cValue[4];
      |                   ^~~~~~

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug middle-end/115570] array subscript 'long unsigned int[0]' is partly outside array bounds of 'unsigned char[4]'
  2024-06-21  0:15 [Bug c/115570] New: array subscript 'long unsigned int[0]' is partly outside array bounds of 'unsigned char[4]' Curt.Blank at curtronics dot com
@ 2024-06-21  0:22 ` pinskia at gcc dot gnu.org
  2024-06-21  0:23 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-21  0:22 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115570

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |INVALID
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
The warning is correct.
sizeof(long) on x86_64 is 8 rather than 4 that it was on i686.
So when you do this:

    unsigned char cValue[4];
    unsigned long *value = 0;
    value = (unsigned long *)cValue;

*value & 0xffffffff

You get undefined behavior because you are reading 8 bytes from a 4 byte array.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug middle-end/115570] array subscript 'long unsigned int[0]' is partly outside array bounds of 'unsigned char[4]'
  2024-06-21  0:15 [Bug c/115570] New: array subscript 'long unsigned int[0]' is partly outside array bounds of 'unsigned char[4]' Curt.Blank at curtronics dot com
  2024-06-21  0:22 ` [Bug middle-end/115570] " pinskia at gcc dot gnu.org
@ 2024-06-21  0:23 ` pinskia at gcc dot gnu.org
  2024-06-21  0:25 ` pinskia at gcc dot gnu.org
  2024-06-21  0:31 ` Curt.Blank at curtronics dot com
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-21  0:23 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115570

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note this code also violates C/C++ aliasing rules but that is a different
issue.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug middle-end/115570] array subscript 'long unsigned int[0]' is partly outside array bounds of 'unsigned char[4]'
  2024-06-21  0:15 [Bug c/115570] New: array subscript 'long unsigned int[0]' is partly outside array bounds of 'unsigned char[4]' Curt.Blank at curtronics dot com
  2024-06-21  0:22 ` [Bug middle-end/115570] " pinskia at gcc dot gnu.org
  2024-06-21  0:23 ` pinskia at gcc dot gnu.org
@ 2024-06-21  0:25 ` pinskia at gcc dot gnu.org
  2024-06-21  0:31 ` Curt.Blank at curtronics dot com
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-21  0:25 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115570

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note there is also an alignment requirement that you violate with the
assignment and access of the char array too.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug middle-end/115570] array subscript 'long unsigned int[0]' is partly outside array bounds of 'unsigned char[4]'
  2024-06-21  0:15 [Bug c/115570] New: array subscript 'long unsigned int[0]' is partly outside array bounds of 'unsigned char[4]' Curt.Blank at curtronics dot com
                   ` (2 preceding siblings ...)
  2024-06-21  0:25 ` pinskia at gcc dot gnu.org
@ 2024-06-21  0:31 ` Curt.Blank at curtronics dot com
  3 siblings, 0 replies; 5+ messages in thread
From: Curt.Blank at curtronics dot com @ 2024-06-21  0:31 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115570

--- Comment #4 from Curtis J Blank <Curt.Blank at curtronics dot com> ---
Thank you the information.

So I should use unsigned int now on x86_64?

Where can I read more on the rule violations and what would be the proper so do
this?

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-06-21  0:31 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-21  0:15 [Bug c/115570] New: array subscript 'long unsigned int[0]' is partly outside array bounds of 'unsigned char[4]' Curt.Blank at curtronics dot com
2024-06-21  0:22 ` [Bug middle-end/115570] " pinskia at gcc dot gnu.org
2024-06-21  0:23 ` pinskia at gcc dot gnu.org
2024-06-21  0:25 ` pinskia at gcc dot gnu.org
2024-06-21  0:31 ` Curt.Blank at curtronics dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).