Skip to content

Silence -Wstringop-truncation#55

Open
RhysU wants to merge 1 commit into
imb:masterfrom
RhysU:strncpy_warning
Open

Silence -Wstringop-truncation#55
RhysU wants to merge 1 commit into
imb:masterfrom
RhysU:strncpy_warning

Conversation

@RhysU
Copy link
Copy Markdown
Contributor

@RhysU RhysU commented Jan 16, 2026

GCC 15.2.0 warns...

tests/fct.h:267:5: warning: ‘__builtin_strncpy’ specified bound 256 equals destination size [-Wstringop-truncation]
  267 |     strncpy(dst, src, num);
      |     ^

...because it dislikes writing a length-N src to a length-N dst. Immediately thereafter there's an assignment, dst[num-1] = '\0' to avoid the unbounded string concern. However, GCC does not notice that and still issues the warning. Slightly rework the statements here to avoid the warning without changing the behavior. Routine continues to write at most a length N-1, null-terminated string into the buffer.

GCC 15.2.0 warns...

    tests/fct.h:267:5: warning: ‘__builtin_strncpy’ specified bound 256 equals destination size [-Wstringop-truncation]
      267 |     strncpy(dst, src, num);
          |     ^

...because it dislikes writing a length-N src to a length-N dst.
Immediately thereafter there's an assignment, dst[num-1] = '\0'
to avoid the unbounded string concern.  However, GCC does not
notice that and still issues the warning.  Slightly rework
the statements here to avoid the warning without changing
the behavior.  Routine continues to write at most a length N-1,
null-terminated string into the buffer.
Comment thread include/fct.h
#endif
strncpy(dst, src, num-1);
dst[num-1] = '\0';
#endif
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay to move this final write inside the #endif because of the _TRUNCATE flag on the other half of the #ifdef.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant