[ccan] [PATCH 1/3] configurator: avoid leaks that LeakSanitizer doesn't like and hide type punning

Cody P Schafer dev at codyps.com
Mon Aug 17 08:54:37 AEST 2015


These leaks aren't really an issue since they are completely bounded,
but if one is building with leak sanitizer enabled (as
-fsanitize=address does in gcc-5.1), it kills the configurator, which
isn't very useful for us. Add the few free() calls it's looking for.

As for the type punning: gcc-5.1 (at least) warns about type punning in
the previous example. The new usage should be exactly equivalent to the
old, but just seperates the cast and deref into 2 statements. Frankly,
I'm suprised gcc's type-punning analysis is so limited.

Neither of these things are actual code issues, they just workaround
some optional compiler peculiarities.

Signed-off-by: Cody P Schafer <dev at codyps.com>
---
 tools/configurator/configurator.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/tools/configurator/configurator.c b/tools/configurator/configurator.c
index f4edb8e..119404f 100644
--- a/tools/configurator/configurator.c
+++ b/tools/configurator/configurator.c
@@ -282,7 +282,8 @@ static struct test tests[] = {
 	  "int main(int argc, char *argv[]) {\n"
 	  "     char pad[sizeof(int *) * 1];\n"
 	  "	strncpy(pad, argv[0], sizeof(pad));\n"
-	  "	return *(int *)(pad) == *(int *)(pad + 1);\n"
+	  "	int *x = (int *)pad, *y = (int *)(pad + 1);\n"
+	  "	return *x == *y;\n"
 	  "}\n" },
 	{ "HAVE_UTIME", DEFINES_FUNC, NULL, NULL,
 	  "#include <sys/types.h>\n"
@@ -430,6 +431,9 @@ static bool run_test(const char *cmd, struct test *test)
 				test->done = true;
 				return test->answer;
 			}
+			if (deps[len])
+				free(dep);
+
 			deps += len;
 			deps += strspn(deps, " ");
 		}
@@ -549,6 +553,7 @@ int main(int argc, const char *argv[])
 	cmd = connect_args(argv, " -o " OUTPUT_FILE " " INPUT_FILE);
 	for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
 		run_test(cmd, &tests[i]);
+	free(cmd);
 
 	unlink(OUTPUT_FILE);
 	unlink(INPUT_FILE);
@@ -560,7 +565,9 @@ int main(int argc, const char *argv[])
 	printf("#define _GNU_SOURCE /* Always use GNU extensions. */\n");
 	printf("#endif\n");
 	printf("#define CCAN_COMPILER \"%s\"\n", argv[1]);
-	printf("#define CCAN_CFLAGS \"%s\"\n\n", connect_args(argv+1, ""));
+	cmd = connect_args(argv+1, "");
+	printf("#define CCAN_CFLAGS \"%s\"\n\n", cmd);
+	free(cmd);
 	/* This one implies "#include <ccan/..." works, eg. for tdb2.h */
 	printf("#define HAVE_CCAN 1\n");
 	for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
-- 
2.5.0



More information about the ccan mailing list