[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#915405: gcc-8: Spurious stringop-overflow error when compiling with -O2



Package: gcc-8
Version: 8.2.0-9
Severity: normal

Dear Maintainer,
I have a piece of code in a CMake project that fails to compile when building
in RelWithDebInfo mode.
The problem seems to come from gcc-8 when both -Wall and -O2 options are set:

char* copy_string(const char* str) {
  int len = strlen(str)+1;
  char* dest = malloc(sizeof(char)*len);
  if(dest) {
    strncpy(dest, str, len);
  }
  return dest;
}

gcc reports the following error:

test_strncpy.c:7:3: error: ‘strncpy’ specified bound depends on the length of
the source argument [-Werror=stringop-overflow=]
   strncpy(dest, str, len);
   ^~~~~~~~~~~~~~~~~~~~~~~
test_strncpy.c:5:13: note: length computed here
   int len = strlen(str)+1;
             ^~~~~~~~~~~
cc1: all warnings being treated as errors


In this case, there is no problem in the source code, but -Werror=stringop-
overflow still reports an error.

Gcc only reports an error when -O2 and -Werror=stringop-overflow (which is
enabled by -Wall) are set. Replacing strncpy with memcpy also fixes the
problem.


*** Reporter, please consider answering these questions, where appropriate ***

   * What led up to the situation?
   * What exactly did you do (or not do) that was effective (or
     ineffective)?
   * What was the outcome of this action?
   * What outcome did you expect instead?

*** End of the template - remove these template lines ***



-- System Information:
Debian Release: buster/sid
  APT prefers testing
  APT policy: (500, 'testing'), (500, 'stable')
Architecture: amd64 (x86_64)

Kernel: Linux 4.18.0-2-amd64 (SMP w/4 CPU cores)
Locale: LANG=fr_FR.UTF-8, LC_CTYPE=fr_FR.UTF-8 (charmap=UTF-8), LANGUAGE= (charmap=UTF-8)
Shell: /bin/sh linked to /bin/dash
Init: systemd (via /run/systemd/system)
LSM: AppArmor: enabled

Versions of packages gcc-8 depends on:
ii  binutils      2.31.1-10
ii  cpp-8         8.2.0-9
ii  gcc-8-base    8.2.0-9
ii  libc6         2.27-8
ii  libcc1-0      8.2.0-9
ii  libgcc-8-dev  8.2.0-9
ii  libgcc1       1:8.2.0-9
ii  libgmp10      2:6.1.2+dfsg-3
ii  libisl19      0.20-2
ii  libmpc3       1.1.0-1
ii  libmpfr6      4.0.1-1
ii  libstdc++6    8.2.0-9
ii  zlib1g        1:1.2.11.dfsg-1

Versions of packages gcc-8 recommends:
ii  libc6-dev  2.27-8

Versions of packages gcc-8 suggests:
pn  gcc-8-doc         <none>
pn  gcc-8-locales     <none>
ii  gcc-8-multilib    8.2.0-9
pn  libasan5-dbg      <none>
pn  libatomic1-dbg    <none>
pn  libgcc1-dbg       <none>
pn  libgomp1-dbg      <none>
pn  libitm1-dbg       <none>
pn  liblsan0-dbg      <none>
pn  libmpx2-dbg       <none>
pn  libquadmath0-dbg  <none>
pn  libtsan0-dbg      <none>
pn  libubsan1-dbg     <none>

-- no debconf information

Reply to: