Add missing memory allocation failure checks.

Inspired by GitHub PR #221
This commit is contained in:
Todd C. Miller
2022-12-15 09:30:49 -07:00
parent e706204f5f
commit 0e6482e827
2 changed files with 26 additions and 7 deletions

View File

@@ -93,10 +93,22 @@ create_str_array(size_t count, ...)
va_start(args, count);
char ** result = calloc(count, sizeof(char *));
for (size_t i = 0; i < count; ++i) {
const char *str = va_arg(args, char *);
result[i] = (str == NULL ? NULL : strdup(str));
char **result = calloc(count, sizeof(char *));
if (result != NULL) {
for (size_t i = 0; i < count; ++i) {
const char *str = va_arg(args, char *);
if (str != NULL) {
result[i] = strdup(str);
if (result[i] == NULL) {
while (i > 0) {
free(result[--i]);
}
free(result);
result = NULL;
break;
}
}
}
}
va_end(args);
@@ -164,6 +176,8 @@ fake_conversation(int num_msgs, const struct sudo_conv_message msgs[],
return 1; // simulates user interruption (conversation error)
replies[i].reply = strdup(data.conv_replies[i]);
if (replies[i].reply == NULL)
return 1; // memory allocation error
}
return 0; // simulate user answered just fine