Skip to content

-Werror=stringop-*#1401

Merged
hallyn merged 2 commits into
shadow-maint:masterfrom
alejandro-colomar:stringop
Jul 15, 2026
Merged

-Werror=stringop-*#1401
hallyn merged 2 commits into
shadow-maint:masterfrom
alejandro-colomar:stringop

Conversation

@alejandro-colomar

@alejandro-colomar alejandro-colomar commented Dec 5, 2025

Copy link
Copy Markdown
Collaborator

Enable some errors


Revisions:

v1b
  • Rebase
$ git rd 
1:  e2b8dea92 = 1:  914cc281d autogen.sh: Enable -Werror=stringop-* diagnostics
2:  6afa1de54 = 2:  54a9b2f25 tests/unit/test_strncpy.c: Remove strncpy_a() tests
v1c
  • Rebase
$ git rd 
1:  914cc281d = 1:  d80d40e9b autogen.sh: Enable -Werror=stringop-* diagnostics
2:  54a9b2f25 = 2:  5284b6830 tests/unit/test_strncpy.c: Remove strncpy_a() tests
v1d
  • Rebase
$ git rd 
1:  d80d40e9b289 = 1:  6fc3fff7ca98 autogen.sh: Enable -Werror=stringop-* diagnostics
2:  5284b68308cd = 2:  b881eee7e628 tests/unit/test_strncpy.c: Remove strncpy_a() tests
v1e
  • Rebase
$ git rd 
1:  6fc3fff7ca98 = 1:  989757d8b204 autogen.sh: Enable -Werror=stringop-* diagnostics
2:  b881eee7e628 ! 2:  692bb72f92b2 tests/unit/test_strncpy.c: Remove strncpy_a() tests
    @@ Commit message
     
      ## tests/unit/Makefile.am ##
     @@ tests/unit/Makefile.am: check_PROGRAMS = \
    -     test_atoi_strtoi \
    +     test_chkhash \
          test_chkname \
          test_snprintf \
     -    test_strncpy \
v2
  • Rebase
  • Drop -Werror=stringop-truncation. There's a false positive in legitimate strncpy(3) calls.
$ git rd 
1:  989757d8 ! 1:  a1c9e134 autogen.sh: Enable -Werror=stringop-* diagnostics
    @@ Commit message
         We can't enable -Wstringop-overread because it has bogus diagnostics
         with legitimate strncat(3) calls.
     
    +    We can't enable -Wstringop-truncation because it has bogus diagnostics
    +    with legitimate strncpy(3) calls.
    +
         Link: <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=123024>
         Signed-off-by: Alejandro Colomar <alx@kernel.org>
     
    @@ autogen.sh: CFLAGS="$CFLAGS -Werror=incompatible-pointer-types"
      CFLAGS="$CFLAGS -Werror=sign-compare"
      CFLAGS="$CFLAGS -Werror=sizeof-pointer-div"
     +CFLAGS="$CFLAGS -Werror=stringop-overflow=4"
    -+CFLAGS="$CFLAGS -Werror=stringop-truncation"
      CFLAGS="$CFLAGS -Werror=unused-but-set-parameter"
      CFLAGS="$CFLAGS -Werror=unused-function"
      CFLAGS="$CFLAGS -Werror=unused-label"
2:  692bb72f = 2:  52dee45f tests/unit/test_strncpy.c: Remove strncpy_a() tests
v2b
  • Rebase
