[4/4] main: Don't add int to string

Message ID 1498076108-29914-5-git-send-email-simon.marchi@ericsson.com
State New, archived
Headers

Commit Message

Simon Marchi June 21, 2017, 8:15 p.m. UTC
  clang shows this warning:

  /home/emaisin/src/binutils-gdb/gdb/main.c:227:56: error: adding 'int' to a string does not append to the string [-Werror,-Wstring-plus-int]
                char *tmp_sys_gdbinit = xstrdup (SYSTEM_GDBINIT + datadir_len);
                                                 ~~~~~~~~~~~~~~~^~~~~~~~~~~~~
  /home/emaisin/src/binutils-gdb/gdb/main.c:227:56: note: use array indexing to silence this warning
                char *tmp_sys_gdbinit = xstrdup (SYSTEM_GDBINIT + datadir_len);
                                                                ^
                                                 &              [            ]

It's quite easy to get rid of it by using &foo[len] instead of foo + len.
I think this warning is relevant to keep enabled, because it can be an
easy mistake to do.

This warning is already discussed here in GCC bugzilla:

  https://gcc.gnu.org/ml/gcc-patches/2017-06/msg00729.html

and a patch series for it was submitted very recently.

gdb/ChangeLog:

	* main.c (get_init_files): Replace "SYSTEM_GDBINIT +
	datadir_len" with "&SYSTEM_GDBINIT[datadir_len]".
---
 gdb/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
  

Comments

Sergio Durigan Junior June 21, 2017, 8:35 p.m. UTC | #1
On Wednesday, June 21 2017, Simon Marchi wrote:

> clang shows this warning:
>
>   /home/emaisin/src/binutils-gdb/gdb/main.c:227:56: error: adding 'int' to a string does not append to the string [-Werror,-Wstring-plus-int]
>                 char *tmp_sys_gdbinit = xstrdup (SYSTEM_GDBINIT + datadir_len);
>                                                  ~~~~~~~~~~~~~~~^~~~~~~~~~~~~
>   /home/emaisin/src/binutils-gdb/gdb/main.c:227:56: note: use array indexing to silence this warning
>                 char *tmp_sys_gdbinit = xstrdup (SYSTEM_GDBINIT + datadir_len);
>                                                                 ^
>                                                  &              [            ]
>
> It's quite easy to get rid of it by using &foo[len] instead of foo + len.
> I think this warning is relevant to keep enabled, because it can be an
> easy mistake to do.
>
> This warning is already discussed here in GCC bugzilla:
>
>   https://gcc.gnu.org/ml/gcc-patches/2017-06/msg00729.html
>
> and a patch series for it was submitted very recently.
>
> gdb/ChangeLog:
>
> 	* main.c (get_init_files): Replace "SYSTEM_GDBINIT +
> 	datadir_len" with "&SYSTEM_GDBINIT[datadir_len]".
> ---
>  gdb/main.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/gdb/main.c b/gdb/main.c
> index df4b111..9813041 100644
> --- a/gdb/main.c
> +++ b/gdb/main.c
> @@ -224,7 +224,7 @@ get_init_files (const char **system_gdbinit,
>  	    {
>  	      /* Append the part of SYSTEM_GDBINIT that follows GDB_DATADIR
>  		 to gdb_datadir.  */
> -	      char *tmp_sys_gdbinit = xstrdup (SYSTEM_GDBINIT + datadir_len);
> +	      char *tmp_sys_gdbinit = xstrdup (&SYSTEM_GDBINIT[datadir_len]);
>  	      char *p;
>  
>  	      for (p = tmp_sys_gdbinit; IS_DIR_SEPARATOR (*p); ++p)
> -- 
> 2.7.4

LGTM.

Thanks,
  
Simon Marchi June 25, 2017, 10:58 a.m. UTC | #2
On 2017-06-21 22:35, Sergio Durigan Junior wrote:
> On Wednesday, June 21 2017, Simon Marchi wrote:
> 
>> clang shows this warning:
>> 
>>   /home/emaisin/src/binutils-gdb/gdb/main.c:227:56: error: adding 
>> 'int' to a string does not append to the string 
>> [-Werror,-Wstring-plus-int]
>>                 char *tmp_sys_gdbinit = xstrdup (SYSTEM_GDBINIT + 
>> datadir_len);
>>                                                  
>> ~~~~~~~~~~~~~~~^~~~~~~~~~~~~
>>   /home/emaisin/src/binutils-gdb/gdb/main.c:227:56: note: use array 
>> indexing to silence this warning
>>                 char *tmp_sys_gdbinit = xstrdup (SYSTEM_GDBINIT + 
>> datadir_len);
>>                                                                 ^
>>                                                  &              [      
>>       ]
>> 
>> It's quite easy to get rid of it by using &foo[len] instead of foo + 
>> len.
>> I think this warning is relevant to keep enabled, because it can be an
>> easy mistake to do.
>> 
>> This warning is already discussed here in GCC bugzilla:
>> 
>>   https://gcc.gnu.org/ml/gcc-patches/2017-06/msg00729.html
>> 
>> and a patch series for it was submitted very recently.
>> 
>> gdb/ChangeLog:
>> 
>> 	* main.c (get_init_files): Replace "SYSTEM_GDBINIT +
>> 	datadir_len" with "&SYSTEM_GDBINIT[datadir_len]".
>> ---
>>  gdb/main.c | 2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>> 
>> diff --git a/gdb/main.c b/gdb/main.c
>> index df4b111..9813041 100644
>> --- a/gdb/main.c
>> +++ b/gdb/main.c
>> @@ -224,7 +224,7 @@ get_init_files (const char **system_gdbinit,
>>  	    {
>>  	      /* Append the part of SYSTEM_GDBINIT that follows GDB_DATADIR
>>  		 to gdb_datadir.  */
>> -	      char *tmp_sys_gdbinit = xstrdup (SYSTEM_GDBINIT + 
>> datadir_len);
>> +	      char *tmp_sys_gdbinit = xstrdup 
>> (&SYSTEM_GDBINIT[datadir_len]);
>>  	      char *p;
>> 
>>  	      for (p = tmp_sys_gdbinit; IS_DIR_SEPARATOR (*p); ++p)
>> --
>> 2.7.4
> 
> LGTM.
> 
> Thanks,

Thanks, pushed.
  

Patch

diff --git a/gdb/main.c b/gdb/main.c
index df4b111..9813041 100644
--- a/gdb/main.c
+++ b/gdb/main.c
@@ -224,7 +224,7 @@  get_init_files (const char **system_gdbinit,
 	    {
 	      /* Append the part of SYSTEM_GDBINIT that follows GDB_DATADIR
 		 to gdb_datadir.  */
-	      char *tmp_sys_gdbinit = xstrdup (SYSTEM_GDBINIT + datadir_len);
+	      char *tmp_sys_gdbinit = xstrdup (&SYSTEM_GDBINIT[datadir_len]);
 	      char *p;
 
 	      for (p = tmp_sys_gdbinit; IS_DIR_SEPARATOR (*p); ++p)