summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Adler <madler@alumni.caltech.edu>2012-10-11 16:10:59 -0700
committerMark Adler <madler@alumni.caltech.edu>2012-10-11 16:10:59 -0700
commite69a9ceee0f38e817fc87930ad9792aada3d82fe (patch)
treeca990a667d85dc35ef9a3a31a6a1db7c2b8bb37b
parentc4888637eaee189c0e21259cb87ab7e5e1d4ce76 (diff)
downloadzlib-e69a9ceee0f38e817fc87930ad9792aada3d82fe.tar.gz
zlib-e69a9ceee0f38e817fc87930ad9792aada3d82fe.tar.bz2
zlib-e69a9ceee0f38e817fc87930ad9792aada3d82fe.zip
Check for input buffer malloc failure in examples/gzappend.c.
-rw-r--r--examples/gzappend.c17
1 files changed, 10 insertions, 7 deletions
diff --git a/examples/gzappend.c b/examples/gzappend.c
index 0fc42b9..662dec3 100644
--- a/examples/gzappend.c
+++ b/examples/gzappend.c
@@ -1,7 +1,7 @@
1/* gzappend -- command to append to a gzip file 1/* gzappend -- command to append to a gzip file
2 2
3 Copyright (C) 2003, 2012 Mark Adler, all rights reserved 3 Copyright (C) 2003, 2012 Mark Adler, all rights reserved
4 version 1.2, 13 Aug 2012 4 version 1.2, 11 Oct 2012
5 5
6 This software is provided 'as-is', without any express or implied 6 This software is provided 'as-is', without any express or implied
7 warranty. In no event will the author be held liable for any damages 7 warranty. In no event will the author be held liable for any damages
@@ -39,7 +39,8 @@
39 * - Keep gzip file clean on appended file read errors 39 * - Keep gzip file clean on appended file read errors
40 * - Use in-place rotate instead of auxiliary buffer 40 * - Use in-place rotate instead of auxiliary buffer
41 * (Why you ask? Because it was fun to write!) 41 * (Why you ask? Because it was fun to write!)
42 * 1.2 13 Aug 2012 - Fix for proper z_const usage 42 * 1.2 11 Oct 2012 - Fix for proper z_const usage
43 * - Check for input buffer malloc failure
43 */ 44 */
44 45
45/* 46/*
@@ -400,14 +401,14 @@ local void gztack(char *name, int gd, z_stream *strm, int last)
400 } 401 }
401 402
402 /* allocate buffers */ 403 /* allocate buffers */
403 in = fd == -1 ? NULL : malloc(CHUNK); 404 in = malloc(CHUNK);
404 out = malloc(CHUNK); 405 out = malloc(CHUNK);
405 if (out == NULL) bye("out of memory", ""); 406 if (in == NULL || out == NULL) bye("out of memory", "");
406 407
407 /* compress input file and append to gzip file */ 408 /* compress input file and append to gzip file */
408 do { 409 do {
409 /* get more input */ 410 /* get more input */
410 len = fd == -1 ? 0 : read(fd, in, CHUNK); 411 len = read(fd, in, CHUNK);
411 if (len == -1) { 412 if (len == -1) {
412 fprintf(stderr, 413 fprintf(stderr,
413 "gzappend warning: error reading %s, skipping rest ...\n", 414 "gzappend warning: error reading %s, skipping rest ...\n",
@@ -454,7 +455,7 @@ local void gztack(char *name, int gd, z_stream *strm, int last)
454 455
455 /* clean up and return */ 456 /* clean up and return */
456 free(out); 457 free(out);
457 if (in != NULL) free(in); 458 free(in);
458 if (fd > 0) close(fd); 459 if (fd > 0) close(fd);
459} 460}
460 461
@@ -472,7 +473,9 @@ int main(int argc, char **argv)
472 473
473 /* provide usage if no arguments */ 474 /* provide usage if no arguments */
474 if (*argv == NULL) { 475 if (*argv == NULL) {
475 printf("gzappend 1.1 (4 Nov 2003) Copyright (C) 2003 Mark Adler\n"); 476 printf(
477 "gzappend 1.2 (11 Oct 2012) Copyright (C) 2003, 2012 Mark Adler\n"
478 );
476 printf( 479 printf(
477 "usage: gzappend [-level] file.gz [ addthis [ andthis ... ]]\n"); 480 "usage: gzappend [-level] file.gz [ addthis [ andthis ... ]]\n");
478 return 0; 481 return 0;