From 547f12a8a8ed17e200f4fa3724ed42c057e7afa7 Mon Sep 17 00:00:00 2001 From: Kubat <mael.martin31@gmail.com> Date: Tue, 22 Sep 2020 19:36:14 +0200 Subject: [PATCH] MODULE: Avoid some segfaults when errors occures on kurisu / the network - error on empty downloaded json file in repo module. - only accept a json from kurisu/api --- src/module/module_repo.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/module/module_repo.c b/src/module/module_repo.c index 4d2cbc29..fffc8ff6 100644 --- a/src/module/module_repo.c +++ b/src/module/module_repo.c @@ -136,13 +136,19 @@ __json_sync(struct module_repo_internal *repo, struct json_object **json) RETURN_UNLESS(json, "Invalid argument", 1); CURL *curl_handle; CURLcode res; + struct curl_slist *headers = NULL; int ret = 1; struct __memory file = { .mem = NULL, .size = 0. }; + /* Only accept json file */ + headers = curl_slist_append(headers, "Accept: application/json"); + headers = curl_slist_append(headers, "Content-Type: application/json"); + curl_handle = curl_easy_init(); + curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl_handle, CURLOPT_URL, repo->get_all_json); curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, __write_mem); curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *) &file); @@ -155,11 +161,18 @@ __json_sync(struct module_repo_internal *repo, struct json_object **json) goto err; } + /* Check if recieved json is not empty */ + if (file.size == 0) { + LOG_ERROR("CURL", "Gottent file is empty"); + goto err; + } + *json = json_tokener_parse(file.mem); ret = 0; err: __clean_memory(&file); curl_easy_cleanup(curl_handle); + curl_slist_free_all(headers); return ret; } -- GitLab