fortify-headers

standalone fortify-source implementation
git clone git://git.2f30.org/fortify-headers
Log | Files | Refs | README | LICENSE

commit f9f83da31803062d1fb665ad612ccede5e445757
parent 33acec595b47ec97ae57a98ec396a26f6d8e309e
Author: jvoisin <julien.voisin@dustri.org>
Date:   Sun,  9 Jul 2023 18:03:34 +0200

Add tests for stdio.h

Diffstat:
Mtests/Makefile | 6++++++
Atests/test_fread_int_overflow.c | 14++++++++++++++
Atests/test_fread_overwrite.c | 18++++++++++++++++++
Atests/test_fwrite_int_overflow.c | 14++++++++++++++
Atests/test_fwrite_overwrite.c | 18++++++++++++++++++
Atests/test_vsnprintf.c | 28++++++++++++++++++++++++++++
Atests/test_vsprintf.c | 28++++++++++++++++++++++++++++
7 files changed, 126 insertions(+), 0 deletions(-)

diff --git a/tests/Makefile b/tests/Makefile @@ -51,6 +51,12 @@ TARGETS= \ test_readlink \ test_ttyname_r \ test_write \ + test_fread_int_overflow \ + test_fread_overwrite \ + test_fwrite_int_overflow \ + test_fwrite_overwrite \ + test_vsnprintf \ + test_vsprintf \ .SILENT: diff --git a/tests/test_fread_int_overflow.c b/tests/test_fread_int_overflow.c @@ -0,0 +1,14 @@ +#include "common.h" + +#include <stdio.h> + +int main(int argc, char** argv) { + char buffer[12] = {0}; + + CHK_FAIL_START + fread(buffer, (size_t)-1, (size_t)-1, NULL); + CHK_FAIL_END + + puts(buffer); + return ret; +} diff --git a/tests/test_fread_overwrite.c b/tests/test_fread_overwrite.c @@ -0,0 +1,18 @@ +#include <assert.h> + +#include "common.h" + +#include <stdio.h> + +int main(int argc, char** argv) { + char buffer[12] = {0}; + + assert(sizeof(buffer - 2) * 10 > sizeof(buffer)); + + CHK_FAIL_START + fread(buffer, sizeof(buffer) - 2, 10, NULL); + CHK_FAIL_END + + puts(buffer); + return ret; +} diff --git a/tests/test_fwrite_int_overflow.c b/tests/test_fwrite_int_overflow.c @@ -0,0 +1,14 @@ +#include "common.h" + +#include <stdio.h> + +int main(int argc, char** argv) { + char buffer[12] = {0}; + + CHK_FAIL_START + fwrite(buffer, (size_t)-1, (size_t)-1, NULL); + CHK_FAIL_END + + puts(buffer); + return ret; +} diff --git a/tests/test_fwrite_overwrite.c b/tests/test_fwrite_overwrite.c @@ -0,0 +1,18 @@ +#include <assert.h> + +#include "common.h" + +#include <stdio.h> + +int main(int argc, char** argv) { + char buffer[12] = {0}; + + assert(sizeof(buffer - 2) * 10 > sizeof(buffer)); + + CHK_FAIL_START + fwrite(buffer, sizeof(buffer) - 2, 10, NULL); + CHK_FAIL_END + + puts(buffer); + return ret; +} diff --git a/tests/test_vsnprintf.c b/tests/test_vsnprintf.c @@ -0,0 +1,28 @@ +#include "common.h" + +#include <stdarg.h> +#include <unistd.h> + +char buffer[12] = {0}; + +int msg_valid(int n, const char * format, ... ) { + va_list args; + va_start (args, format); + vsnprintf(buffer, n, format, args); + va_end (args); +} + +int msg(int n, const char * format, ... ) { + va_list args; + va_start (args, format); + CHK_FAIL_START + vsnprintf(buffer, n, format, args); + CHK_FAIL_END + va_end (args); + return ret; +} + +int main(int argc, char** argv) { + msg_valid(sizeof(buffer), "%s", "1234567890ABCDEF"); + return msg(sizeof(buffer)+1, "%s", "1234567890ABCDEF"); +} diff --git a/tests/test_vsprintf.c b/tests/test_vsprintf.c @@ -0,0 +1,28 @@ +#include "common.h" + +#include <stdarg.h> +#include <unistd.h> + +char buffer[12] = {0}; + +int msg_valid(const char * format, ... ) { + va_list args; + va_start (args, format); + vsprintf(buffer, format, args); + va_end (args); +} + +int msg(int n, const char * format, ... ) { + va_list args; + va_start (args, format); + CHK_FAIL_START + vsprintf(buffer, format, args); + CHK_FAIL_END + va_end (args); + return ret; +} + +int main(int argc, char** argv) { + msg_valid("%s", "1234567"); + return msg("%s", "1234567890ABCDEF"); +}