[Piglit] [PATCH 1/3] shader_runner: Use strndup to get a null-terminated string for 'line'

Jordan Justen jordan.l.justen at intel.com
Mon May 25 12:42:01 PDT 2015


In order to use sscanf with optional parameters, the string needs to
be null terminated. Otherwise, sscanf will treat the newline as
whitespace and continue to look for matches on the following lines.

Signed-off-by: Jordan Justen <jordan.l.justen at intel.com>
---
 tests/shaders/shader_runner.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)

diff --git a/tests/shaders/shader_runner.c b/tests/shaders/shader_runner.c
index 1827e08..2df9e78 100644
--- a/tests/shaders/shader_runner.c
+++ b/tests/shaders/shader_runner.c
@@ -2294,7 +2294,7 @@ probe_atomic_counter(GLint counter_num, const char *op, uint32_t value)
 enum piglit_result
 piglit_display(void)
 {
-	const char *line;
+	const char *line, *next_line;
 	bool pass = true;
 	GLbitfield clear_bits = 0;
 	bool link_error_expected = false;
@@ -2303,14 +2303,23 @@ piglit_display(void)
 	if (test_start == NULL)
 		return PIGLIT_PASS;
 
-	line = test_start;
-	while (line[0] != '\0') {
+	next_line = test_start;
+	while (next_line[0] != '\0') {
 		float c[32];
 		double d[4];
 		int x, y, z, w, h, l, tex, level;
 		char s[32];
 
-		line = eat_whitespace(line);
+
+		line = eat_whitespace(next_line);
+
+		next_line = strchrnul(next_line, '\n');
+
+		if (next_line[0] != '\0')
+			next_line++;
+
+		/* Duplicate the line to make it null terminated */
+		line = strndup(line, next_line - line);
 
 		if (sscanf(line, "atomic counters %d", &x) == 1) {
 			GLuint *atomics_buf = calloc(x, sizeof(GLuint));
@@ -2701,9 +2710,7 @@ piglit_display(void)
 			piglit_report_result(PIGLIT_FAIL);
 		}
 
-		line = strchrnul(line, '\n');
-		if (line[0] != '\0')
-			line++;
+		free((void*) line);
 	}
 
 	if (!link_ok && !link_error_expected) {
-- 
2.1.4



More information about the Piglit mailing list