Silence gcc-8 warnings

Michael Matz matz@suse.de
Mon Apr 23 13:42:00 GMT 2018


Hi,

On Mon, 23 Apr 2018, Alan Modra wrote:

> All of these warnings were false positives.  -Wstringop-truncation is
> particularly annoying when it warns about strncpy used quite correctly.

Which version of gcc-8 were you using?  I fixed one problem with that 
warning a couple days ago.  I certainly can't reproduce warnings on this 
code sample, which seems to reflect the situation in elf-linux-core.h:

% cat x3.c
#include <stdlib.h>
#include <string.h>

struct S {
    int i;
    char pr_fname[16+1];
    char pr_psargs[80+1];
};

void copy_stuff (struct S *dest, struct S *src)
{
  dest->i = src->i;
  strncpy (dest->pr_fname, src->pr_fname, sizeof(dest->pr_fname));
  strncpy (dest->pr_psargs, src->pr_psargs, sizeof(dest->pr_psargs));
}


Ciao,
Michael.

> Changing strncpy to memcpy in the bfd files would be wrong.
> 
> bfd/
> 	* elf-linux-core.h (swap_linux_prpsinfo32_ugid32_out): Disable
> 	gcc-8 string truncation warning.
> 	(swap_linux_prpsinfo32_ugid16_out): Likewise.
> 	(swap_linux_prpsinfo64_ugid32_out): Likewise.
> 	(swap_linux_prpsinfo64_ugid16_out): Likewise.
> 	* elf.c (elfcore_write_prpsinfo): Likewise.
> gas/
> 	* stabs.c (generate_asm_file): Use memcpy rather than strncpy.
> 	Remove call to strlen inside loop.
> 	* config/tc-cr16.c (getreg_image): Warning fix.
> 	* config/tc-crx.c (getreg_image): Warning fix.
> 
> diff --git a/bfd/elf-linux-core.h b/bfd/elf-linux-core.h
> index 0a5d76f..25d33f6 100644
> --- a/bfd/elf-linux-core.h
> +++ b/bfd/elf-linux-core.h
> @@ -69,8 +69,12 @@ swap_linux_prpsinfo32_ugid32_out
>    bfd_put_32 (obfd, from->pr_ppid, to->pr_ppid);
>    bfd_put_32 (obfd, from->pr_pgrp, to->pr_pgrp);
>    bfd_put_32 (obfd, from->pr_sid, to->pr_sid);
> +#pragma GCC diagnostic push
> +#pragma GCC diagnostic ignored "-Wpragmas"
> +#pragma GCC diagnostic ignored "-Wstringop-truncation"
>    strncpy (to->pr_fname, from->pr_fname, sizeof (to->pr_fname));
>    strncpy (to->pr_psargs, from->pr_psargs, sizeof (to->pr_psargs));
> +#pragma GCC diagnostic pop
>  }
>  
>  /* External 32-bit structure for PRPSINFO.  This structure is
> @@ -121,8 +125,12 @@ swap_linux_prpsinfo32_ugid16_out
>    bfd_put_32 (obfd, from->pr_ppid, to->pr_ppid);
>    bfd_put_32 (obfd, from->pr_pgrp, to->pr_pgrp);
>    bfd_put_32 (obfd, from->pr_sid, to->pr_sid);
> +#pragma GCC diagnostic push
> +#pragma GCC diagnostic ignored "-Wpragmas"
> +#pragma GCC diagnostic ignored "-Wstringop-truncation"
>    strncpy (to->pr_fname, from->pr_fname, sizeof (to->pr_fname));
>    strncpy (to->pr_psargs, from->pr_psargs, sizeof (to->pr_psargs));
> +#pragma GCC diagnostic pop
>  }
>  
>  /* External 64-bit structure for PRPSINFO.  This structure is
> @@ -174,8 +182,12 @@ swap_linux_prpsinfo64_ugid32_out
>    bfd_put_32 (obfd, from->pr_ppid, to->pr_ppid);
>    bfd_put_32 (obfd, from->pr_pgrp, to->pr_pgrp);
>    bfd_put_32 (obfd, from->pr_sid, to->pr_sid);
> +#pragma GCC diagnostic push
> +#pragma GCC diagnostic ignored "-Wpragmas"
> +#pragma GCC diagnostic ignored "-Wstringop-truncation"
>    strncpy (to->pr_fname, from->pr_fname, sizeof (to->pr_fname));
>    strncpy (to->pr_psargs, from->pr_psargs, sizeof (to->pr_psargs));
> +#pragma GCC diagnostic pop
>  }
>  
>  /* External 64-bit structure for PRPSINFO.  This structure is
> @@ -227,8 +239,12 @@ swap_linux_prpsinfo64_ugid16_out
>    bfd_put_32 (obfd, from->pr_ppid, to->pr_ppid);
>    bfd_put_32 (obfd, from->pr_pgrp, to->pr_pgrp);
>    bfd_put_32 (obfd, from->pr_sid, to->pr_sid);
> +#pragma GCC diagnostic push
> +#pragma GCC diagnostic ignored "-Wpragmas"
> +#pragma GCC diagnostic ignored "-Wstringop-truncation"
>    strncpy (to->pr_fname, from->pr_fname, sizeof (to->pr_fname));
>    strncpy (to->pr_psargs, from->pr_psargs, sizeof (to->pr_psargs));
> +#pragma GCC diagnostic pop
>  }
>  
>  #endif
> diff --git a/bfd/elf.c b/bfd/elf.c
> index 8ea5a81..b4549ea 100644
> --- a/bfd/elf.c
> +++ b/bfd/elf.c
> @@ -10549,8 +10549,12 @@ elfcore_write_prpsinfo (bfd  *abfd,
>  #endif
>  
>        memset (&data, 0, sizeof (data));
> +#pragma GCC diagnostic push
> +#pragma GCC diagnostic ignored "-Wpragmas"
> +#pragma GCC diagnostic ignored "-Wstringop-truncation"
>        strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
>        strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
> +#pragma GCC diagnostic pop
>        return elfcore_write_note (abfd, buf, bufsiz,
>  				 "CORE", note_type, &data, sizeof (data));
>      }
> diff --git a/gas/config/tc-cr16.c b/gas/config/tc-cr16.c
> index 0b73003..d25afcc 100644
> --- a/gas/config/tc-cr16.c
> +++ b/gas/config/tc-cr16.c
> @@ -1124,8 +1124,7 @@ getreg_image (reg r)
>  /* Issue a error message when register is illegal.  */
>  #define IMAGE_ERR \
>    as_bad (_("Illegal register (`%s') in Instruction: `%s'"), \
> -            reg_name, ins_parse);                            \
> -  break;
> +	  reg_name, ins_parse);
>  
>    switch (rreg->type)
>      {
> @@ -1134,6 +1133,7 @@ getreg_image (reg r)
>          return rreg->image;
>        else
>          IMAGE_ERR;
> +      break;
>  
>      case CR16_P_REGTYPE:
>        return rreg->image;
> @@ -1141,6 +1141,7 @@ getreg_image (reg r)
>  
>      default:
>        IMAGE_ERR;
> +      break;
>      }
>  
>    return 0;
> diff --git a/gas/config/tc-crx.c b/gas/config/tc-crx.c
> index ce8cbce..8cf4af4 100644
> --- a/gas/config/tc-crx.c
> +++ b/gas/config/tc-crx.c
> @@ -1135,8 +1135,7 @@ getreg_image (reg r)
>  /* Issue a error message when register is illegal.  */
>  #define IMAGE_ERR \
>    as_bad (_("Illegal register (`%s') in instruction: `%s'"), \
> -	    reg_name, ins_parse);			     \
> -  break;
> +	  reg_name, ins_parse);
>  
>    switch (rreg->type)
>    {
> @@ -1145,18 +1144,21 @@ getreg_image (reg r)
>  	return rreg->image;
>        else
>  	IMAGE_ERR;
> +      break;
>  
>      case CRX_CFG_REGTYPE:
>        if (is_procreg)
>  	return rreg->image;
>        else
>  	IMAGE_ERR;
> +      break;
>  
>      case CRX_R_REGTYPE:
>        if (! is_procreg)
>  	return rreg->image;
>        else
>  	IMAGE_ERR;
> +      break;
>  
>      case CRX_C_REGTYPE:
>      case CRX_CS_REGTYPE:
> @@ -1165,6 +1167,7 @@ getreg_image (reg r)
>  
>      default:
>        IMAGE_ERR;
> +      break;
>    }
>  
>    return 0;
> diff --git a/gas/stabs.c b/gas/stabs.c
> index d82de31..6ddbdad 100644
> --- a/gas/stabs.c
> +++ b/gas/stabs.c
> @@ -543,12 +543,12 @@ generate_asm_file (int type, const char *file)
>    while (tmp < file_endp)
>      {
>        const char *bslash = strchr (tmp, '\\');
> -      size_t len = (bslash) ? (size_t) (bslash - tmp + 1) : strlen (tmp);
> +      size_t len = bslash != NULL ? bslash - tmp + 1 : file_endp - tmp;
>  
>        /* Double all backslashes, since demand_copy_C_string (used by
>  	 s_stab to extract the part in quotes) will try to replace them as
>  	 escape sequences.  backslash may appear in a filespec.  */
> -      strncpy (bufp, tmp, len);
> +      memcpy (bufp, tmp, len);
>  
>        tmp += len;
>        bufp += len;
> 
> 



More information about the Binutils mailing list