Do not use JSON_ARRAY with sudo_json_add_value()

This commit is contained in:
Todd C. Miller
2020-03-29 05:05:08 -06:00
parent f24dacdee2
commit cffda82e20
7 changed files with 48 additions and 59 deletions

View File

@@ -932,6 +932,8 @@ iolog_write_info_file_json(int dfd, const char *parent, struct iolog_info *info)
bool ret = false;
FILE *fp = NULL;
int fd = -1;
size_t i;
char *cp;
debug_decl(iolog_write_info_file_json, SUDO_DEBUG_UTIL);
if (info->cmd == NULL || info->user == NULL || info->runas_user == NULL)
@@ -974,16 +976,28 @@ iolog_write_info_file_json(int dfd, const char *parent, struct iolog_info *info)
goto oom;
if (info->argv != NULL) {
json_value.type = JSON_ARRAY;
json_value.u.array = info->argv;
if (!sudo_json_add_value(&json, "runargv", &json_value))
if (!sudo_json_open_array(&json, "runargv"))
goto oom;
for (i = 0; (cp = info->argv[i]) != NULL; i++) {
json_value.type = JSON_STRING;
json_value.u.string = cp;
if (!sudo_json_add_value(&json, NULL, &json_value))
goto oom;
}
if (!sudo_json_close_array(&json))
goto oom;
}
if (info->envp != NULL) {
json_value.type = JSON_ARRAY;
json_value.u.array = info->envp;
if (!sudo_json_add_value(&json, "runenv", &json_value))
if (!sudo_json_open_array(&json, "runenv"))
goto oom;
for (i = 0; (cp = info->envp[i]) != NULL; i++) {
json_value.type = JSON_STRING;
json_value.u.string = cp;
if (!sudo_json_add_value(&json, NULL, &json_value))
goto oom;
}
if (!sudo_json_close_array(&json))
goto oom;
}

View File

@@ -305,7 +305,6 @@ sudo_json_add_value_int(struct json_container *json, const char *name,
struct json_value *value, bool as_object)
{
char numbuf[(((sizeof(long long) * 8) + 2) / 3) + 2];
unsigned int i;
debug_decl(sudo_json_add_value, SUDO_DEBUG_UTIL);
/* Add comma if we are continuing an object/array. */
@@ -356,41 +355,7 @@ sudo_json_add_value_int(struct json_container *json, const char *name,
debug_return_bool(false);
break;
case JSON_ARRAY:
if (value->u.array[0] == NULL || value->u.array[1] == NULL) {
if (!json_append_buf(json, "[ "))
debug_return_bool(false);
if (value->u.array[0] != NULL) {
if (!json_append_string(json, value->u.array[0]))
debug_return_bool(false);
if (!json_append_buf(json, " "))
debug_return_bool(false);
}
if (!json_append_buf(json, "]"))
debug_return_bool(false);
} else {
if (!json_append_buf(json, "["))
debug_return_bool(false);
if (!json_append_buf(json, json->compact ? " " : "\n"))
debug_return_bool(false);
json->indent_level += json->indent_increment;
for (i = 0; value->u.array[i] != NULL; i++) {
if (!json_append_indent(json, json->indent_level))
debug_return_bool(false);
if (!json_append_string(json, value->u.array[i]))
debug_return_bool(false);
if (value->u.array[i + 1] != NULL) {
if (!json_append_buf(json, ","))
debug_return_bool(false);
}
if (!json_append_buf(json, json->compact ? " " : "\n"))
debug_return_bool(false);
}
json->indent_level -= json->indent_increment;
if (!json_append_indent(json, json->indent_level))
debug_return_bool(false);
if (!json_append_buf(json, "]"))
debug_return_bool(false);
}
sudo_fatalx("internal error: can't print JSON_ARRAY");
break;
case JSON_OBJECT:
sudo_fatalx("internal error: can't print JSON_OBJECT");