summaryrefslogtreecommitdiff
path: root/input_gadgets.c
diff options
context:
space:
mode:
authorErich Eckner <git@eckner.net>2018-11-02 13:15:55 +0100
committerErich Eckner <git@eckner.net>2018-11-02 13:15:55 +0100
commit210a2e4c4401c23158469d07c75a5a20c4a22a13 (patch)
treeb04b65e52779f1d357ea071e16026875b4307627 /input_gadgets.c
parent846295cb7cdf0828a712b9e052c88c99fa220b50 (diff)
downloadanzeige-210a2e4c4401c23158469d07c75a5a20c4a22a13.tar.xz
input_gadgets.c: fix some memory leaks
Diffstat (limited to 'input_gadgets.c')
-rw-r--r--input_gadgets.c86
1 files 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<warnings_len; i++) {
free(warnings[i].headline);
free(warnings[i].event);
}
free(warnings);
+ zip_discard(za);
return NULL;
}
@@ -476,7 +480,6 @@ char *gadgets_retrieve_weather_warnings(char *output, int max_len)
cur_node = xmlDocGetRootElement(doc);
if (cur_node == NULL) {
fprintf(stderr, "Failed to reach root node of xml\n");
- free(inflated);
free(chunk.memory);
for (int i=0; i<warnings_len; i++) {
free(warnings[i].headline);
@@ -484,12 +487,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, "alert");
if (cur_node == NULL) {
fprintf(stderr, "Failed to enter <alert>\n");
- free(inflated);
free(chunk.memory);
for (int i=0; i<warnings_len; i++) {
free(warnings[i].headline);
@@ -497,13 +500,14 @@ char *gadgets_retrieve_weather_warnings(char *output, int max_len)
}
free(warnings);
xmlFreeDoc(doc);
+ zip_discard(za);
return NULL;
}
+
cur_node = cur_node -> children;
text = xml_extract_string(doc, cur_node, "status");
if (text == NULL) {
fprintf(stderr, "Failed to enter <status>\n");
- free(inflated);
free(chunk.memory);
for (int i=0; i<warnings_len; i++) {
free(warnings[i].headline);
@@ -511,20 +515,20 @@ char *gadgets_retrieve_weather_warnings(char *output, int max_len)
}
free(warnings);
xmlFreeDoc(doc);
+ zip_discard(za);
return NULL;
}
if (strcmp((char *)text, "Actual") != 0) { // Warning is not an Actual warning
printf("no warning, but '%s'\n",text);
xmlFree(text);
- free(inflated);
xmlFreeDoc(doc);
+ zip_discard(za);
continue;
}
xmlFree(text);
text = xml_extract_string(doc, cur_node, "msgType");
if (text == NULL) {
fprintf(stderr, "Failed to enter <msgType>\n");
- free(inflated);
free(chunk.memory);
for (int i=0; i<warnings_len; i++) {
free(warnings[i].headline);
@@ -532,6 +536,7 @@ char *gadgets_retrieve_weather_warnings(char *output, int max_len)
}
free(warnings);
xmlFreeDoc(doc);
+ zip_discard(za);
return NULL;
}
if (strcmp((char *)text, "Update") == 0)
@@ -544,7 +549,6 @@ char *gadgets_retrieve_weather_warnings(char *output, int max_len)
warnings[warnings_len].is_update=-2;
xmlFree(text);
if (warnings[warnings_len].is_update < 0) {
- free(inflated);
if (warnings[warnings_len].is_update == -1) // "Cancel"
continue;
fprintf(stderr, "<msgType> 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 <info>\n");
- free(inflated);
free(chunk.memory);
for (int i=0; i<warnings_len; i++) {
free(warnings[i].headline);
@@ -568,13 +572,13 @@ char *gadgets_retrieve_weather_warnings(char *output, int max_len)
}
free(warnings);
xmlFreeDoc(doc);
+ zip_discard(za);
return NULL;
}
cur_node = cur_node -> children;
text = xml_extract_string(doc, cur_node, "category");
- if (!text) {
+ if (text == NULL) {
fprintf(stderr, "Failed to enter <category> %s\n",zs.name);
- free(inflated);
free(chunk.memory);
for (int i=0; i<warnings_len; i++) {
free(warnings[i].headline);
@@ -582,6 +586,7 @@ char *gadgets_retrieve_weather_warnings(char *output, int max_len)
}
free(warnings);
xmlFreeDoc(doc);
+ zip_discard(za);
return NULL;
}
if (strcmp((char *)text,"Met") == 0)
@@ -594,19 +599,18 @@ char *gadgets_retrieve_weather_warnings(char *output, int max_len)
if (warnings[warnings_len].category == -1) {
fprintf(stderr, "<category> is neither 'Met' nor 'Health'\n");
free(chunk.memory);
- free(inflated);
for (int i=0; i<warnings_len; i++) {
free(warnings[i].headline);
free(warnings[i].event);
}
free(warnings);
xmlFreeDoc(doc);
+ zip_discard(za);
return NULL;
}
text = xml_extract_string(doc, cur_node, "responseType");
- if (!text) {
+ if (text == NULL) {
fprintf(stderr, "Failed to enter <responseType> %s\n",zs.name);
- free(inflated);
free(chunk.memory);
for (int i=0; i<warnings_len; i++) {
free(warnings[i].headline);
@@ -614,6 +618,7 @@ char *gadgets_retrieve_weather_warnings(char *output, int max_len)
}
free(warnings);
xmlFreeDoc(doc);
+ zip_discard(za);
return NULL;
}
if ((strcmp((char *)text,"Prepare") != 0) && (strcmp((char *)text,"None")!=0)) {
@@ -626,19 +631,18 @@ char *gadgets_retrieve_weather_warnings(char *output, int max_len)
if (warnings[warnings_len].category == -1) {
fprintf(stderr, "<category> is neither 'Met' nor 'Health'\n");
free(chunk.memory);
- free(inflated);
for (int i=0; i<warnings_len; i++) {
free(warnings[i].headline);
free(warnings[i].event);
}
free(warnings);
xmlFreeDoc(doc);
+ zip_discard(za);
return NULL;
}
text = xml_extract_string(doc, cur_node, "severity");
- if (!text) {
+ if (text == NULL) {
fprintf(stderr, "Failed to enter <severity> %s\n",zs.name);
- free(inflated);
free(chunk.memory);
for (int i=0; i<warnings_len; i++) {
free(warnings[i].headline);
@@ -646,6 +650,7 @@ char *gadgets_retrieve_weather_warnings(char *output, int max_len)
}
free(warnings);
xmlFreeDoc(doc);
+ zip_discard(za);
return NULL;
}
if (strcmp((char *)text,"Minor") == 0)
@@ -659,7 +664,6 @@ char *gadgets_retrieve_weather_warnings(char *output, int max_len)
else {
fprintf(stderr, "Unknown severity '%s'\n",text);
xmlFree(text);
- free(inflated);
free(chunk.memory);
for (int i=0; i<warnings_len; i++) {
free(warnings[i].headline);
@@ -667,6 +671,7 @@ char *gadgets_retrieve_weather_warnings(char *output, int max_len)
}
free(warnings);
xmlFreeDoc(doc);
+ zip_discard(za);
return NULL;
}
xmlFree(text);
@@ -675,7 +680,6 @@ char *gadgets_retrieve_weather_warnings(char *output, int max_len)
text = xml_extract_string(doc, cur_node, "onset");
if (text == NULL) {
fprintf(stderr, "Failed to enter <onset> %s\n",zs.name);
- free(inflated);
free(chunk.memory);
for (int i=0; i<warnings_len; i++) {
free(warnings[i].headline);
@@ -683,12 +687,12 @@ char *gadgets_retrieve_weather_warnings(char *output, int max_len)
}
free(warnings);
xmlFreeDoc(doc);
+ zip_discard(za);
return NULL;
}
if (strptime((char *)text,"%FT%T+",&time_struct) == NULL) {
fprintf(stderr, "Failed to parse time '%s'\n",text);
xmlFree(text);
- free(inflated);
free(chunk.memory);
for (int i=0; i<warnings_len; i++) {
free(warnings[i].headline);
@@ -696,16 +700,16 @@ char *gadgets_retrieve_weather_warnings(char *output, int max_len)
}
free(warnings);
xmlFreeDoc(doc);
+ zip_discard(za);
return NULL;
}
xmlFree(text);
warnings[warnings_len].onset = mktime(&time_struct);
text = xml_extract_string(doc, cur_node, "expires");
- if (text) {
+ if (text != NULL) {
if (strptime((char *)text,"%FT%T+",&time_struct) == NULL) {
fprintf(stderr, "Failed to parse time '%s'\n",text);
xmlFree(text);
- free(inflated);
free(chunk.memory);
for (int i=0; i<warnings_len; i++) {
free(warnings[i].headline);
@@ -713,6 +717,7 @@ char *gadgets_retrieve_weather_warnings(char *output, int max_len)
}
free(warnings);
xmlFreeDoc(doc);
+ zip_discard(za);
return NULL;
}
xmlFree(text);
@@ -727,7 +732,6 @@ char *gadgets_retrieve_weather_warnings(char *output, int max_len)
text = xml_extract_string(doc, sub_sub_node -> children, "valueName");
if (text == NULL) {
fprintf(stderr, "Failed to enter <valueName>\n");
- free(inflated);
free(chunk.memory);
for (int i=0; i<warnings_len; i++) {
free(warnings[i].headline);
@@ -735,6 +739,7 @@ char *gadgets_retrieve_weather_warnings(char *output, int max_len)
}
free(warnings);
xmlFreeDoc(doc);
+ zip_discard(za);
return NULL;
}
if (strcmp((char *)text,"WARNCELLID")!=0) {
@@ -745,7 +750,6 @@ char *gadgets_retrieve_weather_warnings(char *output, int max_len)
text = xml_extract_string(doc, sub_sub_node -> children, "value");
if (text == NULL) {
fprintf(stderr, "Failed to enter <value>\n");
- free(inflated);
free(chunk.memory);
for (int i=0; i<warnings_len; i++) {
free(warnings[i].headline);
@@ -753,6 +757,7 @@ char *gadgets_retrieve_weather_warnings(char *output, int max_len)
}
free(warnings);
xmlFreeDoc(doc);
+ zip_discard(za);
return NULL;
}
if (strcmp((char *)text,"816053000")==0) // Stadt Jena
@@ -763,7 +768,6 @@ char *gadgets_retrieve_weather_warnings(char *output, int max_len)
text = xmlNodeListGetString(doc, sub_sub_node -> children, 0);
if (text == NULL) {
fprintf(stderr, "Failed to read text of <polygon>\n");
- free(inflated);
free(chunk.memory);
for (int i=0; i<warnings_len; i++) {
free(warnings[i].headline);
@@ -771,6 +775,7 @@ char *gadgets_retrieve_weather_warnings(char *output, int max_len)
}
free(warnings);
xmlFreeDoc(doc);
+ zip_discard(za);
return NULL;
}
int ret = point_is_inside_polygon(50.8830, 11.6223, (char *)text);
@@ -779,23 +784,31 @@ char *gadgets_retrieve_weather_warnings(char *output, int max_len)
else if (ret != 0) {
fprintf(stderr, "point_is_inside_polygon failed to parse '%s'\n", (char *)text);
xmlFree(text);
+ free(chunk.memory);
+ for (int i=0; i<warnings_len; i++) {
+ free(warnings[i].headline);
+ free(warnings[i].event);
+ }
+ free(warnings);
+ xmlFreeDoc(doc);
+ zip_discard(za);
return NULL;
}
xmlFree(text);
}
}
- if (!is_relevant)
- continue;
-
if (warnings[warnings_len].onset >= 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 <headline>\n");
- free(inflated);
free(chunk.memory);
for (int i=0; i<warnings_len; i++) {
free(warnings[i].headline);
@@ -803,12 +816,12 @@ char *gadgets_retrieve_weather_warnings(char *output, int max_len)
}
free(warnings);
xmlFreeDoc(doc);
+ zip_discard(za);
return NULL;
}
warnings[warnings_len].headline = malloc(strlen((char *)text)+1);
if (warnings[warnings_len].headline == NULL) {
fprintf(stderr, "malloc failed allocating %d bytes\n", strlen((char *)text)+1);
- free(inflated);
for (int i=0; i<warnings_len; i++) {
free(warnings[i].headline);
free(warnings[i].event);
@@ -816,6 +829,7 @@ char *gadgets_retrieve_weather_warnings(char *output, int max_len)
free(warnings);
free(chunk.memory);
xmlFreeDoc(doc);
+ zip_discard(za);
return NULL;
}
strcpy(warnings[warnings_len].headline, (char *)text);
@@ -824,7 +838,6 @@ char *gadgets_retrieve_weather_warnings(char *output, int max_len)
text = xml_extract_string(doc, cur_node, "event");
if (text == NULL) {
fprintf(stderr, "Failed to enter <event>\n");
- free(inflated);
free(chunk.memory);
for (int i=0; i<warnings_len; i++) {
free(warnings[i].headline);
@@ -833,12 +846,12 @@ char *gadgets_retrieve_weather_warnings(char *output, int max_len)
free(warnings[warnings_len].headline);
free(warnings);
xmlFreeDoc(doc);
+ zip_discard(za);
return NULL;
}
warnings[warnings_len].event = malloc(strlen((char *)text)+1);
if (warnings[warnings_len].event == NULL) {
fprintf(stderr, "malloc failed allocating %d bytes\n", strlen((char *)text)+1);
- free(inflated);
for (int i=0; i<warnings_len; i++) {
free(warnings[i].headline);
free(warnings[i].event);
@@ -847,6 +860,7 @@ char *gadgets_retrieve_weather_warnings(char *output, int max_len)
free(warnings);
free(chunk.memory);
xmlFreeDoc(doc);
+ zip_discard(za);
return NULL;
}
strcpy(warnings[warnings_len].event, (char *)text);
@@ -855,7 +869,6 @@ char *gadgets_retrieve_weather_warnings(char *output, int max_len)
warnings = (warning_t *)realloc(warnings, (++warnings_len + 1) * sizeof(warning_t));
if (warnings == NULL) {
fprintf(stderr, "realloc failed allocating %d bytes\n", (warnings_len + 1) * sizeof(warning_t));
- free(inflated);
for (int i=0; i<warnings_len; i++) {
free(warnings[i].headline);
free(warnings[i].event);
@@ -863,11 +876,12 @@ char *gadgets_retrieve_weather_warnings(char *output, int max_len)
free(warnings);
free(chunk.memory);
xmlFreeDoc(doc);
+ zip_discard(za);
return NULL;
}
- free(inflated);
xmlFreeDoc(doc);
}
+ zip_discard(za);
free(chunk.memory);
char *ende = output;