[PATCH wayland v2] scanner: Close input resource when done to prevent leak

Bryce Harrington bryce at osg.samsung.com
Thu Oct 1 18:13:29 PDT 2015


Addresses this error reported by Denis Denisov:

  [src/scanner.c:1415]: (error) Resource leak: input

Signed-off-by: Bryce Harrington <bryce at osg.samsung.com>

v2: Also close input for other exit points in main.
Signed-off-by: Bryce Harrington <bryce at osg.samsung.com>
---
 src/scanner.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/src/scanner.c b/src/scanner.c
index 9b41ae4..5504fce 100644
--- a/src/scanner.c
+++ b/src/scanner.c
@@ -1531,6 +1531,7 @@ int main(int argc, char *argv[])
 		if (freopen(argv[2], "w", stdout) == NULL) {
 			fprintf(stderr, "Could not open output file: %s\n",
 				strerror(errno));
+			fclose(input);
 			exit(EXIT_FAILURE);
 		}
 	}
@@ -1550,6 +1551,7 @@ int main(int argc, char *argv[])
 	XML_SetUserData(ctx.parser, &ctx);
 	if (ctx.parser == NULL) {
 		fprintf(stderr, "failed to create parser\n");
+		fclose(input);
 		exit(EXIT_FAILURE);
 	}
 
@@ -1561,6 +1563,7 @@ int main(int argc, char *argv[])
 		len = fread(buf, 1, XML_BUFFER_SIZE, input);
 		if (len < 0) {
 			fprintf(stderr, "fread: %m\n");
+			fclose(input);
 			exit(EXIT_FAILURE);
 		}
 		if (XML_ParseBuffer(ctx.parser, len, len == 0) == 0) {
@@ -1569,6 +1572,7 @@ int main(int argc, char *argv[])
 				XML_GetCurrentLineNumber(ctx.parser),
 				XML_GetCurrentColumnNumber(ctx.parser),
 				XML_ErrorString(XML_GetErrorCode(ctx.parser)));
+			fclose(input);
 			exit(EXIT_FAILURE);
 		}
 	} while (len > 0);
@@ -1588,6 +1592,7 @@ int main(int argc, char *argv[])
 	}
 
 	free_protocol(&protocol);
+	fclose(input);
 
 	return 0;
 }
-- 
1.9.1



More information about the wayland-devel mailing list