diff --git a/modules/json/filterx-cache-json-file.c b/modules/json/filterx-cache-json-file.c index 2a69d566c..540e3ff75 100644 --- a/modules/json/filterx-cache-json-file.c +++ b/modules/json/filterx-cache-json-file.c @@ -91,7 +91,7 @@ _load_json_file(const gchar *filepath, GError **error) { g_set_error(error, CACHE_JSON_FILE_ERROR, CACHE_JSON_FILE_ERROR_FILE_OPEN_ERROR, "failed to open file: %s (%s)", filepath, g_strerror(errno)); - return FALSE; + return NULL; } struct json_tokener *tokener = json_tokener_new(); @@ -106,6 +106,9 @@ _load_json_file(const gchar *filepath, GError **error) if (ferror(file)) g_set_error(error, CACHE_JSON_FILE_ERROR, CACHE_JSON_FILE_ERROR_FILE_READ_ERROR, "failed to read file: %s (%s)", filepath, g_strerror(errno)); + else + g_set_error(error, CACHE_JSON_FILE_ERROR, CACHE_JSON_FILE_ERROR_FILE_READ_ERROR, + "failed to read file: %s (unexpected EOF)", filepath); break; } @@ -123,11 +126,23 @@ _load_json_file(const gchar *filepath, GError **error) } FilterXObject *result = NULL; - if (object && (result = filterx_json_new_from_object(object))) - filterx_object_make_readonly(result); - else - json_object_put(object); + if (!object) + goto exit; + + if (json_object_get_type(object) != json_type_object && json_object_get_type(object) != json_type_array) + { + g_set_error(error, CACHE_JSON_FILE_ERROR, CACHE_JSON_FILE_ERROR_JSON_PARSE_ERROR, + "JSON file must contain an object or an array"); + json_object_put(object); + goto exit; + } + + result = filterx_json_new_from_object(object); + g_assert(result); + filterx_object_make_readonly(result); + +exit: json_tokener_free(tokener); fclose(file); return result;