ape: fix memory leak and path limit in unlink()
db1 was leaked, and newname could overflow. fixed.
This commit is contained in:
parent
a16f5cd2a3
commit
2fcd19f16e
1 changed files with 19 additions and 6 deletions
|
@ -19,7 +19,7 @@ unlink(const char *path)
|
||||||
long long nn;
|
long long nn;
|
||||||
Dir *db1, *db2, nd;
|
Dir *db1, *db2, nd;
|
||||||
Fdinfo *f;
|
Fdinfo *f;
|
||||||
char *p, newname[PATH_MAX], newelem[32];
|
char *p, *newname, newelem[32];
|
||||||
|
|
||||||
/* if the file is already open, make it close-on-exec (and rename to qid) */
|
/* if the file is already open, make it close-on-exec (and rename to qid) */
|
||||||
if((db1 = _dirstat(path)) == nil) {
|
if((db1 = _dirstat(path)) == nil) {
|
||||||
|
@ -33,7 +33,10 @@ unlink(const char *path)
|
||||||
db1->qid.vers == db2->qid.vers &&
|
db1->qid.vers == db2->qid.vers &&
|
||||||
db1->type == db2->type &&
|
db1->type == db2->type &&
|
||||||
db1->dev == db2->dev) {
|
db1->dev == db2->dev) {
|
||||||
sprintf(newelem, "%8.8lx%8.8lx", (ulong)(db2->qid.path>>32), (ulong)db2->qid.path);
|
newname = 0;
|
||||||
|
sprintf(newelem, "%8.8lx%8.8lx",
|
||||||
|
(ulong)(db2->qid.path>>32),
|
||||||
|
(ulong)db2->qid.path);
|
||||||
_nulldir(&nd);
|
_nulldir(&nd);
|
||||||
nd.name = newelem;
|
nd.name = newelem;
|
||||||
if(_dirfwstat(i, &nd) < 0)
|
if(_dirfwstat(i, &nd) < 0)
|
||||||
|
@ -43,15 +46,23 @@ unlink(const char *path)
|
||||||
if(p == 0)
|
if(p == 0)
|
||||||
p = newelem;
|
p = newelem;
|
||||||
else {
|
else {
|
||||||
memmove(newname, path, p-path);
|
n = p-path;
|
||||||
newname[p-path] = '/';
|
newname = malloc(n+1+sizeof(newelem));
|
||||||
strcpy(newname+(p-path)+1, newelem);
|
if(newname == 0){
|
||||||
|
free(db2);
|
||||||
|
free(db1);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
memmove(newname, path, n);
|
||||||
|
newname[n] = '/';
|
||||||
|
strcpy(newname+n+1, newelem);
|
||||||
p = newname;
|
p = newname;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* reopen remove on close */
|
/* reopen remove on close */
|
||||||
fd = _OPEN(p, 64|(f->oflags));
|
fd = _OPEN(p, ORCLOSE|(f->oflags));
|
||||||
if(fd < 0){
|
if(fd < 0){
|
||||||
|
free(newname);
|
||||||
free(db2);
|
free(db2);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -61,6 +72,8 @@ unlink(const char *path)
|
||||||
_SEEK(fd, nn, 0);
|
_SEEK(fd, nn, 0);
|
||||||
_DUP(fd, i);
|
_DUP(fd, i);
|
||||||
_CLOSE(fd);
|
_CLOSE(fd);
|
||||||
|
free(newname);
|
||||||
|
free(db2);
|
||||||
free(db1);
|
free(db1);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue