[cairo] [PATCH v2 2/2] test: Fix issues reported by cppcheck static analysis tool

Ravi Nanjundappa nravi.n at samsung.com
Thu Aug 21 04:59:02 PDT 2014


cppcheck analysis tool reports the following issues when run on the
latest Cairo source.

$ grep "(error)"  cppcheck_error_log.txt
[test/pdf-mime-data.c:58]: (error) Possible null pointer dereference: file - otherwise it is redundant to check if file is null at line 53
[test/pdf-mime-data.c:75]: (error) Resource leak: fp
$

The proposed changes fixes the above issues.

Signed-off-by: Ravi Nanjundappa <nravi.n at samsung.com>
Reviewed-by: Chris Wilson <chris at chris-wilson.co.uk>
---
 test/pdf-mime-data.c |   10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/test/pdf-mime-data.c b/test/pdf-mime-data.c
index e2c529e..dba72fd 100644
--- a/test/pdf-mime-data.c
+++ b/test/pdf-mime-data.c
@@ -50,7 +50,7 @@ read_file (const cairo_test_context_t *ctx,
     FILE *fp;
 
     fp = fopen (file, "rb");
-    if (file == NULL) {
+    if (fp == NULL) {
 	char filename[4096];
 
 	/* try again with srcdir */
@@ -71,11 +71,15 @@ read_file (const cairo_test_context_t *ctx,
     *len = ftell(fp);
     fseek (fp, 0, SEEK_SET);
     *data = malloc (*len);
-    if (*data == NULL)
+    if (*data == NULL) {
+	fclose(fp);
 	return CAIRO_TEST_NO_MEMORY;
+    }
 
-    if (fread(*data, *len, 1, fp) != 1)
+    if (fread(*data, *len, 1, fp) != 1) {
+	fclose(fp);
 	return CAIRO_TEST_FAILURE;
+    }
 
     fclose(fp);
     return CAIRO_TEST_SUCCESS;
-- 
1.7.9.5



More information about the cairo mailing list