Skip to content

Commit 15b5855

Browse files
committed
json: Verify JSON config file starts with an array
Do not blindly assume the file starts with an array. This avoids a crash in json-c. Signed-off-by: Daniel Wagner <[email protected]>
1 parent 8f5d708 commit 15b5855

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

src/nvme/json.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,15 @@ int json_read_config(nvme_root_t r, const char *config_file)
207207
return fd;
208208
}
209209
json_root = parse_json(r, fd);
210+
close(fd);
210211
if (!json_root) {
211212
errno = EPROTO;
212-
close(fd);
213+
return -1;
214+
}
215+
if (!json_object_is_type(json_root, json_type_array)) {
216+
nvme_msg(r, LOG_DEBUG, "Wrong format, expected array\n");
217+
json_object_put(json_root);
218+
errno = EPROTO;
213219
return -1;
214220
}
215221
for (h = 0; h < json_object_array_length(json_root); h++) {
@@ -218,7 +224,6 @@ int json_read_config(nvme_root_t r, const char *config_file)
218224
json_parse_host(r, host_obj);
219225
}
220226
json_object_put(json_root);
221-
close(fd);
222227
return 0;
223228
}
224229

0 commit comments

Comments
 (0)