Defensively guard against calling malloc() with a size of zero.

llvm-svn: 155661
This commit is contained in:
Ted Kremenek 2012-04-26 20:54:27 +00:00
parent 822944c97d
commit b62cf212b2
1 changed files with 9 additions and 0 deletions

View File

@ -65,6 +65,15 @@ int save_arguments(int argc, const char **argv) {
for (Length = 0, i = 0; i != (unsigned)argc; ++i)
Length += strlen(argv[i])+1;
// Defensively check for a zero length, even though this is unlikely
// to happen in practice. This avoids calling malloc() below with a
// size of 0.
if (Length == 0) {
SavedArgs = 0;
SavedArgsLength = 0;
return argc;
}
SavedArgs = (char*)malloc(Length);
for (Length = 0, i = 0; i != (unsigned)argc; ++i) {
unsigned Len = strlen(argv[i]);