Skip to content

Commit

Permalink
Fix a memory leak in pngtest.c
Browse files Browse the repository at this point in the history
Ensure that row_buf is deallocated not only after a read error, but
also after a write error.

Use the format "%p" instead of "0x%08lx" for printf-ing row_buf in
a portable manner.
  • Loading branch information
ctruta committed Feb 4, 2019
1 parent 70d122a commit 8439534
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions pngtest.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

/* pngtest.c - a simple test program to test libpng
*
* Copyright (c) 2018 Cosmin Truta
* Copyright (c) 2018-2019 Cosmin Truta
* Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson
* Copyright (c) 1996-1997 Andreas Dilger
* Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.
Expand Down Expand Up @@ -957,6 +957,8 @@ test_one_file(const char *inname, const char *outname)
if (setjmp(png_jmpbuf(write_ptr)))
{
fprintf(STDERR, "%s -> %s: libpng write error\n", inname, outname);
png_free(read_ptr, row_buf);
row_buf = NULL;
if (verbose != 0)
fprintf(STDERR, " destroying read structs\n");
png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
Expand Down Expand Up @@ -1436,7 +1438,7 @@ test_one_file(const char *inname, const char *outname)
row_buf = (png_bytep)png_malloc(read_ptr,
png_get_rowbytes(read_ptr, read_info_ptr));

pngtest_debug1("\t0x%08lx", (unsigned long)row_buf);
pngtest_debug1("\t%p", row_buf);
#endif /* SINGLE_ROWBUF_ALLOC */
pngtest_debug("Writing row data");

Expand Down Expand Up @@ -1490,7 +1492,7 @@ test_one_file(const char *inname, const char *outname)
row_buf = (png_bytep)png_malloc(read_ptr,
png_get_rowbytes(read_ptr, read_info_ptr));

pngtest_debug2("\t0x%08lx (%lu bytes)", (unsigned long)row_buf,
pngtest_debug2("\t%p (%lu bytes)", row_buf,
(unsigned long)png_get_rowbytes(read_ptr, read_info_ptr));

#endif /* !SINGLE_ROWBUF_ALLOC */
Expand Down

0 comments on commit 8439534

Please sign in to comment.