Skip to content

Commit

Permalink
Merge branch 'avalluri-leaks'
Browse files Browse the repository at this point in the history
* avalluri-leaks:
  whitespace fix
  memory leak improvements - delete at end of scope
  CCache: Fix typo in null check
  CCache: Fix memory/file descriptor leaks
  scilab.cxx: Fix memory leaks
  Fix leaked file descriptor
  Lua: Fix possible memory leaks
  go.cxx: Fix use of a freed variable

Closes swig#710
Closes swig#712
Closes swig#713
Closes swig#715
Closes swig#716
  • Loading branch information
wsfulton committed Jun 25, 2016
2 parents 875aca5 + adfa531 commit 2da7e06
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 6 deletions.
13 changes: 12 additions & 1 deletion CCache/ccache.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ static void failed(void)
exit(1);
}
args_add_prefix(orig_args, p);
free(p);
}

if (ccache_verbose) {
Expand Down Expand Up @@ -490,7 +491,9 @@ static void find_hash(ARGS *args)
/* also include the hash of the compiler name - as some compilers
use hard links and behave differently depending on the real name */
if (st.st_nlink > 1) {
hash_string(str_basename(args->argv[0]));
char *path = str_basename(args->argv[0]);
hash_string(path);
free(path);
}

hash_int(st.st_size);
Expand Down Expand Up @@ -523,6 +526,7 @@ static void find_hash(ARGS *args)
input_base, tmp_string(),
i_extension);
x_asprintf(&path_stderr, "%s/tmp.cpp_stderr.%s", temp_dir, tmp_string());
free(input_base);

if (!direct_i_file) {
/* run cpp on the input file to obtain the .i */
Expand Down Expand Up @@ -781,6 +785,7 @@ static void find_compiler(int argc, char **argv)

/* support user override of the compiler */
if ((path=getenv("CCACHE_CC"))) {
free(base);
base = x_strdup(path);
}

Expand All @@ -791,8 +796,10 @@ static void find_compiler(int argc, char **argv)
stats_update(STATS_COMPILER);
cc_log("could not find compiler (%s)\n", base);
perror(base);
free(base);
exit(1);
}
free(base);
}


Expand Down Expand Up @@ -1076,6 +1083,7 @@ static void process_args(int argc, char **argv)
if (strlen(p) < 2) {
cc_log("badly formed dependency file %s\n", output_file);
stats_update(STATS_ARGS);
free(default_depfile_name);
failed();
return;
}
Expand All @@ -1093,6 +1101,7 @@ static void process_args(int argc, char **argv)
strcat(default_depfile_name, ".d");
args_add(stripped_args, "-MF");
args_add(stripped_args, default_depfile_name);
free(default_depfile_name);
}

if (!dependency_target_specified) {
Expand All @@ -1117,6 +1126,7 @@ static void process_args(int argc, char **argv)
exit(1);
}
args_add_prefix(stripped_args, p);
free(p);
}
}

Expand Down Expand Up @@ -1305,6 +1315,7 @@ static void setup_uncached_err(void)

if (putenv(buf) == -1) {
cc_log("putenv failed\n");
close(uncached_fd);
stats_update(STATS_ERROR);
failed();
}
Expand Down
1 change: 1 addition & 0 deletions CCache/execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ char *find_executable(const char *name, const char *exclude_name)
}
free(fname);
}
free(path);

return NULL;
#endif
Expand Down
5 changes: 4 additions & 1 deletion CCache/stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,10 @@ static void stats_update_size(enum stats stat, size_t size, size_t numfiles)

memset(counters, 0, sizeof(counters));

if (lock_fd(fd) != 0) return;
if (lock_fd(fd) != 0) {
close(fd);
return;
}

/* read in the old stats */
stats_read_fd(fd, counters);
Expand Down
3 changes: 2 additions & 1 deletion CCache/unify.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ int unify_hash(const char *fname)
fd = open(fname, O_RDONLY|O_BINARY);
if (fd == -1 || fstat(fd, &st) != 0) {
cc_log("Failed to open preprocessor output %s\n", fname);
if (fd != -1) close(fd);
stats_update(STATS_PREPROCESSOR);
return -1;
}
Expand All @@ -289,12 +290,12 @@ int unify_hash(const char *fname)
lines in preprocessor output. I have seen lines of over
100k in length, so this is well worth it */
map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
close(fd);
if (map == (char *)-1) {
cc_log("Failed to mmap %s\n", fname);
stats_update(STATS_PREPROCESSOR);
return -1;
}
close(fd);

