From 210a2e4c4401c23158469d07c75a5a20c4a22a13 Mon Sep 17 00:00:00 2001 From: Erich Eckner Date: Fri, 2 Nov 2018 13:15:55 +0100 Subject: input_gadgets.c: fix some memory leaks --- input_gadgets.c | 86 +++++++++++++++++++++++++++++++++------------------------ 1 file changed, 50 insertions(+), 36 deletions(-) diff --git a/input_gadgets.c b/input_gadgets.c index bd704e3..2da6abe 100644 --- a/input_gadgets.c +++ b/input_gadgets.c @@ -401,7 +401,7 @@ char *gadgets_retrieve_weather_warnings(char *output, int max_len) zip_stat_t zs; zip_error_init(&error); - if ((src = zip_source_buffer_create(chunk.memory, chunk.size, 1, &error)) == NULL) { + if ((src = zip_source_buffer_create(chunk.memory, chunk.size, 0, &error)) == NULL) { fprintf(stderr, "can't create zip source: %s\n", zip_error_strerror(&error)); zip_error_fini(&error); free(chunk.memory); @@ -422,6 +422,7 @@ char *gadgets_retrieve_weather_warnings(char *output, int max_len) warning_t *warnings = malloc(sizeof(warning_t)); if (warnings == NULL) { fprintf(stderr, "malloc failed allocating %d bytes\n", sizeof(warning_t)); + zip_discard(za); free(chunk.memory); return NULL; } @@ -439,6 +440,7 @@ char *gadgets_retrieve_weather_warnings(char *output, int max_len) free(warnings[i].event); } free(warnings); + zip_discard(za); return NULL; } int read = zip_fread(zf, inflated, zs.size); @@ -452,20 +454,22 @@ char *gadgets_retrieve_weather_warnings(char *output, int max_len) free(warnings[i].event); } free(warnings); + zip_discard(za); return NULL; } xmlDocPtr doc; doc = xmlReadMemory(inflated, zs.size, "noname.xml", NULL, 0); + free(inflated); if (doc == NULL) { fprintf(stderr, "Failed to parse document\n"); - free(inflated); free(chunk.memory); for (int i=0; i\n"); - free(inflated); free(chunk.memory); for (int i=0; i children; text = xml_extract_string(doc, cur_node, "status"); if (text == NULL) { fprintf(stderr, "Failed to enter \n"); - free(inflated); free(chunk.memory); for (int i=0; i\n"); - free(inflated); free(chunk.memory); for (int i=0; i is neither 'Alert' nor 'Cancel' nor 'Update'\n"); @@ -555,12 +559,12 @@ char *gadgets_retrieve_weather_warnings(char *output, int max_len) } free(warnings); xmlFreeDoc(doc); + zip_discard(za); return NULL; } cur_node = xml_find_node_by_name(cur_node, "info"); if (cur_node == NULL) { fprintf(stderr, "Failed to enter \n"); - free(inflated); free(chunk.memory); for (int i=0; i children; text = xml_extract_string(doc, cur_node, "category"); - if (!text) { + if (text == NULL) { fprintf(stderr, "Failed to enter %s\n",zs.name); - free(inflated); free(chunk.memory); for (int i=0; i is neither 'Met' nor 'Health'\n"); free(chunk.memory); - free(inflated); for (int i=0; i %s\n",zs.name); - free(inflated); free(chunk.memory); for (int i=0; i is neither 'Met' nor 'Health'\n"); free(chunk.memory); - free(inflated); for (int i=0; i %s\n",zs.name); - free(inflated); free(chunk.memory); for (int i=0; i %s\n",zs.name); - free(inflated); free(chunk.memory); for (int i=0; i children, "valueName"); if (text == NULL) { fprintf(stderr, "Failed to enter \n"); - free(inflated); free(chunk.memory); for (int i=0; i children, "value"); if (text == NULL) { fprintf(stderr, "Failed to enter \n"); - free(inflated); free(chunk.memory); for (int i=0; i children, 0); if (text == NULL) { fprintf(stderr, "Failed to read text of \n"); - free(inflated); free(chunk.memory); for (int i=0; i= time(NULL) + warnings_future_timespan) - continue; // start is too far ahead + is_relevant = 0; // start is too far ahead if (warnings[warnings_len].expires < time(NULL)) - continue; // warning has expired + is_relevant = 0; // warning has expired + if (!is_relevant) { + xmlFreeDoc(doc); + continue; + } text = xml_extract_string(doc, cur_node, "headline"); if (text == NULL) { fprintf(stderr, "Failed to enter \n"); - free(inflated); free(chunk.memory); for (int i=0; i\n"); - free(inflated); free(chunk.memory); for (int i=0; i