Skip to content

Commit

Permalink
test-str.c: fix gcc warnings with FORTIFY_SOURCE enabled
Browse files Browse the repository at this point in the history
* test_strncpy(). The test intentionally copies less than the total length
  of the string, so it looks like there is nothing we can do about it in
  the test other than explicitly ignoring the warnings.

    inlined from ‘test_strncpy’ at test-str.c:943:2:
    /usr/include/x86_64-linux-gnu/bits/string_fortified.h:106:10: error: ‘__builtin_strncpy’
      output may be truncated copying 1 byte from a string of length 8191 [-Werror=stringop-truncation]
      106 |   return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
          |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ...

* test_strcat(). The fix for the warning below is to avoid using overlapping
  buffers in src and dst. This is actually a requirement for both strcat() and
  strncpy(), so fix both functions.

    inlined from ‘test_strcat’ at test-str.c:1052:2:
    /usr/include/x86_64-linux-gnu/bits/string_fortified.h:128:10: error: ‘__builtin___strcat_chk’
      accessing 4097 or more bytes at offsets 4096 and 0 may overlap 1 byte at offset 4096 [-Werror=restrict]
      128 |   return __builtin___strcat_chk (__dest, __src, __bos (__dest));
          |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ...

Signed-off-by: Alexey Kodanev <[email protected]>
  • Loading branch information
akodanev authored and vathpela committed May 4, 2022
1 parent 4df989a commit 6aac595
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions test-str.c
Original file line number Diff line number Diff line change
Expand Up @@ -926,12 +926,15 @@ static int
test_strncpy(void)
{
char s[] = "0123456789abcdef\0000";
char s0[4096+4096];
char *s1 = &s0[4096];
char s0[4096];
char s1[4096];

memset(s0, 0, sizeof(s0));
memcpy(s0, s, sizeof(s));

#if __GNUC_PREREQ(8, 1)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wstringop-truncation"
#endif
memset(s1, 0, 4096);
assert_equal_return(strncpy(s1, s0, 0), s1, -1, "got %p expected %p\n");
assert_equal_return(strlen(s1), 0, -1, "got %d expected %d\n");
Expand Down Expand Up @@ -1030,20 +1033,22 @@ test_strncpy(void)
assert_equal_return(s1[16], '\000', -1, "got %#02hhx expected %02hhx\n");
assert_equal_return(s1[17], '0', -1, "got %#02hhx expected %02hhx\n");
assert_equal_return(s1[18], '1', -1, "got %#02hhx expected %02hhx\n");

#if __GNUC_PREREQ(8, 1)
# pragma GCC diagnostic pop
#endif
return 0;
}

static int
test_strcat(void)
{
char s[] = "0123456789abcdef\0000";
char s0[8192];
char *s1 = &s0[4096];
char s0[4096];
char s1[4096];
char *s2;
char s3[] = "0123456789abcdef0123456789abcdef\000\000\000\000\000";

memset(s0, 0, 8192);
memset(s0, 0, sizeof(s0));
memcpy(s0, s, sizeof(s));

memset(s1, 0, 4096);
Expand Down

0 comments on commit 6aac595

Please sign in to comment.