/* pass it through the unifier */
unify((unsigned char *)map, st.st_size);
Expand Down
9 changes: 7 additions & 2 deletions CCache/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,11 @@ void copy_fd(int fd_in, int fd_out) {

while ((n = gzread(gz_in, buf, sizeof(buf))) > 0) {
if (write(fd_out, buf, n) != n) {
gzclose(gz_in);
fatal("Failed to copy fd");
}
}
gzclose(gz_in);
}

static int _copy_file(const char *src, const char *dest, int mode) {
Expand Down Expand Up @@ -248,9 +250,11 @@ static int _copy_file(const char *src, const char *dest, int mode) {
}

if (mode == COPY_TO_CACHE) {
gz_out = gzdopen(dup(fd_out), "wb");
int dup_fd_out = dup(fd_out);
gz_out = gzdopen(dup_fd_out, "wb");
if (!gz_out) {
gzclose(gz_in);
close(dup_fd_out);
close(fd_out);
free(tmp_name);
return -1;
Expand Down Expand Up @@ -459,6 +463,7 @@ int create_cachedirtag(const char *dir)
f = fopen(filename, "w");
if (!f) goto error;
if (fwrite(CACHEDIR_TAG, sizeof(CACHEDIR_TAG)-1, 1, f) != 1) {
fclose(f);
goto error;
}
if (fclose(f)) goto error;
Expand All @@ -485,7 +490,7 @@ void x_asprintf(char **ptr, const char *format, ...)
}
va_end(ap);

if (!ptr) fatal("out of memory in x_asprintf");
if (!*ptr) fatal("out of memory in x_asprintf");
}

/*
Expand Down
3 changes: 2 additions & 1 deletion Source/Modules/go.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -4176,7 +4176,6 @@ class GO:public Language {

Wrapper *dummy = NewWrapper();
emit_attach_parmmaps(parms, dummy);
DelWrapper(dummy);

Swig_typemap_attach_parms("gotype", parms, NULL);
Swig_typemap_attach_parms("imtype", parms, NULL);
Expand Down Expand Up @@ -4233,6 +4232,8 @@ class GO:public Language {
Swig_typemap_attach_parms("goin", parms, dummy);
Swig_typemap_attach_parms("goargout", parms, dummy);

DelWrapper(dummy);

if (!is_ignored) {
// We use an interface to see if this method is defined in Go.
Printv(f_go_wrappers, "type ", interface_name, " interface {\n", NULL);
Expand Down
3 changes: 3 additions & 0 deletions Source/Modules/lua.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,9 @@ class LUA:public Language {
//Printf(stdout,"Swig_overload_dispatch %s %s '%s' %d\n",symname,wname,dispatch,maxargs);

if (!luaAddSymbol(lua_name, n)) {
DelWrapper(f);
Delete(dispatch);
Delete(tmp);
return SWIG_ERROR;
}

Expand Down
4 changes: 4 additions & 0 deletions Source/Modules/scilab.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ class SCILAB:public Language {
bool isLastOverloaded = isOverloaded && !Getattr(node, "sym:nextSibling");

if (!isOverloaded && !addSymbol(functionName, node)) {
DelWrapper(wrapper);
return SWIG_ERROR;
}

Expand Down Expand Up @@ -633,7 +634,10 @@ class SCILAB:public Language {

/* Add function to builder table */
addFunctionToScilab(scilabSetFunctionName, setFunctionName);

DelWrapper(setFunctionWrapper);
}
DelWrapper(getFunctionWrapper);

return SWIG_OK;
}
Expand Down
1 change: 1 addition & 0 deletions Source/Swig/include.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ int Swig_insert_file(const_String_or_char_ptr filename, File *outfile) {
while ((nbytes = Read(f, buffer, 4096)) > 0) {
Write(outfile, buffer, nbytes);
}
fclose(f);
return 0;
}

Expand Down

0 comments on commit 2da7e06

Please sign in to comment.