From dd4a6120ed0a77691ea6f0f2d6262dcda05ce939 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Wed, 18 Sep 2013 12:23:33 +0200 Subject: fix same memory management errors found with valgrind --- cqrlogo.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/cqrlogo.c b/cqrlogo.c index a72fd2b..a44d661 100644 --- a/cqrlogo.c +++ b/cqrlogo.c @@ -90,7 +90,7 @@ int generate_png (struct bitmap_t *bitmap, const char *uri) { # endif png_set_text(png_ptr, info_ptr, pngtext, textcount); - free(pngtext); + png_free (png_ptr, pngtext); # if PNG_ENABLE_TEXT_VERSIONS free(libsstr); # endif @@ -118,6 +118,9 @@ int generate_png (struct bitmap_t *bitmap, const char *uri) { for (y = 0; y < bitmap->height; ++y) png_free (png_ptr, row_pointers[y]); png_free (png_ptr, row_pointers); + png_destroy_write_struct(&png_ptr, &info_ptr); + + fclose(stdout); return 0; } @@ -198,15 +201,18 @@ struct bitmap_t * encode_qrcode (const char *text, unsigned int scale, unsigned /*** get_value ***/ int get_value(const char *query_string, const char *pattern, unsigned int *value, unsigned int def, unsigned int min, unsigned int max) { char *match = NULL, *newpattern = NULL; + unsigned int length; newpattern = strdup(pattern); - newpattern = realloc(newpattern, strlen(newpattern) + 2); - sprintf(newpattern + strlen(newpattern), "="); + length = strlen(newpattern); + /* length is without null termination, allocacte 4 bytes so we + * have "=", "%u" and null termination */ + newpattern = realloc(newpattern, length + 4); + sprintf(newpattern + length, "="); if ((match = strstr(query_string, newpattern)) != NULL) { - newpattern = realloc(newpattern, strlen(newpattern) + 3); - sprintf(newpattern + strlen(newpattern), "%%u"); + sprintf(newpattern + length, "%%u"); if ((sscanf(match, newpattern, value)) > 0) if (*value < min || *value > max) -- cgit v1.2.3-70-g09d2