[Bf-blender-cvs] [79171b15e43] master: makesdna: enforce use of '_pad' naming convention

Campbell Barton noreply at git.blender.org
Wed Feb 27 05:33:34 CET 2019


Commit: 79171b15e4389702ec1d15af869a0a658a18a2b9
Author: Campbell Barton
Date:   Wed Feb 27 15:07:56 2019 +1100
Branches: master
https://developer.blender.org/rB79171b15e4389702ec1d15af869a0a658a18a2b9

makesdna: enforce use of '_pad' naming convention

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

M	source/blender/makesdna/intern/makesdna.c

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

diff --git a/source/blender/makesdna/intern/makesdna.c b/source/blender/makesdna/intern/makesdna.c
index 09600566436..5941c9b1b72 100644
--- a/source/blender/makesdna/intern/makesdna.c
+++ b/source/blender/makesdna/intern/makesdna.c
@@ -292,6 +292,48 @@ static const char *version_elem_static_from_alias(
 	return elem_alias_full;
 }
 
+/**
+ * Enforce '_pad123' naming convention, disallow 'pad123' or 'pad_123',
+ * special exception for [a-z] after since there is a 'pad_rot_angle' preference.
+ */
+static bool is_name_legal(const char *name)
+{
+	const int name_size = strlen(name) + 1;
+	char *name_strip = alloca(name_size);
+	DNA_elem_id_strip_copy(name_strip, name);
+
+	const char prefix[] = {'p', 'a', 'd'};
+
+	if (name[0] == '_') {
+		if (strncmp(&name_strip[1], prefix, sizeof(prefix)) != 0) {
+			fprintf(stderr, "Error: only '_pad' variables can start with an underscore, found '%s'\n", name);
+			return false;
+		}
+	}
+	else if (strncmp(name_strip, prefix, sizeof(prefix)) == 0) {
+		int i = sizeof(prefix);
+		if (name_strip[i] >= 'a' && name_strip[i] <= 'z') {
+			/* may be part of a word, allow that. */
+			return true;
+		}
+		bool has_only_digit_or_none = true;
+		for (; name_strip[i]; i++) {
+			const char c = name_strip[i];
+			if (!((c >= '0' && c <= '9') || c == '_')) {
+				has_only_digit_or_none = false;
+				break;
+			}
+		}
+		if (has_only_digit_or_none) {
+			/* found 'pad' or 'pad123'. */
+			fprintf(stderr, "Error: padding variables must be formatted '_pad[number]', found '%s'\n", name);
+			return false;
+		}
+	}
+	return true;
+}
+
+
 static int add_type(const char *str, int len)
 {
 	int nr;
@@ -464,7 +506,12 @@ static int add_name(const char *str)
 		}
 	}
 
-	/* append new type */
+	/* Sanity check the name. */
+	if (!is_name_legal(name)) {
+		return -1;
+	}
+
+	/* Append new name. */
 	const int name_size = strlen(name) + 1;
 	cp = BLI_memarena_alloc(mem_arena, name_size);
 	memcpy(cp, name, name_size);
@@ -729,6 +776,10 @@ static int convert_include(const char *filename)
 										md1[slen - 1] = 0;
 
 										name = add_name(version_elem_static_from_alias(strct, md1));
+										if (name == -1) {
+											fprintf(stderr, "File '%s' contains struct with name that can't be added \"%s\"\n", filename, md1);
+											return 1;
+										}
 										slen += additional_slen_offset;
 										sp[0] = type;
 										sp[1] = name;
@@ -745,6 +796,10 @@ static int convert_include(const char *filename)
 									}
 
 									name = add_name(version_elem_static_from_alias(strct, md1));
+									if (name == -1) {
+										fprintf(stderr, "File '%s' contains struct with name that can't be added \"%s\"\n", filename, md1);
+										return 1;
+									}
 									slen += additional_slen_offset;
 
 									sp[0] = type;



More information about the Bf-blender-cvs mailing list