From 02f5b0fc08e979dab65a7bc1ee0383098ac99b2e Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Mon, 15 Jul 2013 03:54:57 +0000 Subject: debug option (off by default), for BLI_string to help find incorrect sizes being passed in (enable in source files only) --- source/blender/blenlib/BLI_string.h | 4 ++-- source/blender/blenlib/intern/string.c | 28 +++++++++++++++++++++------- source/blender/blenlib/intern/string_utf8.c | 16 ++++++++++++++++ 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/source/blender/blenlib/BLI_string.h b/source/blender/blenlib/BLI_string.h index c6f0bc49c9b..02eb0734f8c 100644 --- a/source/blender/blenlib/BLI_string.h +++ b/source/blender/blenlib/BLI_string.h @@ -93,14 +93,14 @@ __attribute__((nonnull)) #endif ; -size_t BLI_snprintf(char *__restrict buffer, size_t len, const char *__restrict format, ...) +size_t BLI_snprintf(char *__restrict dst, size_t maxncpy, const char *__restrict format, ...) #ifdef __GNUC__ __attribute__ ((format(printf, 3, 4))) __attribute__((nonnull)) #endif ; -size_t BLI_vsnprintf(char *__restrict buffer, size_t count, const char *__restrict format, va_list arg) +size_t BLI_vsnprintf(char *__restrict dst, size_t maxncpy, const char *__restrict format, va_list arg) #ifdef __GNUC__ __attribute__ ((format(printf, 3, 0))) #endif diff --git a/source/blender/blenlib/intern/string.c b/source/blender/blenlib/intern/string.c index 03bed428c07..24e14d04c68 100644 --- a/source/blender/blenlib/intern/string.c +++ b/source/blender/blenlib/intern/string.c @@ -47,6 +47,8 @@ # pragma GCC diagnostic error "-Wsign-conversion" #endif +// #define DEBUG_STRSIZE + /** * Duplicates the first \a len bytes of cstring \a str * into a newly mallocN'd string and returns it. \a str @@ -111,6 +113,10 @@ char *BLI_strncpy(char *__restrict dst, const char *__restrict src, const size_t size_t srclen = BLI_strnlen(src, maxncpy - 1); BLI_assert(maxncpy != 0); +#ifdef DEBUG_STRSIZE + memset(dst, 0xff, sizeof(*dst) * maxncpy); +#endif + memcpy(dst, src, srclen); dst[srclen] = '\0'; return dst; @@ -134,6 +140,10 @@ size_t BLI_strncpy_rlen(char *__restrict dst, const char *__restrict src, const size_t srclen = BLI_strnlen(src, maxncpy - 1); BLI_assert(maxncpy != 0); +#ifdef DEBUG_STRSIZE + memset(dst, 0xff, sizeof(*dst) * maxncpy); +#endif + memcpy(dst, src, srclen); dst[srclen] = '\0'; return srclen; @@ -149,21 +159,21 @@ size_t BLI_strcpy_rlen(char *__restrict dst, const char *__restrict src) /** * Portable replacement for #vsnprintf */ -size_t BLI_vsnprintf(char *__restrict buffer, size_t count, const char *__restrict format, va_list arg) +size_t BLI_vsnprintf(char *__restrict buffer, size_t maxncpy, const char *__restrict format, va_list arg) { size_t n; BLI_assert(buffer != NULL); - BLI_assert(count > 0); + BLI_assert(maxncpy > 0); BLI_assert(format != NULL); - n = (size_t)vsnprintf(buffer, count, format, arg); + n = (size_t)vsnprintf(buffer, maxncpy, format, arg); - if (n != -1 && n < count) { + if (n != -1 && n < maxncpy) { buffer[n] = '\0'; } else { - buffer[count - 1] = '\0'; + buffer[maxncpy - 1] = '\0'; } return n; @@ -172,13 +182,17 @@ size_t BLI_vsnprintf(char *__restrict buffer, size_t count, const char *__restri /** * Portable replacement for #snprintf */ -size_t BLI_snprintf(char *__restrict buffer, size_t count, const char *__restrict format, ...) +size_t BLI_snprintf(char *__restrict dst, size_t maxncpy, const char *__restrict format, ...) { size_t n; va_list arg; +#ifdef DEBUG_STRSIZE + memset(dst, 0xff, sizeof(*dst) * maxncpy); +#endif + va_start(arg, format); - n = BLI_vsnprintf(buffer, count, format, arg); + n = BLI_vsnprintf(dst, maxncpy, format, arg); va_end(arg); return n; diff --git a/source/blender/blenlib/intern/string_utf8.c b/source/blender/blenlib/intern/string_utf8.c index ab0073a7585..edfcdf145eb 100644 --- a/source/blender/blenlib/intern/string_utf8.c +++ b/source/blender/blenlib/intern/string_utf8.c @@ -45,6 +45,8 @@ # pragma GCC diagnostic error "-Wsign-conversion" #endif +// #define DEBUG_STRSIZE + /* from libswish3, originally called u8_isvalid(), * modified to return the index of the bad character (byte index not utf). * http://svn.swish-e.org/libswish3/trunk/src/libswish3/utf8.c r3044 - campbell */ @@ -203,6 +205,10 @@ char *BLI_strncpy_utf8(char *__restrict dst, const char *__restrict src, size_t char *BLI_strncat_utf8(char *__restrict dst, const char *__restrict src, size_t maxncpy) { +#ifdef DEBUG_STRSIZE + memset(dst, 0xff, sizeof(*dst) * maxncpy); +#endif + while (*dst && maxncpy > 0) { dst++; maxncpy--; @@ -224,6 +230,10 @@ size_t BLI_strncpy_wchar_as_utf8(char *__restrict dst, const wchar_t *__restrict BLI_assert(maxncpy != 0); +#ifdef DEBUG_STRSIZE + memset(dst, 0xff, sizeof(*dst) * maxncpy); +#endif + while (*src && len < maxncpy) { /* XXX can still run over the buffer because utf8 size isn't known :| */ len += BLI_str_utf8_from_unicode((unsigned int)*src++, dst + len); } @@ -302,6 +312,12 @@ size_t BLI_strncpy_wchar_from_utf8(wchar_t *__restrict dst_w, const char *__rest { size_t len = 0; + BLI_assert(maxncpy != 0); + +#ifdef DEBUG_STRSIZE + memset(dst_w, 0xff, sizeof(*dst_w) * maxncpy); +#endif + if (dst_w == NULL || src_c == NULL) { return 0; } -- cgit v1.2.3