shared/json: use different return code for empty input

It is useful to distinguish if json_parse_file() got no input or invalid input.
Use different return codes for the two cases.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2022-10-19 09:46:55 +02:00
parent 7922ead507
commit 87a16eb8b5
3 changed files with 23 additions and 3 deletions
+1 -1
View File
@@ -859,7 +859,7 @@ int parse_elf_object(int fd, const char *executable, bool fork_disable_dump, cha
return -errno;
r = json_parse_file(json_in, NULL, 0, &package_metadata, NULL, NULL);
if (r < 0 && r != -EINVAL) /* EINVAL: json was empty, so we got nothing, but that's ok */
if (r < 0 && r != -ENODATA) /* ENODATA: json was empty, so we got nothing, but that's ok */
return r;
}
+4 -2
View File
@@ -3187,7 +3187,6 @@ int json_parse_continue(const char **p, JsonParseFlags flags, JsonVariant **ret,
int json_parse_file_at(FILE *f, int dir_fd, const char *path, JsonParseFlags flags, JsonVariant **ret, unsigned *ret_line, unsigned *ret_column) {
_cleanup_(json_source_unrefp) JsonSource *source = NULL;
_cleanup_free_ char *text = NULL;
const char *p;
int r;
if (f)
@@ -3199,13 +3198,16 @@ int json_parse_file_at(FILE *f, int dir_fd, const char *path, JsonParseFlags fla
if (r < 0)
return r;
if (isempty(text))
return -ENODATA;
if (path) {
source = json_source_new(path);
if (!source)
return -ENOMEM;
}
p = text;
const char *p = text;
return json_parse_internal(&p, source, flags, ret, ret_line, ret_column, false);
}
+18
View File
@@ -344,6 +344,24 @@ TEST(build) {
assert_se(json_variant_equal(a, b));
}
TEST(json_parse_file_empty) {
_cleanup_fclose_ FILE *f = NULL;
_cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
assert_se(fopen_unlocked("/dev/null", "re", &f) >= 0);
assert_se(json_parse_file(f, "waldo", 0, &v, NULL, NULL) == -ENODATA);
assert_se(v == NULL);
}
TEST(json_parse_file_invalid) {
_cleanup_fclose_ FILE *f = NULL;
_cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
assert_se(f = fmemopen_unlocked((void*) "kookoo", 6, "r"));
assert_se(json_parse_file(f, "waldo", 0, &v, NULL, NULL) == -EINVAL);
assert_se(v == NULL);
}
TEST(source) {
static const char data[] =
"\n"