Commit 6afe7f23 authored by Andrew Tridgell's avatar Andrew Tridgell

got rid of dependency on alloca in popt

parent 19b27a48
...@@ -9,7 +9,7 @@ const char * findProgramPath(const char * argv0) { ...@@ -9,7 +9,7 @@ const char * findProgramPath(const char * argv0) {
char * path = getenv("PATH"); char * path = getenv("PATH");
char * pathbuf; char * pathbuf;
char * start, * chptr; char * start, * chptr;
char * buf; char * buf, *local = NULL;
/* If there is a / in the argv[0], it has to be an absolute /* If there is a / in the argv[0], it has to be an absolute
path */ path */
...@@ -18,7 +18,7 @@ const char * findProgramPath(const char * argv0) { ...@@ -18,7 +18,7 @@ const char * findProgramPath(const char * argv0) {
if (!path) return NULL; if (!path) return NULL;
start = pathbuf = alloca(strlen(path) + 1); local = start = pathbuf = malloc(strlen(path) + 1);
buf = malloc(strlen(path) + strlen(argv0) + 2); buf = malloc(strlen(path) + strlen(argv0) + 2);
strcpy(pathbuf, path); strcpy(pathbuf, path);
...@@ -28,8 +28,10 @@ const char * findProgramPath(const char * argv0) { ...@@ -28,8 +28,10 @@ const char * findProgramPath(const char * argv0) {
*chptr = '\0'; *chptr = '\0';
sprintf(buf, "%s/%s", start, argv0); sprintf(buf, "%s/%s", start, argv0);
if (!access(buf, X_OK)) if (!access(buf, X_OK)) {
return buf; if (local) free(local);
return buf;
}
if (chptr) if (chptr)
start = chptr + 1; start = chptr + 1;
...@@ -38,6 +40,7 @@ const char * findProgramPath(const char * argv0) { ...@@ -38,6 +40,7 @@ const char * findProgramPath(const char * argv0) {
} while (start && *start); } while (start && *start);
free(buf); free(buf);
if (local) free(local);
return NULL; return NULL;
} }
...@@ -227,7 +227,7 @@ static void execCommand(poptContext con) { ...@@ -227,7 +227,7 @@ static void execCommand(poptContext con) {
if (!con->execAbsolute && strchr(script, '/')) return; if (!con->execAbsolute && strchr(script, '/')) return;
if (!strchr(script, '/') && con->execPath) { if (!strchr(script, '/') && con->execPath) {
char *s = alloca(strlen(con->execPath) + strlen(script) + 2); char *s = malloc(strlen(con->execPath) + strlen(script) + 2);
sprintf(s, "%s/%s", con->execPath, script); sprintf(s, "%s/%s", con->execPath, script);
argv[pos] = s; argv[pos] = s;
} else { } else {
...@@ -398,6 +398,14 @@ int poptGetNextOpt(poptContext con) ...@@ -398,6 +398,14 @@ int poptGetNextOpt(poptContext con)
const struct poptOption * opt = NULL; const struct poptOption * opt = NULL;
int done = 0; int done = 0;
/* looks a bit tricky to get rid of alloca properly in this fn */
#if HAVE_ALLOCA_H
#define ALLOCA(x) alloca(x)
#else
#define ALLOCA(x) malloc(x)
#endif
while (!done) { while (!done) {
const char * origOptString = NULL; const char * origOptString = NULL;
poptCallbackType cb = NULL; poptCallbackType cb = NULL;
...@@ -436,7 +444,7 @@ int poptGetNextOpt(poptContext con) ...@@ -436,7 +444,7 @@ int poptGetNextOpt(poptContext con)
/* Make a copy we can hack at */ /* Make a copy we can hack at */
localOptString = optString = localOptString = optString =
strcpy(alloca(strlen(origOptString) + 1), strcpy(ALLOCA(strlen(origOptString) + 1),
origOptString); origOptString);
if (!optString[0]) if (!optString[0])
......
...@@ -55,8 +55,8 @@ static void configLine(poptContext con, char * line) { ...@@ -55,8 +55,8 @@ static void configLine(poptContext con, char * line) {
} }
int poptReadConfigFile(poptContext con, const char * fn) { int poptReadConfigFile(poptContext con, const char * fn) {
char * file, * chptr, * end; char * file=NULL, * chptr, * end;
char * buf, * dst; char * buf=NULL, * dst;
int fd, rc; int fd, rc;
int fileLength; int fileLength;
...@@ -71,16 +71,17 @@ int poptReadConfigFile(poptContext con, const char * fn) { ...@@ -71,16 +71,17 @@ int poptReadConfigFile(poptContext con, const char * fn) {
fileLength = lseek(fd, 0, SEEK_END); fileLength = lseek(fd, 0, SEEK_END);
(void) lseek(fd, 0, 0); (void) lseek(fd, 0, 0);
file = alloca(fileLength + 1); file = malloc(fileLength + 1);
if (read(fd, file, fileLength) != fileLength) { if (read(fd, file, fileLength) != fileLength) {
rc = errno; rc = errno;
close(fd); close(fd);
errno = rc; errno = rc;
if (file) free(file);
return POPT_ERROR_ERRNO; return POPT_ERROR_ERRNO;
} }
close(fd); close(fd);
dst = buf = alloca(fileLength + 1); dst = buf = malloc(fileLength + 1);
chptr = file; chptr = file;
end = (file + fileLength); end = (file + fileLength);
...@@ -111,6 +112,9 @@ int poptReadConfigFile(poptContext con, const char * fn) { ...@@ -111,6 +112,9 @@ int poptReadConfigFile(poptContext con, const char * fn) {
} }
} }
free(file);
free(buf);
return 0; return 0;
} }
...@@ -125,10 +129,11 @@ int poptReadDefaultConfig(poptContext con, /*@unused@*/ int useEnv) { ...@@ -125,10 +129,11 @@ int poptReadDefaultConfig(poptContext con, /*@unused@*/ int useEnv) {
if (getuid() != geteuid()) return 0; if (getuid() != geteuid()) return 0;
if ((home = getenv("HOME"))) { if ((home = getenv("HOME"))) {
fn = alloca(strlen(home) + 20); fn = malloc(strlen(home) + 20);
strcpy(fn, home); strcpy(fn, home);
strcat(fn, "/.popt"); strcat(fn, "/.popt");
rc = poptReadConfigFile(con, fn); rc = poptReadConfigFile(con, fn);
free(fn);
if (rc) return rc; if (rc) return rc;
} }
......
...@@ -43,7 +43,8 @@ int poptParseArgvString(const char * s, int * argcPtr, const char *** argvPtr) ...@@ -43,7 +43,8 @@ int poptParseArgvString(const char * s, int * argcPtr, const char *** argvPtr)
const char ** argv = malloc(sizeof(*argv) * argvAlloced); const char ** argv = malloc(sizeof(*argv) * argvAlloced);
int argc = 0; int argc = 0;
int buflen = strlen(s) + 1; int buflen = strlen(s) + 1;
char * buf = memset(alloca(buflen), 0, buflen); char *buf0 = calloc(buflen, 1);
char *buf = buf0;
argv[argc] = buf; argv[argc] = buf;
...@@ -55,6 +56,7 @@ int poptParseArgvString(const char * s, int * argcPtr, const char *** argvPtr) ...@@ -55,6 +56,7 @@ int poptParseArgvString(const char * s, int * argcPtr, const char *** argvPtr)
src++; src++;
if (!*src) { if (!*src) {
free(argv); free(argv);
free(buf0);
return POPT_ERROR_BADQUOTE; return POPT_ERROR_BADQUOTE;
} }
if (*src != quote) *buf++ = '\\'; if (*src != quote) *buf++ = '\\';
...@@ -78,6 +80,7 @@ int poptParseArgvString(const char * s, int * argcPtr, const char *** argvPtr) ...@@ -78,6 +80,7 @@ int poptParseArgvString(const char * s, int * argcPtr, const char *** argvPtr)
src++; src++;
if (!*src) { if (!*src) {
free(argv); free(argv);
free(buf0);
return POPT_ERROR_BADQUOTE; return POPT_ERROR_BADQUOTE;
} }
/*@fallthrough@*/ /*@fallthrough@*/
...@@ -94,6 +97,6 @@ int poptParseArgvString(const char * s, int * argcPtr, const char *** argvPtr) ...@@ -94,6 +97,6 @@ int poptParseArgvString(const char * s, int * argcPtr, const char *** argvPtr)
(void) poptDupArgv(argc, argv, argcPtr, argvPtr); (void) poptDupArgv(argc, argv, argcPtr, argvPtr);
free(argv); free(argv);
free(buf0);
return 0; return 0;
} }
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