From d8ccb037a0b9eeb643a8aed08279f052d20ef4a4 Mon Sep 17 00:00:00 2001 From: Rikard Falkeborn Date: Tue, 27 Nov 2018 23:21:25 +0100 Subject: [PATCH 1/3] Fix memset size In the call to memset(), the size of the pointer to mag2buf was erroneously used instead of size of what mag2buf pointed to (float). This leads to problems on systems where the size of the pointer is different from the size of floats. Instead of just fixing the size, use calloc() instead of malloc() to directly set the memory to 0. --- tools/psdpng.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tools/psdpng.c b/tools/psdpng.c index 15e640b..91c02dd 100644 --- a/tools/psdpng.c +++ b/tools/psdpng.c @@ -129,9 +129,7 @@ void transform_signal(void) CHECKNULL( inbuf=(short*)malloc(sizeof(short)*2*nfft ) ); CHECKNULL( tbuf=(kiss_fft_scalar*)malloc(sizeof(kiss_fft_scalar)*nfft ) ); CHECKNULL( fbuf=(kiss_fft_cpx*)malloc(sizeof(kiss_fft_cpx)*nfreqs ) ); - CHECKNULL( mag2buf=(float*)malloc(sizeof(float)*nfreqs ) ); - - memset(mag2buf,0,sizeof(mag2buf)*nfreqs); + CHECKNULL( mag2buf=(float*)calloc(nfreqs,sizeof(float) ) ); while (1) { if (stereo) { From 5c845c5006beec35150b89712823974305049f0b Mon Sep 17 00:00:00 2001 From: Rikard Falkeborn Date: Tue, 27 Nov 2018 23:29:37 +0100 Subject: [PATCH 2/3] Free memory This makes psdpng run clean under address sanitizer and valgrind. --- tools/psdpng.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tools/psdpng.c b/tools/psdpng.c index 91c02dd..adf1f2d 100644 --- a/tools/psdpng.c +++ b/tools/psdpng.c @@ -211,6 +211,9 @@ void make_png(void) png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY , NULL); + free(row_pointers); + free(row_data); + png_destroy_write_struct(&png_ptr, &info_ptr); } int main(int argc,char ** argv) @@ -223,5 +226,6 @@ int main(int argc,char ** argv) if (fout!=stdout) fclose(fout); if (fin!=stdin) fclose(fin); + free(vals); return 0; } From cfa9afee312ec1254c3d5fdc789e4d0a5d6649ba Mon Sep 17 00:00:00 2001 From: Rikard Falkeborn Date: Tue, 27 Nov 2018 23:40:21 +0100 Subject: [PATCH 3/3] Add checks for memory failures --- tools/psdpng.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/psdpng.c b/tools/psdpng.c index adf1f2d..5170c8c 100644 --- a/tools/psdpng.c +++ b/tools/psdpng.c @@ -162,7 +162,7 @@ void transform_signal(void) if (++avgctr == navg) { avgctr=0; ++nrows; - vals = (float*)realloc(vals,sizeof(float)*nrows*nfreqs); + CHECKNULL( vals = (float*)realloc(vals,sizeof(float)*nrows*nfreqs) ); float eps = 1; for (i=0;i