$ git rd -U0
1:  a1c9e134 = 1:  64df7a0f autogen.sh: Enable -Werror=stringop-* diagnostics
2:  52dee45f ! 2:  b848f9fe tests/unit/test_strncpy.c: Remove strncpy_a() tests
    @@ tests/unit/test_strncpy.c (deleted)
    +-#include "attr.h"
    @@ tests/unit/test_strncpy.c (deleted)
    --
    --static void test_strncpy_a_trunc(void **);
    --static void test_strncpy_a_fit(void **);
    --static void test_strncpy_a_pad(void **);
    +-static void test_strncpy_a_trunc(MAYBE_UNUSED void ** _1);
    +-static void test_strncpy_a_fit(MAYBE_UNUSED void ** _1);
    +-static void test_strncpy_a_pad(MAYBE_UNUSED void ** _1);
    @@ tests/unit/test_strncpy.c (deleted)
    --test_strncpy_a_trunc(void **)
    +-test_strncpy_a_trunc(MAYBE_UNUSED void ** _1)
    @@ tests/unit/test_strncpy.c (deleted)
    --test_strncpy_a_fit(void **)
    +-test_strncpy_a_fit(MAYBE_UNUSED void ** _1)
    @@ tests/unit/test_strncpy.c (deleted)
    --test_strncpy_a_pad(void **)
    +-test_strncpy_a_pad(MAYBE_UNUSED void ** _1)
v2c
  • Rebase
$ git rd 
1:  64df7a0f ! 1:  60baaf47 autogen.sh: Enable -Werror=stringop-* diagnostics
    @@ Commit message
      ## autogen.sh ##
     @@ autogen.sh: CFLAGS="$CFLAGS -Werror=incompatible-pointer-types"
      CFLAGS="$CFLAGS -Werror=int-conversion"
    - CFLAGS="$CFLAGS -Werror=sign-compare"
    + CFLAGS="$CFLAGS -Werror=overflow"
      CFLAGS="$CFLAGS -Werror=sizeof-pointer-div"
     +CFLAGS="$CFLAGS -Werror=stringop-overflow=4"
      CFLAGS="$CFLAGS -Werror=unused-but-set-parameter"
2:  b848f9fe = 2:  f1d1e2c4 tests/unit/test_strncpy.c: Remove strncpy_a() tests
v2d
  • Rebase
$ git rd 
1:  60baaf47 = 1:  759e4ed1 autogen.sh: Enable -Werror=stringop-* diagnostics
2:  f1d1e2c4 ! 2:  cc60b931 tests/unit/test_strncpy.c: Remove strncpy_a() tests
    @@ tests/unit/Makefile.am
     @@ tests/unit/Makefile.am: check_PROGRAMS = \
          test_chkhash \
          test_chkname \
    -     test_snprintf \
    +     test_stprintf \
     -    test_strncpy \
          test_strtcpy \
          test_typetraits \
          test_exit_if_null
    -@@ tests/unit/Makefile.am: test_snprintf_LDADD = \
    +@@ tests/unit/Makefile.am: test_stprintf_LDADD = \
          $(CMOCKA_LIBS) \
          $(NULL)
      
v2e
  • Rebase
$ git rd 
1:  759e4ed18f6d = 1:  339452eb8f11 autogen.sh: Enable -Werror=stringop-* diagnostics
2:  cc60b9319ed2 ! 2:  801b1ca131a8 tests/unit/test_strncpy.c: Remove strncpy_a() tests
    @@ tests/unit/Makefile.am: test_stprintf_LDADD = \
     -test_strncpy_LDFLAGS = \
     -    $(NULL)
     -test_strncpy_LDADD = \
    +-    $(LIBSHADOW) \
     -    $(CMOCKA_LIBS) \
     -    $(NULL)
     -
      test_strtcpy_SOURCES = \
    -     ../../lib/string/strcpy/strtcpy.c \
          test_strtcpy.c \
    +     $(NULL)
     
      ## tests/unit/test_strncpy.c (deleted) ##
     @@
v2f
$ git rd 
1:  339452eb8f11 ! 1:  38a078b8ecca autogen.sh: Enable -Werror=stringop-* diagnostics
    @@ Commit message
         with legitimate strncpy(3) calls.
     
         Link: <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=123024>
    +    Reviewed-by: Serge Hallyn <serge@hallyn.com>
         Signed-off-by: Alejandro Colomar <alx@kernel.org>
     
      ## autogen.sh ##
2:  801b1ca131a8 ! 2:  d12f08cba686 tests/unit/test_strncpy.c: Remove strncpy_a() tests
    @@ Commit message
         arrays, but I never looked at that code.)
     
         Link: <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122963>
    +    Reviewed-by: Serge Hallyn <serge@hallyn.com>
         Signed-off-by: Alejandro Colomar <alx@kernel.org>
     
      ## tests/unit/Makefile.am ##

