Skip to content

Commit

Permalink
Add tests for stpcpy
Browse files Browse the repository at this point in the history
  • Loading branch information
jvoisin committed Jun 22, 2023
1 parent cb1ce9e commit af7480d
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 5 deletions.
13 changes: 10 additions & 3 deletions include/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,16 @@ __access(write_only, 1)
__access(read_only, 2)
_FORTIFY_FN(stpcpy) char *stpcpy(char *__d, const char *__s)
{
size_t __b = __bos(__d, 0);
size_t __n = strlen(__s) + 1;

if (strlen(__s) + 1 > __b)
/* trap if pointers are overlapping but not if dst == src.
* gcc seems to like to generate code that relies on dst == src */
if ((__d < __s && __d + __n > __s) ||
(__s < __d && __s + __n > __d))
__builtin_trap();

size_t __b = __bos(__d, 0);
if (__n > __b)
__builtin_trap();
return __orig_stpcpy(__d, __s);
}
Expand Down Expand Up @@ -129,7 +136,7 @@ _FORTIFY_FN(strcpy) char *strcpy(char *__d, const char *__s)
__builtin_trap();

size_t __b = __bos(__d, 0);
if (strlen(__s) + 1 > __b)
if (__n > __b)
__builtin_trap();
return __orig_strcpy(__d, __s);
}
Expand Down
7 changes: 5 additions & 2 deletions tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ TARGETS=test_memcpy_static_write \
test_memmove_dynamic_read \
test_memset_static_write \
test_memset_dynamic_write \
test_strcpy_static_write \
test_stpcpy_overwrite_over \
test_stpcpy_overwrite_under \
test_stpcpy_static_write \
test_strcat_static_write \
test_strcpy_overwrite_over \
test_strcpy_overwrite_under \
test_strncpy_static_write \
test_strcpy_static_write \
test_strncpy_overwrite_over \
test_strncpy_overwrite_under \
test_strncpy_static_write \
test_getcwd \

.SILENT:
Expand Down
15 changes: 15 additions & 0 deletions tests/test_stpcpy_overwrite_over.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "common.h"

#include <string.h>

int main(int argc, char** argv) {
char buffer[9] = {'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', '\0'};
puts(buffer);

CHK_FAIL_START
stpcpy(buffer+1, buffer);
CHK_FAIL_END

puts(buffer);
return ret;
}
15 changes: 15 additions & 0 deletions tests/test_stpcpy_overwrite_under.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "common.h"

#include <string.h>

int main(int argc, char** argv) {
char buffer[9] = {'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', '\0'};
puts(buffer);

CHK_FAIL_START
stpcpy(buffer-1, buffer);
CHK_FAIL_END

puts(buffer);
return ret;
}
16 changes: 16 additions & 0 deletions tests/test_stpcpy_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};
strcpy(buffer, "1234567");
puts(buffer);

CHK_FAIL_START
stpcpy(buffer, "1234567890");
CHK_FAIL_END

puts(buffer);
return ret;
}

0 comments on commit af7480d

Please sign in to comment.