Skip to content

'strncpy' output truncated warning with gcc 10 #9

@babelouest

Description

@babelouest

While trying to build liboauth2 for Debian unstable with gcc 10, I have the following compiling error:

In function 'oauth2_strndup',
    inlined from 'oauth2_strdup' at src/util.c:545:9:
src/util.c:535:2: error: 'strncpy' output truncated before terminating nul copying as many bytes from a string as its length [-Werror=stringop-truncation]
  535 |  strncpy(dst, src, len);
      |  ^~~~~~~~~~~~~~~~~~~~~~
src/util.c: In function 'oauth2_strdup':
src/util.c:545:35: note: length computed here
  545 |  return oauth2_strndup(src, src ? strlen(src) : 0);
      |                                   ^~~~~~~~~~~
In function 'oauth2_strndup',
    inlined from 'oauth2_strdup' at src/util.c:545:9,
    inlined from '_oauth2_stradd4' at src/util.c:555:9:
src/util.c:535:2: error: 'strncpy' destination unchanged after copying no bytes [-Werror=stringop-truncation]
  535 |  strncpy(dst, src, len);
      |  ^~~~~~~~~~~~~~~~~~~~~~
In function 'oauth2_strndup',
    inlined from 'oauth2_strdup' at src/util.c:545:9,
    inlined from 'oauth2_nv_list2s' at src/util.c:611:14:
src/util.c:535:2: error: 'strncpy' output truncated before terminating nul copying 1 byte from a string of the same length [-Werror=stringop-truncation]
  535 |  strncpy(dst, src, len);
      |  ^~~~~~~~~~~~~~~~~~~~~~

A quick workaround is to replace strncpy with memcpy on src/util.c:545 or to disable the warning just for this case:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstringop-truncation"
    strncpy(dst, src, len);
#pragma GCC diagnostic pop

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions