diff options
| author | Mark Adler <git@madler.net> | 2026-01-03 01:07:40 -0600 |
|---|---|---|
| committer | Mark Adler <git@madler.net> | 2026-01-05 15:03:04 -0600 |
| commit | fd366384cf324d750596feb03be44ddf4d1e6acd (patch) | |
| tree | 0c1d8eaa0538f8681ae0ce7a4e02ba71ff07d4fd /gzwrite.c | |
| parent | cab7352dc71048f130a7d4e0b7fd773909761133 (diff) | |
| download | zlib-fd366384cf324d750596feb03be44ddf4d1e6acd.tar.gz zlib-fd366384cf324d750596feb03be44ddf4d1e6acd.tar.bz2 zlib-fd366384cf324d750596feb03be44ddf4d1e6acd.zip | |
Prevent the use of insecure functions without an explicit request.
ZLIB_INSECURE must be defined in order to compile code that uses
the insecure functions vsprintf() or sprintf(). This would occur
only if the standard vsnprintf() or snprintf() functions are not
available. Providing the --insecure option to ./configure will
define ZLIB_INSECURE. A flag is added to zlibCompileFlags() to
indicate that gzprintf() is not implemented due to the need for
the use of an insecure function, but ZLIB_INSECURE was not
defined.
Diffstat (limited to 'gzwrite.c')
| -rw-r--r-- | gzwrite.c | 28 |
1 files changed, 26 insertions, 2 deletions
| @@ -371,6 +371,9 @@ int ZEXPORT gzputs(gzFile file, const char *s) { | |||
| 371 | return len && put == 0 ? -1 : (int)put; | 371 | return len && put == 0 ? -1 : (int)put; |
| 372 | } | 372 | } |
| 373 | 373 | ||
| 374 | #if (((!defined(STDC) && !defined(Z_HAVE_STDARG_H)) || !defined(NO_vsnprintf)) && \ | ||
| 375 | (defined(STDC) || defined(Z_HAVE_STDARG_H) || !defined(NO_snprintf))) || \ | ||
| 376 | defined(ZLIB_INSECURE) | ||
| 374 | /* If the second half of the input buffer is occupied, write out the contents. | 377 | /* If the second half of the input buffer is occupied, write out the contents. |
| 375 | If there is any input remaining due to a non-blocking stall on write, move | 378 | If there is any input remaining due to a non-blocking stall on write, move |
| 376 | it to the start of the buffer. Return true if this did not open up the | 379 | it to the start of the buffer. Return true if this did not open up the |
| @@ -391,12 +394,20 @@ local int gz_vacate(gz_statep state) { | |||
| 391 | strm->next_in = state->in; | 394 | strm->next_in = state->in; |
| 392 | return strm->avail_in > state->size; | 395 | return strm->avail_in > state->size; |
| 393 | } | 396 | } |
| 397 | #endif | ||
| 394 | 398 | ||
| 395 | #if defined(STDC) || defined(Z_HAVE_STDARG_H) | 399 | #if defined(STDC) || defined(Z_HAVE_STDARG_H) |
| 396 | #include <stdarg.h> | 400 | #include <stdarg.h> |
| 397 | 401 | ||
| 398 | /* -- see zlib.h -- */ | 402 | /* -- see zlib.h -- */ |
| 399 | int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va) { | 403 | int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va) { |
| 404 | #if defined(NO_vsnprintf) && !defined(ZLIB_INSECURE) | ||
| 405 | #warning "vsnprintf() not available -- gzprintf() stub returns Z_STREAM_ERROR" | ||
| 406 | #warning "you can recompile with ZLIB_INSECURE defined to use vsprintf()" | ||
| 407 | /* prevent use of insecure vsprintf(), unless purposefully requested */ | ||
| 408 | (void)file, (void)format, (void)va; | ||
| 409 | return Z_STREAM_ERROR; | ||
| 410 | #else | ||
| 400 | int len, ret; | 411 | int len, ret; |
| 401 | char *next; | 412 | char *next; |
| 402 | gz_statep state; | 413 | gz_statep state; |
| @@ -470,6 +481,7 @@ int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va) { | |||
| 470 | if (state->err && !state->again) | 481 | if (state->err && !state->again) |
| 471 | return state->err; | 482 | return state->err; |
| 472 | return len; | 483 | return len; |
| 484 | #endif | ||
| 473 | } | 485 | } |
| 474 | 486 | ||
| 475 | int ZEXPORTVA gzprintf(gzFile file, const char *format, ...) { | 487 | int ZEXPORTVA gzprintf(gzFile file, const char *format, ...) { |
| @@ -489,6 +501,17 @@ int ZEXPORTVA gzprintf(gzFile file, const char *format, int a1, int a2, int a3, | |||
| 489 | int a4, int a5, int a6, int a7, int a8, int a9, int a10, | 501 | int a4, int a5, int a6, int a7, int a8, int a9, int a10, |
| 490 | int a11, int a12, int a13, int a14, int a15, int a16, | 502 | int a11, int a12, int a13, int a14, int a15, int a16, |
| 491 | int a17, int a18, int a19, int a20) { | 503 | int a17, int a18, int a19, int a20) { |
| 504 | #if defined(NO_snprintf) && !defined(ZLIB_INSECURE) | ||
| 505 | #warning "snprintf() not available -- gzprintf() stub returns Z_STREAM_ERROR" | ||
| 506 | #warning "you can recompile with ZLIB_INSECURE defined to use sprintf()" | ||
| 507 | /* prevent use of insecure sprintf(), unless purposefully requested */ | ||
| 508 | (void)file, (void)format, (void)a1, (void)a2, (void)a3, (void)a4, (void)a5, | ||
| 509 | (void)a6, (void)a7, (void)a8, (void)a9, (void)a10, (void)a11, (void)a12, | ||
| 510 | (void)a13, (void)a14, (void)a15, (void)a16, (void)a17, (void)a18, | ||
| 511 | (void)a19, (void)a20; | ||
| 512 | return Z_STREAM_ERROR; | ||
| 513 | #else | ||
| 514 | int ret; | ||
| 492 | unsigned len, left; | 515 | unsigned len, left; |
| 493 | char *next; | 516 | char *next; |
| 494 | gz_statep state; | 517 | gz_statep state; |
| @@ -511,11 +534,11 @@ int ZEXPORTVA gzprintf(gzFile file, const char *format, int a1, int a2, int a3, | |||
| 511 | 534 | ||
| 512 | /* make sure we have some buffer space */ | 535 | /* make sure we have some buffer space */ |
| 513 | if (state->size == 0 && gz_init(state) == -1) | 536 | if (state->size == 0 && gz_init(state) == -1) |
| 514 | return state->error; | 537 | return state->err; |
| 515 | 538 | ||
| 516 | /* check for seek request */ | 539 | /* check for seek request */ |
| 517 | if (state->skip && gz_zero(state) == -1) | 540 | if (state->skip && gz_zero(state) == -1) |
| 518 | return state->error; | 541 | return state->err; |
| 519 | 542 | ||
| 520 | /* do the printf() into the input buffer, put length in len -- the input | 543 | /* do the printf() into the input buffer, put length in len -- the input |
| 521 | buffer is double-sized just for this function, so there is guaranteed to | 544 | buffer is double-sized just for this function, so there is guaranteed to |
| @@ -571,6 +594,7 @@ int ZEXPORTVA gzprintf(gzFile file, const char *format, int a1, int a2, int a3, | |||
| 571 | if (state->err && !state->again) | 594 | if (state->err && !state->again) |
| 572 | return state->err; | 595 | return state->err; |
| 573 | return (int)len; | 596 | return (int)len; |
| 597 | #endif | ||
| 574 | } | 598 | } |
| 575 | 599 | ||
| 576 | #endif | 600 | #endif |
