[Piglit] [PATCH piglit] util: avoid leaking memory when caller doesn't ask for it

Eric Engestrom eric.engestrom at intel.com
Thu Aug 23 14:23:55 UTC 2018


It doesn't happen anywhere right now, but a caller could say it doesn't
want the source, only its size, and in that case we would just leak that
memory.
Let's only actually allocate it when the caller wants it and will take
ownership of that memory.

Suggested-by: Caio Marcelo de Oliveira Filho <caio.oliveira at intel.com>
Signed-off-by: Eric Engestrom <eric.engestrom at intel.com>
---
 tests/util/piglit-shader-test.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/tests/util/piglit-shader-test.c b/tests/util/piglit-shader-test.c
index 4802983e56d1037be079..f11ee8ab5383080ae090 100644
--- a/tests/util/piglit-shader-test.c
+++ b/tests/util/piglit-shader-test.c
@@ -101,7 +101,6 @@ piglit_load_source_from_shader_test(const char *filename,
 				    unsigned *output_source_size)
 {
 	char group_name[4096];
-	char *source = NULL;
 	unsigned text_size;
 	char *line = NULL;
 	char *first_line = NULL;
@@ -142,11 +141,11 @@ piglit_load_source_from_shader_test(const char *filename,
 	}
 
 	text_size = line - first_line + 1;
-	source = malloc(sizeof(char*) * text_size);
-	snprintf(source, line - first_line + 1, "%s", first_line);
 
-	if (output_source)
-		*output_source = source;
+	if (output_source) {
+		*output_source = malloc(sizeof(char*) * text_size);
+		snprintf(*output_source, line - first_line + 1, "%s", first_line);
+	}
 
 	if (output_source_size)
 		*output_source_size = text_size;
-- 
Cheers,
  Eric



More information about the Piglit mailing list