From cd6121019bc4f18f82050b2c28fb7e6cd4b532a8 Mon Sep 17 00:00:00 2001 From: wael Date: Sat, 5 Mar 2022 16:52:37 +0200 Subject: [PATCH] Update safe string copy for better debug flow and fix filename typo. --- strings/{strzcpy.c => strscpy.c} | 3 +++ strings/strscpy.h | 7 +++++++ 2 files changed, 10 insertions(+) rename strings/{strzcpy.c => strscpy.c} (97%) diff --git a/strings/strzcpy.c b/strings/strscpy.c similarity index 97% rename from strings/strzcpy.c rename to strings/strscpy.c index 71c64ad..ed279b3 100644 --- a/strings/strzcpy.c +++ b/strings/strscpy.c @@ -29,6 +29,9 @@ * Will return the length copied (and whether it overflowed or not). */ char *strscpy(char *restrict dest, const char* restrict src, size_t n) { + /* Sanity check. */ + assert(dest && src); + /* Copy using memcpy. */ char *last = memccpy(dest, src, NULL_CHAR, n); diff --git a/strings/strscpy.h b/strings/strscpy.h index b7a8557..f0d0594 100644 --- a/strings/strscpy.h +++ b/strings/strscpy.h @@ -20,6 +20,13 @@ #define STRSCPY_H /* Include string.h */ #include +/* Include assert.h to allow code generation, but only run with debug flag. */ +#ifdef DEBUG +#ifndef NDEBUG +#define NDEBUG +#endif /* NDEBUG. */ +#endif /* DEBUG. */ +#include /* Constants. */ #define NULL_CHAR '\0'