Skip to content
This repository has been archived by the owner on May 2, 2018. It is now read-only.

Commit

Permalink
ld: fix memory leaks
Browse files Browse the repository at this point in the history
R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/5434068
  • Loading branch information
bytbox authored and rsc committed Dec 7, 2011
1 parent 0c64972 commit a2ba34d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/cmd/ld/go.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ loadpkgdata(char *file, char *pkg, char *data, int len)
x = ilookup(name);
if(x->prefix == nil) {
x->prefix = prefix;
x->def = def;
x->def = strdup(def);
x->file = file;
} else if(strcmp(x->prefix, prefix) != 0) {
fprint(2, "%s: conflicting definitions for %s\n", argv0, name);
Expand All @@ -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);
nerrors++;
}
free(name);
free(def);
}
free(file);
}

// replace all "". with pkg.
Expand All @@ -264,7 +267,7 @@ expandpkg(char *t0, char *pkg)
n++;

if(n == 0)
return t0;
return strdup(t0);

// use malloc, not mal, so that caller can free
w0 = malloc(strlen(t0) + strlen(pkg)*n);
Expand Down
12 changes: 11 additions & 1 deletion src/cmd/ld/lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ objfile(char *file, char *pkg)
Bseek(f, 0L, 0);
ldobj(f, pkg, l, file, FileObj);
Bterm(f);
free(pkg);
return;
}

Expand Down Expand Up @@ -412,6 +413,7 @@ objfile(char *file, char *pkg)

out:
Bterm(f);
free(pkg);
}

void
Expand Down Expand Up @@ -439,14 +441,17 @@ ldobj(Biobuf *f, char *pkg, int64 len, char *pn, int whence)
magic = c1<<24 | c2<<16 | c3<<8 | c4;
if(magic == 0x7f454c46) { // \x7F E L F
ldelf(f, pkg, len, pn);
free(pn);
return;
}
if((magic&~1) == 0xfeedface || (magic&~0x01000000) == 0xcefaedfe) {
ldmacho(f, pkg, len, pn);
free(pn);
return;
}
if(c1 == 0x4c && c2 == 0x01 || c1 == 0x64 && c2 == 0x86) {
ldpe(f, pkg, len, pn);
free(pn);
return;
}

Expand All @@ -472,16 +477,18 @@ ldobj(Biobuf *f, char *pkg, int64 len, char *pn, int whence)
return;
}
diag("%s: not an object file", pn);
free(pn);
return;
}

// 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] = ' ';
if(strncmp(line+10, t, strlen(t)) != 0 && !debug['f']) {
line[n] = '\0';
diag("%s: object is [%s] expected [%s]", pn, line+10, t);
free(t);
free(pn);
return;
}

Expand All @@ -496,6 +503,7 @@ ldobj(Biobuf *f, char *pkg, int64 len, char *pn, int whence)
line[n] = '\0';
diag("%s: object is [%s] expected [%s]", pn, line+10, theline);
free(t);
free(pn);
return;
}
}
Expand All @@ -521,10 +529,12 @@ ldobj(Biobuf *f, char *pkg, int64 len, char *pn, int whence)
Bseek(f, import1, 0);

ldobj1(f, pkg, eof - Boffset(f), pn);
free(pn);
return;

eof:
diag("truncated object file: %s", pn);
free(pn);
}

static Sym*
Expand Down

0 comments on commit a2ba34d

Please sign in to comment.