nss: Skip empty files and avoid use of uninitialized value

JSON parser isn't called when reading empty files so `jerr` will be used
uninitialized in the original code. Empty files appear when a network
has no dhcp clients.

This patch checks for such files and skip them.

Fixes: a8d828c88bbdaf83ae78dc06cdd84d5667fcc424
Signed-off-by: Jiang XueQian <jiangxueqian@gmail.com>
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Jiang XueQian 2025-01-18 16:32:10 +08:00 committed by Michal Privoznik
parent a2297fb157
commit 63a3d70697
2 changed files with 14 additions and 4 deletions

View File

@ -263,7 +263,7 @@ findLeases(const char *file,
enum json_tokener_error jerr;
int jsonflags = JSON_TOKENER_STRICT | JSON_TOKENER_VALIDATE_UTF8;
char line[1024];
ssize_t nreadTotal = 0;
size_t nreadTotal = 0;
int rv;
if ((fd = open(file, O_RDONLY)) < 0) {
@ -290,12 +290,17 @@ findLeases(const char *file,
jerr = json_tokener_get_error(tok);
} while (jerr == json_tokener_continue);
if (nreadTotal == 0) {
ret = 0;
goto cleanup;
}
if (jerr == json_tokener_continue) {
ERROR("Cannot parse %s: incomplete json found", file);
goto cleanup;
}
if (nreadTotal > 0 && jerr != json_tokener_success) {
if (jerr != json_tokener_success) {
ERROR("Cannot parse %s: %s", file, json_tokener_error_desc(jerr));
goto cleanup;
}

View File

@ -124,7 +124,7 @@ findMACs(const char *file,
json_tokener *tok = NULL;
enum json_tokener_error jerr;
int jsonflags = JSON_TOKENER_STRICT | JSON_TOKENER_VALIDATE_UTF8;
ssize_t nreadTotal = 0;
size_t nreadTotal = 0;
int rv;
size_t i;
@ -152,12 +152,17 @@ findMACs(const char *file,
jerr = json_tokener_get_error(tok);
} while (jerr == json_tokener_continue);
if (nreadTotal == 0) {
ret = 0;
goto cleanup;
}
if (jerr == json_tokener_continue) {
ERROR("Cannot parse %s: incomplete json found", file);
goto cleanup;
}
if (nreadTotal > 0 && jerr != json_tokener_success) {
if (jerr != json_tokener_success) {
ERROR("Cannot parse %s: %s", file, json_tokener_error_desc(jerr));
goto cleanup;
}