debug option (off by default), for BLI_string to help find incorrect sizes being passed in (enable in source files only)

This commit is contained in:
Campbell Barton
2013-07-15 03:54:57 +00:00
parent a91f51b67b
commit 02f5b0fc08
3 changed files with 39 additions and 9 deletions

View File

@@ -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

View File

@@ -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;

View File

@@ -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;
}