From 229b80a004b1d5498f967f53aa258c7c8944b808 Mon Sep 17 00:00:00 2001 From: George Koehler Date: Mon, 13 Nov 2017 21:34:31 -0500 Subject: [PATCH] Free buf in GetFile(). aprintf() returns a const char *; the assignment to char * caused both clang and gcc to warn of the dropped const. Commit 893471a introduced a tiny memory leak, because GetFile() stopped freeing buf. The const return type of aprintf() suggests that the buffer must not be freed. Now use Malloc() to allocate the buffer and free() to free it. This also checks if we are out of memory, because Malloc() does the check and aprintf() currently doesn't. --- lang/m2/comp/defmodule.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/lang/m2/comp/defmodule.c b/lang/m2/comp/defmodule.c index 0ecb1dd2a..b778d7cb2 100644 --- a/lang/m2/comp/defmodule.c +++ b/lang/m2/comp/defmodule.c @@ -17,7 +17,7 @@ #include #include #include -#include +#include #include "LLlex.h" #include "Lpars.h" @@ -59,15 +59,24 @@ char* return ""; } -STATIC +STATIC int GetFile(name) char* name; { /* Try to find a file with basename "name" and extension ".def", in the directories mentioned in "DEFPATH". */ - char* buf = aprintf("%s.def", name); + size_t len; + int found; + char *buf; + + len = strlen(name); + buf = Malloc(len + 5); + memcpy(buf, name, len); + memcpy(buf + len, ".def", 5); DEFPATH[0] = WorkingDir; - if (!InsertFile(buf, DEFPATH, &(FileName))) + found = InsertFile(buf, DEFPATH, &(FileName)); + free(buf); + if (!found) { error("could not find a DEFINITION MODULE for \"%s\"", name); return 0;