Better handling of out-of-memory conditions.

This commit is contained in:
Todd C. Miller
2022-11-22 11:57:42 -07:00
parent 9fff5a5fae
commit eb4ae10ab4

View File

@@ -201,33 +201,45 @@ check_passwd(void)
static char ** static char **
build_command_info(const char *command) build_command_info(const char *command)
{ {
static char **command_info; char **command_info;
int i = 0; int i = 0;
/* Setup command info. */ /* Setup command info. */
command_info = calloc(32, sizeof(char *)); command_info = calloc(32, sizeof(char *));
if (command_info == NULL) if (command_info == NULL)
return NULL; goto oom;
if ((command_info[i++] = sudo_new_key_val("command", command)) == NULL || if ((command_info[i] = sudo_new_key_val("command", command)) == NULL)
asprintf(&command_info[i++], "runas_euid=%ld", (long)runas_uid) == -1 || goto oom;
asprintf(&command_info[i++], "runas_uid=%ld", (long)runas_uid) == -1) { i++;
return NULL; if (asprintf(&command_info[i], "runas_euid=%ld", (long)runas_uid) == -1)
} goto oom;
i++;
if (asprintf(&command_info[i++], "runas_uid=%ld", (long)runas_uid) == -1)
goto oom;
i++;
if (runas_gid != (gid_t)-1) { if (runas_gid != (gid_t)-1) {
if (asprintf(&command_info[i++], "runas_gid=%ld", (long)runas_gid) == -1 || if (asprintf(&command_info[i++], "runas_gid=%ld", (long)runas_gid) == -1)
asprintf(&command_info[i++], "runas_egid=%ld", (long)runas_gid) == -1) { goto oom;
return NULL; i++;
} if (asprintf(&command_info[i++], "runas_egid=%ld", (long)runas_gid) == -1)
} goto oom;
if (use_sudoedit) { i++;
command_info[i] = strdup("sudoedit=true");
if (command_info[i++] == NULL)
return NULL;
} }
#ifdef USE_TIMEOUT #ifdef USE_TIMEOUT
command_info[i++] = "timeout=30"; if ((command_info[i] = strdup("timeout=30")) == NULL)
goto oom;
i++;
#endif #endif
if (use_sudoedit) {
if ((command_info[i] = strdup("sudoedit=true")) == NULL)
goto oom;
}
return command_info; return command_info;
oom:
while (i > 0) {
free(command_info[i--]);
}
return NULL;
} }
static char * static char *