aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorqorg11 <qorg@vxempire.xyz>2021-07-29 03:07:48 +0200
committerqorg11 <qorg@vxempire.xyz>2021-07-29 03:07:48 +0200
commitff3fcd7891f6ff70052fe26f061787b6879753ef (patch)
tree6815218d2c9c8a79a6907aa9c4e3938313196a25
parent91a12163a4a1c411457dba12de1e101de90b6d31 (diff)
downloaddemiurge-ff3fcd7891f6ff70052fe26f061787b6879753ef.tar.gz
demiurge-ff3fcd7891f6ff70052fe26f061787b6879753ef.zip
Allocate the HTTP response using dynamic memory
So no (or less...) overflows fix: #7
-rw-r--r--src/login.c18
-rw-r--r--src/upload_file.c11
2 files changed, 17 insertions, 12 deletions
diff --git a/src/login.c b/src/login.c
index 75227b0..9829d75 100644
--- a/src/login.c
+++ b/src/login.c
@@ -64,14 +64,14 @@ setup()
return -1;
}
- char buf[8192];
char *post_url = NULL;
-
+ struct memory chunk = {0};
+
dm_asprintf(&post_url, "%s%s", instance, api_url);
curl_easy_setopt(curl, CURLOPT_URL, post_url);
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, buf);
+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cb);
+ curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
/* TODO: Better way lol */
/* If someone wants to do it, use curl_mime thing, I am to lazy
@@ -84,7 +84,7 @@ setup()
curl_easy_perform(curl);
curl_easy_cleanup(curl);
- parsed_json = json_tokener_parse(buf);
+ parsed_json = json_tokener_parse(chunk.response);
if(parsed_json == NULL) {
fprintf(stderr, "error\n");
return -1;
@@ -128,11 +128,12 @@ setup()
curl_easy_setopt(curl, CURLOPT_URL, post_url);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_token_url);
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, buf);
+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cb);
+ curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
+
curl_easy_perform(curl);
- parsed_json = json_tokener_parse(buf);
+ parsed_json = json_tokener_parse(chunk.response);
json_object_object_get_ex(parsed_json, "access_token", &json_access_token);
const char *access_token = json_object_get_string(json_access_token);
store_config(instance, client_id, client_secret, access_token);
@@ -140,5 +141,6 @@ setup()
free(code);
free(post_url);
free(post_token_url);
+ free(chunk.response);
return 0;
}
diff --git a/src/upload_file.c b/src/upload_file.c
index c223efa..c17720c 100644
--- a/src/upload_file.c
+++ b/src/upload_file.c
@@ -32,7 +32,6 @@ upload_file(const char *path, const char *description, char **id_ptr)
struct json_object *parsed_json;
struct json_object *json_media_id;
- char buf[8192];
get_tokens_from_file(
".demiurgerc", instance, client_id, client_secret, access_token);
@@ -41,8 +40,11 @@ upload_file(const char *path, const char *description, char **id_ptr)
fprintf(stderr, "Error creating libcurl thing\n");
return -1;
}
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, buf);
+
+ struct memory chunk = {0};
+
+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cb);
+ curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
char *url_to_post = NULL;
dm_asprintf(&url_to_post, "%s/api/v1/media", instance);
@@ -86,11 +88,12 @@ upload_file(const char *path, const char *description, char **id_ptr)
curl_mime_free(mime);
curl_slist_free_all(header_list);
/* Get the media id */
- parsed_json = json_tokener_parse(buf);
+ parsed_json = json_tokener_parse(chunk.response);
json_object_object_get_ex(parsed_json, "id", &json_media_id);
const char *media_id = json_object_get_string(json_media_id);
*id_ptr = (char *)media_id;
free(parsed_json);
+ free(chunk.response);
return 0;
}