From 140cffbe84a08669d67c3257258d2bb70ff29c3b Mon Sep 17 00:00:00 2001 From: jvoisin Date: Fri, 8 Mar 2024 16:07:57 +0100 Subject: Add some NULL-pointers checks See: - https://www.imperialviolet.org/2016/06/26/nonnull.html - https://davidben.net/2024/01/15/empty-slices.html --- tests/Makefile | 6 ++++++ tests/test_memchr_null.c | 13 +++++++++++++ tests/test_memcpy_null_dst.c | 16 ++++++++++++++++ tests/test_memcpy_null_src.c | 16 ++++++++++++++++ tests/test_memmove_null_dst.c | 16 ++++++++++++++++ tests/test_memmove_null_src.c | 16 ++++++++++++++++ tests/test_memset_null.c | 13 +++++++++++++ 7 files changed, 96 insertions(+) create mode 100644 tests/test_memchr_null.c create mode 100644 tests/test_memcpy_null_dst.c create mode 100644 tests/test_memcpy_null_src.c create mode 100644 tests/test_memmove_null_dst.c create mode 100644 tests/test_memmove_null_src.c create mode 100644 tests/test_memset_null.c (limited to 'tests') diff --git a/tests/Makefile b/tests/Makefile index 352e6f8..b1223ea 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -48,10 +48,15 @@ RUNTIME_TARGETS= \ test_mbstowcs_static \ test_memchr_dynamic_read \ test_memchr_static_read \ + test_memchr_null \ test_memcpy_dynamic_read \ test_memcpy_dynamic_write \ test_memcpy_overwrite_over \ test_memcpy_static_read \ + test_memcpy_null_src \ + test_memcpy_null_dst \ + test_memmove_null_src \ + test_memmove_null_dst \ test_memmove_dynamic_read \ test_memmove_dynamic_write \ test_memmove_static_read \ @@ -62,6 +67,7 @@ RUNTIME_TARGETS= \ test_mempcpy_static_write \ test_memset_dynamic_write \ test_memset_static_write \ + test_memset_null \ test_poll_dynamic \ test_poll_static \ test_ppoll_dynamic \ diff --git a/tests/test_memchr_null.c b/tests/test_memchr_null.c new file mode 100644 index 0000000..02b994d --- /dev/null +++ b/tests/test_memchr_null.c @@ -0,0 +1,13 @@ +#include "common.h" + +#include + +int main(int argc, char** argv) { +#ifndef __GNUC__ + CHK_FAIL_START + memchr(NULL, (int)'A', 0); + CHK_FAIL_END +#endif + + return ret; +} diff --git a/tests/test_memcpy_null_dst.c b/tests/test_memcpy_null_dst.c new file mode 100644 index 0000000..9def69c --- /dev/null +++ b/tests/test_memcpy_null_dst.c @@ -0,0 +1,16 @@ +#include "common.h" + +#include + +int main(int argc, char** argv) { +#ifndef __GNUC__ + char buffer[12] = {0}; + + CHK_FAIL_START + memcpy(buffer, NULL, 0); + CHK_FAIL_END + + puts(buffer); +#endif + return ret; +} diff --git a/tests/test_memcpy_null_src.c b/tests/test_memcpy_null_src.c new file mode 100644 index 0000000..42f99a9 --- /dev/null +++ b/tests/test_memcpy_null_src.c @@ -0,0 +1,16 @@ +#include "common.h" + +#include + +int main(int argc, char** argv) { +#ifndef __GNUC__ + char buffer[12] = {0}; + + CHK_FAIL_START + memcpy(NULL, buffer, 0); + CHK_FAIL_END + + puts(buffer); +#endif + return ret; +} diff --git a/tests/test_memmove_null_dst.c b/tests/test_memmove_null_dst.c new file mode 100644 index 0000000..9455a5a --- /dev/null +++ b/tests/test_memmove_null_dst.c @@ -0,0 +1,16 @@ +#include "common.h" + +#include + +int main(int argc, char** argv) { +#ifndef __GNUC__ + char buffer[12] = {0}; + + CHK_FAIL_START + memmove(buffer, NULL, 0); + CHK_FAIL_END + + puts(buffer); +#endif + return ret; +} diff --git a/tests/test_memmove_null_src.c b/tests/test_memmove_null_src.c new file mode 100644 index 0000000..e03b97a --- /dev/null +++ b/tests/test_memmove_null_src.c @@ -0,0 +1,16 @@ +#include "common.h" + +#include + +int main(int argc, char** argv) { +#ifndef __GNUC__ + char buffer[12] = {0}; + + CHK_FAIL_START + memmove(NULL, buffer, 0); + CHK_FAIL_END + + puts(buffer); +#endif + return ret; +} diff --git a/tests/test_memset_null.c b/tests/test_memset_null.c new file mode 100644 index 0000000..0a2398b --- /dev/null +++ b/tests/test_memset_null.c @@ -0,0 +1,13 @@ +#include "common.h" + +#include + +int main(int argc, char** argv) { +#ifndef __GNUC__ + CHK_FAIL_START + memset(NULL, 0, 0); + CHK_FAIL_END +#endif + + return ret; +} -- cgit v1.3