Skip to content

Commit

Permalink
Merge pull request #417 from alltilla/cache-json-file-error-reporting…
Browse files Browse the repository at this point in the history
…-fix

filterx: fix `cache_json_file()` error reporting
  • Loading branch information
bazsi authored Dec 16, 2024
2 parents 29807af + d28caab commit 16b9c5e
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions modules/json/filterx-cache-json-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
}

Expand All @@ -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;
Expand Down

0 comments on commit 16b9c5e

Please sign in to comment.