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

Ravi Nanjundappa <[email protected]>
Newsgroups gmane.comp.lib.cairo
Message-ID <[email protected]>
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 <[email protected]>
Reviewed-by: Chris Wilson <[email protected]>
---
 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

-- 
cairo mailing list
[email protected]
http://lists.cairographics.org/mailman/listinfo/cairo
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.