From 2e85e022ab34f32c6a5c612996b8eadb709ba59e Mon Sep 17 00:00:00 2001 From: MadMaurice Date: Sun, 27 Dec 2020 00:11:03 +0100 Subject: [PATCH] Fix memory leaks --- http.c | 29 +++++++++++++---------------- lichess.c | 15 ++++++++------- main.c | 33 +++++++++++++++------------------ 3 files changed, 36 insertions(+), 41 deletions(-) diff --git a/http.c b/http.c index 42d5bd4..7cd6422 100644 --- a/http.c +++ b/http.c @@ -4,40 +4,37 @@ #include #include -int curl_setup = 0; +static CURL* httpHandler = NULL; + char* request(char *url) { // initialize curl on the first call of this function - if(curl_setup == 0) + if(!httpHandler) { - curl_global_init(CURL_GLOBAL_DEFAULT); - curl_setup=1; + httpHandler = curl_easy_init(); + curl_easy_setopt(httpHandler,CURLOPT_USERAGENT, "cWikiBot/0.1 (https://git.zom.bi/cpp/cWikiBot; cpp@zom.bi) libcurl4/7.64.0"); + curl_easy_setopt(httpHandler,CURLOPT_WRITEFUNCTION, httpResponseCallback); } - + struct httpResponse_t httpResponse; httpResponse.response = malloc(1); httpResponse.size = 0; - CURL *httpHandler = curl_easy_init(); CURLcode return_code; if(httpHandler) { - for(int i=0;i<128;i++) - { - fprintf(stderr,"%x",url[i]); - } curl_easy_setopt(httpHandler,CURLOPT_URL, url); - curl_easy_setopt(httpHandler,CURLOPT_WRITEFUNCTION, httpResponseCallback); - curl_easy_setopt(httpHandler,CURLOPT_USERAGENT, "cWikiBot/0.1 (https://git.zom.bi/cpp/cWikiBot; cpp@zom.bi) libcurl4/7.64.0"); curl_easy_setopt(httpHandler,CURLOPT_WRITEDATA, (void *)&httpResponse); return_code = curl_easy_perform(httpHandler); } else { - fprintf(stderr, "CURL Error"); + fprintf(stderr, "CURL Error\n"); + free(httpResponse.response); return NULL; } if(return_code != CURLE_OK) { - fprintf(stderr, "HTTP Error: %s\n",curl_easy_strerror(return_code)); + fprintf(stderr, "HTTP Error: %s (%d)\n",curl_easy_strerror(return_code), return_code); + free(httpResponse.response); return NULL; } return httpResponse.response; @@ -47,7 +44,7 @@ size_t httpResponseCallback(char *data, size_t wordlength, size_t bytecount, voi // wordlength should be always 1, but this appears to be more secure. size_t size = wordlength * bytecount; struct httpResponse_t *mem = (struct httpResponse_t *) out; - + char *newData = realloc(mem->response, mem->size + size +1); if(newData == NULL) return 0; @@ -56,6 +53,6 @@ size_t httpResponseCallback(char *data, size_t wordlength, size_t bytecount, voi mem->size += size; // Null-terminate the byte chunk, to effectively have a C-String. mem->response[mem-> size] = 0; - + return size; } diff --git a/lichess.c b/lichess.c index 3c80436..e4949af 100644 --- a/lichess.c +++ b/lichess.c @@ -4,6 +4,7 @@ #include #include #include +#include json_object* query_lichess(char* username) { @@ -13,22 +14,22 @@ json_object* query_lichess(char* username) char* api_base = "https://lichess.org/api/user/"; char user_url[128]; int retries = 5; - //strcpy(user_url,api_base); - //strcat(user_url,username); - int user_url_length = sprintf(user_url,"%s%s\0",api_base,username); - user_url[user_url_length]=0; + int user_url_length = sprintf(user_url,"%s%s",api_base,username); printf("querying %s\n",user_url); char* http_response = request(user_url); while(http_response == NULL) { - printf("didn't receive HTTP response, we might be rate-limited. Waiting 60s"); - sleep(60); + printf("didn't receive HTTP response, we might be rate-limited. Waiting 5s.\n"); + sleep(5); http_response = request(user_url); retries--; - if(retries == 0) + if(retries == 0 && http_response == NULL) return NULL; } json_object *lichessUser = json_tokener_parse(http_response); + free(http_response); json_object *profile = json_object_object_get(lichessUser,"profile"); + json_object_get(profile); + json_object_put(lichessUser); return profile; } diff --git a/main.c b/main.c index a1f4f0d..8a6c42a 100644 --- a/main.c +++ b/main.c @@ -1,28 +1,29 @@ /* * main.c - * + * * Copyright 2020 - * + * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301, USA. - * - * + * + * */ - + #include #include +#include #include #include @@ -42,12 +43,11 @@ int main(int argc, char **argv) fputs("lichess_id,name\n",output); char line [128]; int j=0; - while(fgets(line, sizeof line, playersfile) != NULL) + while(fgets(line, 128, playersfile) != NULL) { - printf("%i",j); + printf("%i: ",j); j++; char outline[256]; - char name[128]; line[strlen(line)-1] = '\0'; json_object *profile = query_lichess(line); if(profile == NULL) @@ -55,21 +55,18 @@ int main(int argc, char **argv) printf("No profile for %s\n", line); continue; } - char *firstName = json_object_get_string(json_object_object_get(profile,"firstName")); - char *lastName = json_object_get_string(json_object_object_get(profile,"lastName")); + const char *firstName = json_object_get_string(json_object_object_get(profile,"firstName")); + const char *lastName = json_object_get_string(json_object_object_get(profile,"lastName")); if(firstName == NULL || lastName == NULL) { printf("No name for %s\n",line); + json_object_put(profile); continue; } - strcpy(name,firstName); - strcat(name," "); - strcat(name,lastName); - sprintf(outline, "%s,%s\n",line,name); + sprintf(outline, "%s,%s %s\n",line,firstName,lastName); printf("OK\n"); fputs(outline,output); - fclose(output); - fopen("output.csv","a"); + json_object_put(profile); } fclose(output); fclose(playersfile);