[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [58252] trunk/blender/source/blender/ blenlib: debug option (off by default), for BLI_string to help find incorrect sizes being passed in ( enable in source files only)

Campbell Barton ideasman42 at gmail.com
Mon Jul 15 05:55:03 CEST 2013


Revision: 58252
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=58252
Author:   campbellbarton
Date:     2013-07-15 03:54:57 +0000 (Mon, 15 Jul 2013)
Log Message:
-----------
debug option (off by default), for BLI_string to help find incorrect sizes being passed in (enable in source files only)

Modified Paths:
--------------
    trunk/blender/source/blender/blenlib/BLI_string.h
    trunk/blender/source/blender/blenlib/intern/string.c
    trunk/blender/source/blender/blenlib/intern/string_utf8.c

Modified: trunk/blender/source/blender/blenlib/BLI_string.h
===================================================================
--- trunk/blender/source/blender/blenlib/BLI_string.h	2013-07-15 02:03:31 UTC (rev 58251)
+++ trunk/blender/source/blender/blenlib/BLI_string.h	2013-07-15 03:54:57 UTC (rev 58252)
@@ -93,14 +93,14 @@
 #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

Modified: trunk/blender/source/blender/blenlib/intern/string.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/string.c	2013-07-15 02:03:31 UTC (rev 58251)
+++ trunk/blender/source/blender/blenlib/intern/string.c	2013-07-15 03:54:57 UTC (rev 58252)
@@ -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 @@
 	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 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 @@
 /**
  * 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 @@
 /**
  * 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;

Modified: trunk/blender/source/blender/blenlib/intern/string_utf8.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/string_utf8.c	2013-07-15 02:03:31 UTC (rev 58251)
+++ trunk/blender/source/blender/blenlib/intern/string_utf8.c	2013-07-15 03:54:57 UTC (rev 58252)
@@ -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_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 @@
 
 	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 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;
 	}




More information about the Bf-blender-cvs mailing list