fix segfault in get_default_filepath

parent 23119ece
...@@ -89,7 +89,8 @@ main(int argc, char** argv) ...@@ -89,7 +89,8 @@ main(int argc, char** argv)
die_if_null(filepaths_file,"Could not open given file file %s!", filepaths_file_path); die_if_null(filepaths_file,"Could not open given file file %s!", filepaths_file_path);
while(getline(&curr_url, &url_len, url_file) != -1){ while(getline(&curr_url, &url_len, url_file) != -1){
drop_newline(curr_url); drop_newline(curr_url);
// Determine the filepath // Determine the filepath
// Memory leak.
if (filepaths_file_path != NULL && getline(&curr_filepath, &filepath_len, filepaths_file) != -1) { if (filepaths_file_path != NULL && getline(&curr_filepath, &filepath_len, filepaths_file) != -1) {
// Try to read a filepath from the filepaths file // Try to read a filepath from the filepaths file
drop_newline(curr_filepath); drop_newline(curr_filepath);
...@@ -161,24 +162,17 @@ download_file(char *url, char *filepath, CURL *curl) ...@@ -161,24 +162,17 @@ download_file(char *url, char *filepath, CURL *curl)
char* char*
get_default_filepath(const char *url) get_default_filepath(const char *url)
{ {
char *url_cpy = malloc(strlen(url)+1); char *last_slash = strrchr(url, '/');
strcpy(url_cpy, url); if(last_slash == NULL)
die_if_null(url_cpy, "Malloc failed!"); last_slash = (char *)url;
else
char *tok_pointer = strtok(url_cpy, "/"); last_slash+=1; // remove '/'
char *tok_buf = tok_pointer;
//case of adress without '/' this will return adress as filename
while(tok_buf != NULL){
tok_pointer = tok_buf;
tok_buf = strtok(NULL, "/");
}
char *filepath = malloc(strlen(tok_pointer)+1); char *filepath = NULL;
die_if_null(filepath, "Malloc failed!"); filepath = malloc(strlen(last_slash)+1);
strcpy(filepath, tok_pointer); die_if_null(filepath, "Malloc failed!");
free(url_cpy); strcpy(filepath, last_slash);
return filepath; return filepath;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment