Filename check and free allocated strings

Make sure file is valid before we try to read it. Also free all the
strings we allocate in various functions so we don't leak memory.

Change-Id: Ica3c8dae992e73718c79c12ff5d7e315c290caea
This commit is contained in:
Kenny Root 2010-02-17 18:31:48 -08:00
parent 2e068dc330
commit 21854ccdb2
2 changed files with 7 additions and 0 deletions

View File

@ -67,6 +67,7 @@ char* ConcatFn(const char* name, State* state, int argc, Expr* argv[]) {
for (i = 0; i < argc; ++i) {
free(strings[i]);
}
free(strings);
return result;
}
@ -389,11 +390,13 @@ int ReadArgs(State* state, Expr* argv[], int count, ...) {
for (j = 0; j < i; ++j) {
free(args[j]);
}
free(args);
return -1;
}
*(va_arg(v, char**)) = args[i];
}
va_end(v);
free(args);
return 0;
}

View File

@ -181,6 +181,10 @@ int main(int argc, char** argv) {
}
FILE* f = fopen(argv[1], "r");
if (f == NULL) {
printf("%s: %s: No such file or directory\n", argv[0], argv[1]);
return 1;
}
char buffer[8192];
int size = fread(buffer, 1, 8191, f);
fclose(f);