diff options
author | Pedro Alves <palves@redhat.com> | 2020-01-29 12:53:55 -0500 |
---|---|---|
committer | Sergio Durigan Junior <sergiodj@redhat.com> | 2020-01-29 15:23:37 -0500 |
commit | c47f70e2ce7b347aadbde873aae6c2df92c42180 (patch) | |
tree | c33c1dd5f281fc1eafd9382f04953fbbeca6c431 /gdb/infcmd.c | |
parent | fc1ca146f76eea0b76b1392ea1a7aa8fb0742490 (diff) | |
download | gdb-c47f70e2ce7b347aadbde873aae6c2df92c42180.zip gdb-c47f70e2ce7b347aadbde873aae6c2df92c42180.tar.gz gdb-c47f70e2ce7b347aadbde873aae6c2df92c42180.tar.bz2 |
Fix -Werror-stringop error on infcmd.c:construct_inferior_arguments
While testing a GCC 10 build of our git HEAD, Sergio noticed an error
triggered by -Werror-stringop on
infcmd.c:construct_inferior_arguments. One of the things the function
does is calculate the length of the string that will hold the
inferior's arguments. GCC warns us that 'length' can be 0, which can
lead to undesired behaviour:
../../gdb/infcmd.c: In function 'char* construct_inferior_arguments(int, char**)':
../../gdb/infcmd.c:369:17: error: writing 1 byte into a region of size 0 [-Werror=stringop-overflow=]
369 | result[0] = '\0';
| ~~~~~~~~~~^~~~~~
../../gdb/infcmd.c:368:33: note: at offset 0 to an object with size 0 allocated by 'xmalloc' here
368 | result = (char *) xmalloc (length);
| ~~~~~~~~^~~~~~~~
The solution here is to assert that 'argc' is greater than 0 on entry,
which makes GCC understand that the loops always run at least once,
and thus 'length' is always > 0.
Tested by rebuilding.
gdb/ChangeLog:
2020-01-29 Pedro Alves <palves@redhat.com>
Sergio Durigan Junior <sergiodj@redhat.com>
* infcmd.c (construct_inferior_arguments): Assert that
'argc' is greater than 0.
Change-Id: Ide8407cbedcb4921de1843a6a15bbcb7676c7d26
Diffstat (limited to 'gdb/infcmd.c')
-rw-r--r-- | gdb/infcmd.c | 5 |
1 files changed, 5 insertions, 0 deletions
diff --git a/gdb/infcmd.c b/gdb/infcmd.c index b44adca..62890bd 100644 --- a/gdb/infcmd.c +++ b/gdb/infcmd.c @@ -268,6 +268,11 @@ construct_inferior_arguments (int argc, char **argv) { char *result; + /* ARGC should always be at least 1, but we double check this + here. This is also needed to silence -Werror-stringop + warnings. */ + gdb_assert (argc > 0); + if (startup_with_shell) { #ifdef __MINGW32__ |