diff options
author | Herbert Xu <herbert@gondor.apana.org.au> | 2018-05-19 02:39:48 +0800 |
---|---|---|
committer | Herbert Xu <herbert@gondor.apana.org.au> | 2018-05-28 17:12:23 +0800 |
commit | 4f7527f8e49276894751a9b100e68bc46568bb85 (patch) | |
tree | 8a6ece3bcd6d66225c94f1df33b7fd08163aba42 /src/main.c | |
parent | memalloc: Add growstackto helper (diff) | |
download | dash-4f7527f8e49276894751a9b100e68bc46568bb85.tar.gz dash-4f7527f8e49276894751a9b100e68bc46568bb85.zip |
exec: Do not allocate stack string in padvance
Many callers of padvance immediately free the allocated string so this patch moves the stalloc call to the caller. Instead of returning the allocated string, padvance now returns the length to allocate (this may be longer than the actual string length, even including the NUL). For the case where we would previously return NULL, we now return -1. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Diffstat (limited to '')
-rw-r--r-- | src/main.c | 12 |
1 files changed, 5 insertions, 7 deletions
diff --git a/src/main.c b/src/main.c index 8b35118..c87fbd7 100644 --- a/src/main.c +++ b/src/main.c @@ -292,20 +292,18 @@ find_dot_file(char *basename) char *fullname; const char *path = pathval(); struct stat statb; + int len; /* don't try this for absolute or relative paths */ if (strchr(basename, '/')) return basename; - while ((fullname = padvance(&path, basename)) != NULL) { + while ((len = padvance(&path, basename)) >= 0) { + fullname = stackblock(); if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) { - /* - * Don't bother freeing here, since it will - * be freed by the caller. - */ - return fullname; + /* This will be freed by the caller. */ + return stalloc(len); } - stunalloc(fullname); } /* not found in the PATH */ |