History

9pex: fixes for two (unlikely but) possible memory leaks

Sigrid Solveig Haflínudóttir committed 5 years ago

Commit d93d3851 parent e05b2fc1 file · +11 −8Browse files

Unified · Split

9pex.c+11−8
@@ -260,12 +260,16 @@ newfid(C9fid fid, char *path, char **err) · 259 unmodified lines
260260 if (statpath(path, &st, err) != 0)
261261 return NULL;
262262
263− if ((f = calloc(1, sizeof(*f))) == NULL) {
263+ if ((f = calloc(1, sizeof(*f))) == NULL || (f->path = strdup(path)) == NULL) {
264+nomem:
265+ if (f != NULL) {
266+ free(f->path);
267+ free(f);
268+ }
264269 *err = Enomem;
265270 return NULL;
266271 }
267272 f->fd = -1;
268− f->path = strdup(path);
269273 f->name = strrchr(f->path, '/');
270274 if (f->name == NULL)
271275 f->name = f->path;
@@ -281,10 +285,8 @@ newfid(C9fid fid, char *path, char **err) · 9 unmodified lines
281285 }
282286 }
283287 if (i >= numfids) {
284− if ((newfids = realloc(fids, (numfids+1)*sizeof(*fids))) == NULL) {
285− *err = Enomem;
286− return NULL;
287− }
288+ if ((newfids = realloc(fids, (numfids+1)*sizeof(*fids))) == NULL)
289+ goto nomem;
288290 fids = newfids;
289291 fids[numfids++] = f;
290292 }
@@ -393,9 +395,10 @@ walk(C9fid fid, C9fid nfid, char *el[], C9qid *qids[], char **err) · 102 unmodified lines
393395 path[plen+1+ellen] = 0;
394396
395397 if (!rootescape) {
396− if ((real = realpath(path, NULL)) == NULL)
397− break;
398+ real = realpath(path, NULL);
398399 free(path);
400+ if (real == NULL)
401+ break;
399402 if (strlen(real) < rootlen) { /* don't escape root */
400403 free(real);
401404 real = strdup(rootpath);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t