@alejandro-colomar

Copy link
Copy Markdown
Collaborator Author

The opensuse CI is consistently failing, but it seems to be a spurious error.

@alejandro-colomar

Copy link
Copy Markdown
Collaborator Author

Cc: @kees

@kees kees left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hm, I never like to see tests removed unless an API has been removed with a compile-time check for its accidental reintroduction. Has strncpy been completely removed from this codebase? Perhaps add:

#define strncpy(...) static_assert(false, "Never use strncpy")

or something like?

@alejandro-colomar

Copy link
Copy Markdown
Collaborator Author

Hm, I never like to see tests removed unless an API has been removed with a compile-time check for its accidental reintroduction. Has strncpy been completely removed from this codebase? Perhaps add:

#define strncpy(...) static_assert(false, "Never use strncpy")

or something like?

No, we do actually use strncpy(3) through its wrapper strncpy_a(); it's a function I actually like (for a niche use case).

The reason for removing that test is not that we don't use it, but that GCC has false positives.

See this GCC report (from me):

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122963

That report is about strncat(3), but strncpy(3) has a similar false positive.

@alejandro-colomar

alejandro-colomar commented Feb 26, 2026

Copy link
Copy Markdown
Collaborator Author

Also, I'm not too worried about the tests, because the implementation is so trivial that the tests are overkill.

#define strncpy_a(dst, src)  strncpy(dst, src, countof(dst))

We added the tests back when the API was significantly more complex, IIRC.

@alejandro-colomar

Copy link
Copy Markdown
Collaborator Author

@hallyn

@hallyn hallyn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sounds reasonable. I trust your judgement on this.

@hallyn

hallyn commented Jul 15, 2026

Copy link
Copy Markdown
Member

Oh phoey - pls ping me when you resolve the conflict :)

(or reply and I'll do it)

@alejandro-colomar

alejandro-colomar commented Jul 15, 2026

Copy link
Copy Markdown
Collaborator Author

Oh phoey - pls ping me when you resolve the conflict :)

(or reply and I'll do it)

Solved (in v2e); thanks!

We can't enable -Wstringop-overread because it has bogus diagnostics
with legitimate strncat(3) calls.

We can't enable -Wstringop-truncation because it has bogus diagnostics
with legitimate strncpy(3) calls.

Link: <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=123024>
Reviewed-by: Serge Hallyn <serge@hallyn.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
It's just the obvious thin wrapper around strncpy(3); let's trust it's
ok.

The reason for removing this test is that GCC has bogus diagnostics
for strncpy(3).  Its diagnostics are geared towards helping people
that abuse strncpy(3) as a poor-man's strlcpy(3) not write exploitable
code as easily.  Using strncpy(3) for that purpose is brain damaged, and
those programs should be audited to stop using this API for that.  And
most importantly, GCC should stop encouraging writing bad code that
calls strncpy(3) as that results in diagnostics that are actively
harmful for us, legitimate users of strncpy(3).  Those false positives
should certainly be out of -Wall.

We could fill the tests with pragmas, but let's just remove the tests.
I don't feel like maintaining code for ignoring GCC's brain bamage.

If people want to write a function for truncating strings (because they
can't rely on strlcpy(3) being available, or because they don't like
it), they certainly should write such an API.  strncpy(3) isn't that
API.  strncpy(3) is a function that takes a string and writes it into a
utmpx(5) member, which is NOT a string.  (And I heard it might also be
useful for implementing tar(1), which also uses non-terminated character
arrays, but I never looked at that code.)

Link: <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122963>
Reviewed-by: Serge Hallyn <serge@hallyn.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
@hallyn
hallyn merged commit f02b40c into shadow-maint:master Jul 15, 2026
13 of 15 checks passed
@hallyn

hallyn commented Jul 15, 2026

Copy link
Copy Markdown
Member

Awesome, thanks.

@alejandro-colomar
alejandro-colomar deleted the stringop branch July 15, 2026 16:12
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.

3 participants