On Thu, Oct 1, 2026 at 4:47 AM Oleg Nesterov via Gcc-bugs
<[email protected]> wrote:
>
> See the trivial test-case below.
>
> gcc version 16.2.1 20260819 (Red Hat 16.2.1-2) (GCC)
>
> $ g++ -c -Wall -O2 /tmp/T.cpp -o /dev/null
> In member function ‘virtual bool O_GP::cli(char*)’,
>     inlined from ‘void parse_o(char*)’ at /tmp/T.cpp:31:8,
>     inlined from ‘int main(int, char**)’ at /tmp/T.cpp:37:10:
> /tmp/T.cpp:12:24: warning: ‘char* strncpy(char*, const char*, size_t)’ offset 
> 8 is out of the bounds [0, 8] [-Warray-bounds=]
>    12 |                 strncpy(ylims, arg, 1);
>       |                 ~~~~~~~^~~~~~~~~~~~~~~

Yes this is a known issue, it is already recorded as
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122197 .
We have not figured out a best way to not warn for this false positive yet.

Thanks,
Andrea

>
>
> #include <string.h>
>
> static struct O_N {
>         virtual bool cli(char*)         { return false; }
> } __o_n;
>
> static struct O_GP : public O_N {
>         char ylims[32] = {0};
>
>         bool cli(char *arg)
>         {
>                 strncpy(ylims, arg, 1);
>                 return true;
>         }
> } __o_gp;
>
> static struct O_N *O = &__o_gp;
>
> #define IF(a)   if (!strcmp(n, #a))
>
> static void parse_o(char *n)
> {
>         IF (n)
>                 O = &__o_n;
>         else IF (gp)
>                 O = &__o_gp;
>         else
>                 return;
>
>         extern char *arg;
>         O->cli(arg);
> }
>
> int main(int argc, char* argv[])
> {
>         while (*argv)
>                 parse_o(*argv++);
>         return 0;
> }
>

Reply via email to