[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [51780] trunk/blender/source/blender/ blenlib: add assert if zero is passed to string copy functions, would copy into first byte anyway.

Campbell Barton ideasman42 at gmail.com
Wed Oct 31 05:28:51 CET 2012


Revision: 51780
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=51780
Author:   campbellbarton
Date:     2012-10-31 04:28:49 +0000 (Wed, 31 Oct 2012)
Log Message:
-----------
add assert if zero is passed to string copy functions, would copy into first byte anyway.

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	2012-10-31 04:24:55 UTC (rev 51779)
+++ trunk/blender/source/blender/blenlib/BLI_string.h	2012-10-31 04:28:49 UTC (rev 51780)
@@ -162,7 +162,7 @@
 #endif
 ;
 
-size_t BLI_strescape(char *__restrict dst, const char *__restrict src, const size_t maxlen)
+size_t BLI_strescape(char *__restrict dst, const char *__restrict src, const size_t maxncpy)
 #ifdef __GNUC__
 __attribute__((nonnull))
 #endif

Modified: trunk/blender/source/blender/blenlib/intern/string.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/string.c	2012-10-31 04:24:55 UTC (rev 51779)
+++ trunk/blender/source/blender/blenlib/intern/string.c	2012-10-31 04:28:49 UTC (rev 51780)
@@ -41,6 +41,8 @@
 #include "BLI_dynstr.h"
 #include "BLI_string.h"
 
+#include "BLI_utildefines.h"
+
 char *BLI_strdupn(const char *str, const size_t len)
 {
 	char *n = MEM_mallocN(len + 1, "strdup");
@@ -71,6 +73,7 @@
 {
 	size_t srclen = strlen(src);
 	size_t cpylen = (srclen > (maxncpy - 1)) ? (maxncpy - 1) : srclen;
+	BLI_assert(maxncpy != 0);
 	
 	memcpy(dst, src, cpylen);
 	dst[cpylen] = '\0';
@@ -130,10 +133,13 @@
  * TODO: support more fancy string escaping. current code is primitive
  *    this basically is an ascii version of PyUnicode_EncodeUnicodeEscape()
  *    which is a useful reference. */
-size_t BLI_strescape(char *dst, const char *src, const size_t maxlen)
+size_t BLI_strescape(char *dst, const char *src, const size_t maxncpy)
 {
 	size_t len = 0;
-	while (len < maxlen) {
+
+	BLI_assert(maxncpy != 0);
+
+	while (len < maxncpy) {
 		switch (*src) {
 			case '\0':
 				goto escape_finish;
@@ -144,7 +150,7 @@
 			case '\t':
 			case '\n':
 			case '\r':
-				if (len + 1 < maxlen) {
+				if (len + 1 < maxncpy) {
 					*dst++ = '\\';
 					len++;
 				}
@@ -439,4 +445,3 @@
 		if (str[i] >= 'a' && str[i] <= 'z')
 			str[i] -= 'a' - 'A';
 }
-

Modified: trunk/blender/source/blender/blenlib/intern/string_utf8.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/string_utf8.c	2012-10-31 04:24:55 UTC (rev 51779)
+++ trunk/blender/source/blender/blenlib/intern/string_utf8.c	2012-10-31 04:28:49 UTC (rev 51780)
@@ -33,9 +33,13 @@
 #include <string.h>
 #include <wchar.h>
 #include <wctype.h>
+#include <stdio.h>
+#include <stdlib.h>
 
-#include "BLI_string_utf8.h"
+#include "BLI_utildefines.h"
 
+#include "BLI_string_utf8.h"  /* own include */
+
 /* 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 */




More information about the Bf-blender-cvs mailing list