[Bf-blender-cvs] [9e8bd3a072c] master: path_util: avoid overflow w/ strtoll use

Campbell Barton noreply at git.blender.org
Mon Jun 11 10:46:23 CEST 2018


Commit: 9e8bd3a072cff37fcc7808bdbefa1fb78ab94c22
Author: Campbell Barton
Date:   Mon Jun 11 10:43:34 2018 +0200
Branches: master
https://developer.blender.org/rB9e8bd3a072cff37fcc7808bdbefa1fb78ab94c22

path_util: avoid overflow w/ strtoll use

Also style cleanup.

===================================================================

M	source/blender/blenlib/intern/path_util.c

===================================================================

diff --git a/source/blender/blenlib/intern/path_util.c b/source/blender/blenlib/intern/path_util.c
index f59ef7fd39f..a3651de73a2 100644
--- a/source/blender/blenlib/intern/path_util.c
+++ b/source/blender/blenlib/intern/path_util.c
@@ -83,7 +83,7 @@ static bool BLI_path_is_abs(const char *name);
  * \param tail  Optional area to return copy of part of string following digits, or from dot if no digits.
  * \param numlen  Optional to return number of digits found.
  */
-int BLI_stringdec(const char *string, char *head, char *tail, unsigned short *numlen)
+int BLI_stringdec(const char *string, char *head, char *tail, ushort *r_num_len)
 {
 	uint nums = 0, nume = 0;
 	int i;
@@ -93,8 +93,12 @@ int BLI_stringdec(const char *string, char *head, char *tail, unsigned short *nu
 	const uint lslash_len = lslash != NULL ? (int)(lslash - string) : 0;
 	uint name_end = string_len;
 
-	while (name_end > lslash_len && string[--name_end] != '.') {} /* name ends at dot if present */
-	if (name_end == lslash_len && string[name_end] != '.') name_end = string_len;
+	while (name_end > lslash_len && string[--name_end] != '.') {
+		/* name ends at dot if present */
+	}
+	if (name_end == lslash_len && string[name_end] != '.') {
+		name_end = string_len;
+	}
 
 	for (i = name_end - 1; i >= (int)lslash_len; i--) {
 		if (isdigit(string[i])) {
@@ -113,28 +117,34 @@ int BLI_stringdec(const char *string, char *head, char *tail, unsigned short *nu
 	}
 
 	if (found_digit) {
-		char *ptr;
-		long ret;
-		ret = strtoll(&(string[nums]), &ptr, 10);
+		const long long int ret = strtoll(&(string[nums]), NULL, 10);
 		if (ret >= INT_MIN && ret <= INT_MAX) {
-			if (tail) strcpy(tail, &string[nume + 1]);
+			if (tail) {
+				strcpy(tail, &string[nume + 1]);
+			}
 			if (head) {
 				strcpy(head, string);
 				head[nums] = 0;
 			}
-			if (numlen) *numlen = nume - nums + 1;
-			return ((int)ret);
+			if (r_num_len) {
+				*r_num_len = nume - nums + 1;
+			}
+			return (int)ret;
 		}
 	}
 
-	if (tail) strcpy(tail, string + name_end);
+	if (tail) {
+		strcpy(tail, string + name_end);
+	}
 	if (head) {
 		/* name_end points to last character of head,
 		 * make it +1 so null-terminator is nicely placed
 		 */
 		BLI_strncpy(head, string, name_end + 1);
 	}
-	if (numlen) *numlen = 0;
+	if (r_num_len) {
+		*r_num_len = 0;
+	}
 	return 0;
 }



More information about the Bf-blender-cvs mailing list