Skip to content

Commit

Permalink
Add a test for strncat
Browse files Browse the repository at this point in the history
  • Loading branch information
jvoisin committed Jun 22, 2023
1 parent 532f4bf commit b714dbd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
5 changes: 2 additions & 3 deletions include/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,12 @@ __access (read_only, 2, 3)
_FORTIFY_FN(strncat) char *strncat(char *__d, const char *__s, size_t __n)
{
size_t __b = __bos(__d, 0);
size_t __sl, __dl;

if (__n > __b) {
__sl = strlen(__s);
__dl = strlen(__d);
size_t __sl = strlen(__s);
if (__sl > __n)
__sl = __n;
size_t __dl = strlen(__d);
if (__sl + __dl + 1 > __b)
__builtin_trap();
}
Expand Down
1 change: 1 addition & 0 deletions tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ TARGETS=test_memcpy_static_write \
test_strcpy_overwrite_over \
test_strcpy_overwrite_under \
test_strcpy_static_write \
test_strncat_static_write \
test_strncpy_overwrite_over \
test_strncpy_overwrite_under \
test_strncpy_static_write \
Expand Down
16 changes: 16 additions & 0 deletions tests/test_strncat_static_write.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "common.h"

#include <string.h>

int main(int argc, char** argv) {
char buffer[8] = {0};
strncat(buffer, "1234567", 5);
puts(buffer);

CHK_FAIL_START
strncat(buffer, "1234567890", 10);
CHK_FAIL_END

puts(buffer);
return ret;
}

0 comments on commit b714dbd

Please sign in to comment.