diff options
Diffstat (limited to 'tests/test_strncat_safe.c')
| -rw-r--r-- | tests/test_strncat_safe.c | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/tests/test_strncat_safe.c b/tests/test_strncat_safe.c new file mode 100644 index 0000000..ff8cadd --- /dev/null +++ b/tests/test_strncat_safe.c | |||
| @@ -0,0 +1,34 @@ | |||
| 1 | #include "common.h" | ||
| 2 | |||
| 3 | #include <string.h> | ||
| 4 | |||
| 5 | int main(int argc, char** argv) { | ||
| 6 | char buffer[8] = {0}; | ||
| 7 | |||
| 8 | /* Safe: empty buffer, append 7 chars with n=7 → "1234567\0" = exactly 8 bytes */ | ||
| 9 | strncat(buffer, "1234567", 7); | ||
| 10 | puts(buffer); | ||
| 11 | |||
| 12 | /* Safe: reset and append with n larger than source. | ||
| 13 | * src is "AB" (len 2), n=100 → only 2 chars copied + NUL = 3 bytes */ | ||
| 14 | buffer[0] = '\0'; | ||
| 15 | strncat(buffer, "AB", 100); | ||
| 16 | puts(buffer); | ||
| 17 | |||
| 18 | /* Safe: partially filled, append fits exactly. | ||
| 19 | * buffer = "ABCD" (4 chars), append "EFG" with n=3 → 4+3+1 = 8 = exact fit */ | ||
| 20 | buffer[0] = '\0'; | ||
| 21 | strncat(buffer, "ABCD", 4); | ||
| 22 | strncat(buffer, "EFG", 3); | ||
| 23 | puts(buffer); | ||
| 24 | |||
| 25 | /* Safe: n limits copy to fit. | ||
| 26 | * buffer = "ABCDE" (5 chars), src = "FGHIJKLM" (8 chars), n=2 → 5+2+1 = 8 */ | ||
| 27 | buffer[0] = '\0'; | ||
| 28 | strncat(buffer, "ABCDE", 5); | ||
| 29 | strncat(buffer, "FGHIJKLM", 2); | ||
| 30 | puts(buffer); | ||
| 31 | |||
| 32 | /* All safe operations passed without trapping */ | ||
| 33 | return 0; | ||
| 34 | } | ||
