Commit a2ba34d3 authored by Scott Lawrence's avatar Scott Lawrence Committed by Russ Cox

ld: fix memory leaks

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/5434068
parent 0c64972d
...@@ -235,7 +235,7 @@ loadpkgdata(char *file, char *pkg, char *data, int len) ...@@ -235,7 +235,7 @@ loadpkgdata(char *file, char *pkg, char *data, int len)
x = ilookup(name); x = ilookup(name);
if(x->prefix == nil) { if(x->prefix == nil) {
x->prefix = prefix; x->prefix = prefix;
x->def = def; x->def = strdup(def);
x->file = file; x->file = file;
} else if(strcmp(x->prefix, prefix) != 0) { } else if(strcmp(x->prefix, prefix) != 0) {
fprint(2, "%s: conflicting definitions for %s\n", argv0, name); fprint(2, "%s: conflicting definitions for %s\n", argv0, name);
...@@ -248,7 +248,10 @@ loadpkgdata(char *file, char *pkg, char *data, int len) ...@@ -248,7 +248,10 @@ loadpkgdata(char *file, char *pkg, char *data, int len)
fprint(2, "%s:\t%s %s %s\n", file, prefix, name, def); fprint(2, "%s:\t%s %s %s\n", file, prefix, name, def);
nerrors++; nerrors++;
} }
free(name);
free(def);
} }
free(file);
} }
// replace all "". with pkg. // replace all "". with pkg.
...@@ -264,7 +267,7 @@ expandpkg(char *t0, char *pkg) ...@@ -264,7 +267,7 @@ expandpkg(char *t0, char *pkg)
n++; n++;
if(n == 0) if(n == 0)
return t0; return strdup(t0);
// use malloc, not mal, so that caller can free // use malloc, not mal, so that caller can free
w0 = malloc(strlen(t0) + strlen(pkg)*n); w0 = malloc(strlen(t0) + strlen(pkg)*n);
......
...@@ -351,6 +351,7 @@ objfile(char *file, char *pkg) ...@@ -351,6 +351,7 @@ objfile(char *file, char *pkg)
Bseek(f, 0L, 0); Bseek(f, 0L, 0);
ldobj(f, pkg, l, file, FileObj); ldobj(f, pkg, l, file, FileObj);
Bterm(f); Bterm(f);
free(pkg);
return; return;
} }
...@@ -412,6 +413,7 @@ objfile(char *file, char *pkg) ...@@ -412,6 +413,7 @@ objfile(char *file, char *pkg)
out: out:
Bterm(f); Bterm(f);
free(pkg);
} }
void void
...@@ -439,14 +441,17 @@ ldobj(Biobuf *f, char *pkg, int64 len, char *pn, int whence) ...@@ -439,14 +441,17 @@ ldobj(Biobuf *f, char *pkg, int64 len, char *pn, int whence)
magic = c1<<24 | c2<<16 | c3<<8 | c4; magic = c1<<24 | c2<<16 | c3<<8 | c4;
if(magic == 0x7f454c46) { // \x7F E L F if(magic == 0x7f454c46) { // \x7F E L F
ldelf(f, pkg, len, pn); ldelf(f, pkg, len, pn);
free(pn);
return; return;
} }
if((magic&~1) == 0xfeedface || (magic&~0x01000000) == 0xcefaedfe) { if((magic&~1) == 0xfeedface || (magic&~0x01000000) == 0xcefaedfe) {
ldmacho(f, pkg, len, pn); ldmacho(f, pkg, len, pn);
free(pn);
return; return;
} }
if(c1 == 0x4c && c2 == 0x01 || c1 == 0x64 && c2 == 0x86) { if(c1 == 0x4c && c2 == 0x01 || c1 == 0x64 && c2 == 0x86) {
ldpe(f, pkg, len, pn); ldpe(f, pkg, len, pn);
free(pn);
return; return;
} }
...@@ -472,16 +477,18 @@ ldobj(Biobuf *f, char *pkg, int64 len, char *pn, int whence) ...@@ -472,16 +477,18 @@ ldobj(Biobuf *f, char *pkg, int64 len, char *pn, int whence)
return; return;
} }
diag("%s: not an object file", pn); diag("%s: not an object file", pn);
free(pn);
return; return;
} }
// First, check that the basic goos, string, and version match. // First, check that the basic goos, string, and version match.
t = smprint("%s %s %s ", getgoos(), thestring, getgoversion()); t = smprint("%s %s %s ", goos, thestring, getgoversion());
line[n] = ' '; line[n] = ' ';
if(strncmp(line+10, t, strlen(t)) != 0 && !debug['f']) { if(strncmp(line+10, t, strlen(t)) != 0 && !debug['f']) {
line[n] = '\0'; line[n] = '\0';
diag("%s: object is [%s] expected [%s]", pn, line+10, t); diag("%s: object is [%s] expected [%s]", pn, line+10, t);
free(t); free(t);
free(pn);
return; return;
} }
...@@ -496,6 +503,7 @@ ldobj(Biobuf *f, char *pkg, int64 len, char *pn, int whence) ...@@ -496,6 +503,7 @@ ldobj(Biobuf *f, char *pkg, int64 len, char *pn, int whence)
line[n] = '\0'; line[n] = '\0';
diag("%s: object is [%s] expected [%s]", pn, line+10, theline); diag("%s: object is [%s] expected [%s]", pn, line+10, theline);
free(t); free(t);
free(pn);
return; return;
} }
} }
...@@ -521,10 +529,12 @@ ldobj(Biobuf *f, char *pkg, int64 len, char *pn, int whence) ...@@ -521,10 +529,12 @@ ldobj(Biobuf *f, char *pkg, int64 len, char *pn, int whence)
Bseek(f, import1, 0); Bseek(f, import1, 0);
ldobj1(f, pkg, eof - Boffset(f), pn); ldobj1(f, pkg, eof - Boffset(f), pn);
free(pn);
return; return;
eof: eof:
diag("truncated object file: %s", pn); diag("truncated object file: %s", pn);
free(pn);
} }
static Sym* static Sym*
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment