From e1a357418bad730b2fc5e54975ac2e54183542bc Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Sun, 12 Dec 2021 19:15:51 +0500 Subject: [PATCH] Make "kernaux_memset" signature compatible with C stdlib "memset" --- include/kernaux/stdlib.h | 8 +++----- src/stdlib.c | 9 ++++----- tests/test_stdlib.c | 15 +++++++++++++-- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/include/kernaux/stdlib.h b/include/kernaux/stdlib.h index 91e7d4f..f7fc293 100644 --- a/include/kernaux/stdlib.h +++ b/include/kernaux/stdlib.h @@ -1,15 +1,13 @@ #ifndef KERNAUX_INCLUDED_STDLIB #define KERNAUX_INCLUDED_STDLIB 1 +#include + #ifdef __cplusplus extern "C" { #endif -void kernaux_memset( - void *buffer, - unsigned char value, - unsigned long size -) +void *kernaux_memset(void *buffer, int value, size_t size) __attribute__((nonnull)); unsigned int kernaux_strlen(const char *s) __attribute__((nonnull)); diff --git a/src/stdlib.c b/src/stdlib.c index 879671e..caa76fd 100644 --- a/src/stdlib.c +++ b/src/stdlib.c @@ -4,16 +4,15 @@ #include -void kernaux_memset( - void *const buffer, - const unsigned char value, - const unsigned long size -) { +void *kernaux_memset(void *const buffer, const int value, const size_t size) +{ unsigned char *const s = buffer; for (unsigned long i = 0; i < size; ++i) { s[i] = value; } + + return buffer; } unsigned int kernaux_strlen(const char *const s) diff --git a/tests/test_stdlib.c b/tests/test_stdlib.c index 981e458..34a6b40 100644 --- a/tests/test_stdlib.c +++ b/tests/test_stdlib.c @@ -14,7 +14,7 @@ int main() unsigned char buffer[10] = {32, 13, 254, 165, 98, 36, 169, 152, 233, 222}; - kernaux_memset(buffer, 0, sizeof(buffer)); + assert(kernaux_memset(buffer, 0, sizeof(buffer)) == buffer); for (unsigned int i = 0; i < sizeof(buffer); ++i) { assert(buffer[i] == 0); @@ -25,13 +25,24 @@ int main() unsigned char buffer[10] = {32, 13, 254, 165, 98, 36, 169, 152, 233, 222}; - kernaux_memset(buffer, 143, sizeof(buffer)); + assert(kernaux_memset(buffer, 143, sizeof(buffer)) == buffer); for (unsigned int i = 0; i < sizeof(buffer); ++i) { assert(buffer[i] == 143); } } + { + unsigned char buffer[10] = + {32, 13, 254, 165, 98, 36, 169, 152, 233, 222}; + + assert(kernaux_memset(buffer, 0x89ABCDEF, sizeof(buffer)) == buffer); + + for (unsigned int i = 0; i < sizeof(buffer); ++i) { + assert(buffer[i] == 0xEF); + } + } + // kernaux_strlen assert(kernaux_strlen("") == 0);