summaryrefslogtreecommitdiff
path: root/src/process_url.c
diff options
context:
space:
mode:
authorMole Shang <[email protected]>2023-08-09 20:37:43 +0800
committerMole Shang <[email protected]>2023-08-09 21:37:33 +0800
commitdcae2252e28fca2d8308eda208a03dbd09eaa286 (patch)
tree10ee74ef45326ac2fd119714f258394de268cee6 /src/process_url.c
parent12125ad6f4aabed1c4d0f328c33eb0717a8431ef (diff)
downloadhinata-dcae2252e28fca2d8308eda208a03dbd09eaa286.tar.gz
hinata-dcae2252e28fca2d8308eda208a03dbd09eaa286.tar.bz2
hinata-dcae2252e28fca2d8308eda208a03dbd09eaa286.zip
tree-wide: pass pointer-to-pointer to correctly free pointers
Diffstat (limited to 'src/process_url.c')
-rw-r--r--src/process_url.c54
1 files changed, 28 insertions, 26 deletions
diff --git a/src/process_url.c b/src/process_url.c
index 8d64cda..328fc4f 100644
--- a/src/process_url.c
+++ b/src/process_url.c
@@ -145,7 +145,7 @@ static void gen_fullpathfn(char *fullpathfn, const char *outdir,
outdir[strlen(outdir) - 1] == SPLITTER_CHAR ? "" : SPLITTER_STR, fn);
}
-static int parse_url(const char *URL, const char *outdir, char *fn) {
+static int parse_url(const char *URL, const char *outdir, char **p_fn) {
CURLUcode ue = logerr(curl_url_set(h, CURLUPART_URL, URL, 0));
if (ue && ue != CURLUE_NO_QUERY) {
return 1;
@@ -204,7 +204,7 @@ static int parse_url(const char *URL, const char *outdir, char *fn) {
/* filename */
- if (fn == NULL) {
+ if (p_fn == NULL) {
const char *patterns_str[1] = {"(?:.+\\/)([^#/?]+)"};
str_array_t results = create_str_array(0);
const str_array_t patterns = {(char **)patterns_str, 1};
@@ -228,9 +228,9 @@ static int parse_url(const char *URL, const char *outdir, char *fn) {
return 1;
}
} else {
- curl_c->outfn = malloc(strlen(outdir) + strlen(fn) + 2);
- gen_fullpathfn(curl_c->outfn, outdir, fn);
- free_and_nullify(fn);
+ curl_c->outfn = malloc(strlen(outdir) + strlen(*p_fn) + 2);
+ gen_fullpathfn(curl_c->outfn, outdir, *p_fn);
+ free_and_nullify((void **)p_fn);
}
DEBUG_PRINT("File will be saved as: %s\n", curl_c->outfn);
DEBUG_PRINT("Got regular URL: %s\n", curl_c->URL);
@@ -275,7 +275,7 @@ bool get_info(const char *URL, long *psize, char **p_content_type,
goto end;
}
DEBUG_PRINT("Set-Cookie: %s\n", pch->value);
- if (p_cookie){
+ if (p_cookie) {
*p_cookie = malloc(strlen(pch->value) + 1);
strcpy(*p_cookie, pch->value);
}
@@ -348,7 +348,8 @@ static int pull_part(void *a) {
return (int)res;
}
-static int merge_and_cleanup(curl_conf_t *curl_c) {
+static int merge_and_cleanup(curl_conf_t **p_curl_c) {
+ curl_conf_t *curl_c = *p_curl_c;
if (corrupted) {
append_log("Cancelling...\n");
} else {
@@ -389,8 +390,9 @@ static int merge_and_cleanup(curl_conf_t *curl_c) {
corrupted = false;
curl_c->success_thrd = 0;
curl_c->total_thrd = 0;
- free_and_nullify(curl_c->URL);
- free_and_nullify(curl_c->outfn);
+ free_and_nullify((void **)&curl_c->URL);
+ free_and_nullify((void **)&curl_c->outfn);
+ free_and_nullify((void **)p_curl_c);
return 0;
}
@@ -455,19 +457,20 @@ static void replace_illegal_char(char *str) {
}
}
-static char *callback_struct_convert_fullpath(char *filename) {
- char *tmp = malloc(strlen(outdir_g) + strlen(filename) + 2);
- replace_illegal_char(filename);
- gen_fullpathfn(tmp, outdir_g, filename);
- free_and_nullify(filename);
+static char *callback_struct_convert_fullpath(char **p_filename) {
+ char *tmp = malloc(strlen(outdir_g) + strlen(*p_filename) + 2);
+ replace_illegal_char(*p_filename);
+ gen_fullpathfn(tmp, outdir_g, *p_filename);
+ free_and_nullify((void **)p_filename);
return tmp;
}
-void add_cookie(char *cookie) {
+void add_cookie(char **p_cookie) {
+ char *cookie = *p_cookie;
if (cookie_g) {
char *tmp = malloc(strlen(cookie_g) + strlen(cookie) + 3);
sprintf(tmp, "%s; %s", cookie_g, cookie);
- free_and_nullify(cookie_g);
+ free_and_nullify((void **)&cookie_g);
cookie_g = tmp;
} else {
cookie_g = cookie;
@@ -479,7 +482,7 @@ void set_referer(char *referer) { referer_g = referer; }
void curl_init(char *cookie) {
curl_global_init(CURL_GLOBAL_ALL);
h = curl_url();
- add_cookie(cookie);
+ add_cookie(&cookie);
dl_queue = create_queue();
mtx_init(&mtx, mtx_plain);
cnd_init(&cnd);
@@ -498,13 +501,13 @@ void curl_cleanup(status_t *stat) {
}
mtx_unlock(&mtx);
if (!stat->is_done) {
- merge_and_cleanup(curl_conf);
+ merge_and_cleanup(&curl_conf);
}
mtx_destroy(&mtx);
cnd_destroy(&cnd);
}
free_queue(&dl_queue);
- free_and_nullify(cookie_g);
+ free_and_nullify((void **)&cookie_g);
curl_url_cleanup(h);
curl_global_cleanup();
}
@@ -538,8 +541,7 @@ void poll_status(status_t *stat) {
int r;
thrd_join(tid[i], &r);
}
- merge_and_cleanup(curl_conf);
- free_and_nullify(curl_conf);
+ merge_and_cleanup(&curl_conf);
// Perform the callback
if (is_empty_queue(&dl_queue) && callback_g) {
thrd_t cb_thrd;
@@ -563,7 +565,7 @@ int get(const char *URL, char **pdstr) {
CURLcode res = logerr(curl_easy_perform(curl));
*pdstr = malloc(pagedata.len + 1);
strcpy(*pdstr, pagedata.string);
- free_and_nullify(pagedata.string);
+ free_and_nullify((void **)&pagedata.string);
curl_easy_cleanup(curl);
return res;
}
@@ -594,16 +596,16 @@ void add_url(const char *URL, const char *outdir, const char *fn,
callback_g = callback;
if (p_callback_struct) {
p_callback_struct->videofn =
- callback_struct_convert_fullpath(p_callback_struct->videofn);
+ callback_struct_convert_fullpath(&p_callback_struct->videofn);
p_callback_struct->audiofn =
- callback_struct_convert_fullpath(p_callback_struct->audiofn);
+ callback_struct_convert_fullpath(&p_callback_struct->audiofn);
p_callback_struct->filename =
- callback_struct_convert_fullpath(p_callback_struct->filename);
+ callback_struct_convert_fullpath(&p_callback_struct->filename);
p_callback_struct_g = p_callback_struct;
}
// Pass our cache (outdir_g) to parse_url()
- if (parse_url(URL, outdir_g, filename)) {
+ if (parse_url(URL, outdir_g, &filename)) {
DEBUG_PRINT("parse_url() failed with error.\n");
return; // Parse failed, quit the task directly
};