Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compilation with GCC 11 fails #1077

Closed
Vascom opened this issue Dec 9, 2020 · 7 comments
Closed

Compilation with GCC 11 fails #1077

Vascom opened this issue Dec 9, 2020 · 7 comments

Comments

@Vascom
Copy link
Collaborator

Vascom commented Dec 9, 2020

I can't build stlink 1.6.1 with GCC 11 compiler. Fedora GNU/Linux Rawhide.
Errors are:

/builddir/build/BUILD/stlink-1.6.1/src/st-util/gdb-server.c:90:22: error: 'stlink_open_usb' accessing 64 bytes in a region of size 28 [-Werror=stringop-overflow=]
   90 |                 sl = stlink_open_usb(st->logging_level, st->reset, serialnumber, 0);
      |                      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/builddir/build/BUILD/stlink-1.6.1/src/st-util/gdb-server.c: In function 'serve':
/builddir/build/BUILD/stlink-1.6.1/src/st-util/gdb-server.c:90:22: note: referencing argument 3 of type 'char *'
In file included from /builddir/build/BUILD/stlink-1.6.1/include/stlink.h:297,
                 from /builddir/build/BUILD/stlink-1.6.1/src/st-util/gdb-server.c:27:
/builddir/build/BUILD/stlink-1.6.1/src/usb.h:71:15: note: in a call to function 'stlink_open_usb'
   71 |     stlink_t *stlink_open_usb(enum ugly_loglevel verbose, int reset, char serial[STLINK_SERIAL_MAX_SIZE], int freq);
      |               ^~~~~~~~~~~~~~~
In function 'do_connect',
    inlined from 'do_connect' at /builddir/build/BUILD/stlink-1.6.1/src/st-util/gdb-server.c:87:18,
    inlined from 'main' at /builddir/build/BUILD/stlink-1.6.1/src/st-util/gdb-server.c:212:10:
/builddir/build/BUILD/stlink-1.6.1/src/st-util/gdb-server.c:90:22: error: 'stlink_open_usb' accessing 64 bytes in a region of size 28 [-Werror=stringop-overflow=]
   90 |                 sl = stlink_open_usb(st->logging_level, st->reset, serialnumber, 0);
      |                      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Please help me correct it.

@Vascom
Copy link
Collaborator Author

Vascom commented Dec 9, 2020

May be STLINK_SERIAL_MAX_SIZE define should be set to 28?

@Ant-ON
Copy link
Collaborator

Ant-ON commented Dec 9, 2020

You may fix src/st-util/gdb-server.c:

static char serialnumber[28] = {0};

to

static char serialnumber[STLINK_SERIAL_MAX_SIZE] = {0};

@Vascom
Copy link
Collaborator Author

Vascom commented Dec 9, 2020

I prefer see fix in upstream.

@Ant-ON
Copy link
Collaborator

Ant-ON commented Dec 9, 2020

@Vascom I too. But to fix it in upstream, you need to check works of it.

ps You have created a issue description not in accordance with a template. @Nightwalker-87 may delete it. See #906

@Vascom
Copy link
Collaborator Author

Vascom commented Dec 9, 2020

Yes, it works.

Template not care about compiling problems.

@Nightwalker-87
Copy link
Member

The template is meant to be used and to be filled with content which also includes a description. So please consider this next time.

@JeffreyALaw
Copy link

You may fix src/st-util/gdb-server.c:

static char serialnumber[28] = {0};

to

static char serialnumber[STLINK_SERIAL_MAX_SIZE] = {0};

I nearly suggested that (I originally found the issue testing gcc-11 with Fedora). My concern with that was this loop in gdb-server.c:
for (size_t k = 0; j >= 0 && k < sizeof(serialnumber); ++k, j -= 2) {
char buffer[3] = {0};
memcpy(buffer, optarg + j, 2);
serialnumber[length - k] = (uint8_t)strtol(buffer, NULL, 16);

Which would be affected by changing the size of "serialnumber". Without knowing the intent or the code in general I didn't feel comfortable recommending changing the size of "serialnumber".

And to give folks a bit of background. gcc-11 has added diagnostics to detect out of bounds accesses for array arguments. In simplest terms if a function declares an argument as an array with a fixed length, then GCC assumes the entire array could potentially be referenced.

stlink_open_usb declares its 3rd argument as a 64 byte character array, yet "serialnumber" is just 28 bytes and GCC naturally warns because it thinks there's a potential out-of-bounds of the "serialnumber" argument by stlink_open_usb.

@Nightwalker-87 Nightwalker-87 moved this from To do to In progress in Release v1.7.0 Dec 10, 2020
@Nightwalker-87 Nightwalker-87 changed the title Build with GCC 11 fail Compilation with GCC 11 fails Dec 12, 2020
@Nightwalker-87 Nightwalker-87 self-assigned this Dec 12, 2020
Release v1.7.0 automation moved this from In progress to Done Dec 12, 2020
@stlink-org stlink-org locked as resolved and limited conversation to collaborators Mar 20, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
No open projects
Status: Done
Development

No branches or pull requests

4 participants