Create an error message in allocated memory and set state->err and state->msg accordingly. Free any previous error message already there. Do not try to free or allocate space if the error is Z_MEM_ERROR (out of memory). Simply save the error message as a static string. If there is an allocation failure constructing the error message, then convert the error to out of memory. */
(state, err, msg)
| 615 | allocation failure constructing the error message, then convert the error to |
| 616 | out of memory. */ |
| 617 | void ZLIB_INTERNAL gz_error(state, err, msg) |
| 618 | gz_statep state; |
| 619 | int err; |
| 620 | |
| 621 | const char* msg; |
| 622 | { |
| 623 | /* free previously allocated message and clear */ |
| 624 | if (state->msg != NULL) |
| 625 | { |
| 626 | if (state->err != Z_MEM_ERROR) |
| 627 | free(state->msg); |
| 628 | state->msg = NULL; |
| 629 | } |
| 630 | |
| 631 | /* if fatal, set state->x.have to 0 so that the gzgetc() macro fails */ |
| 632 | if (err != Z_OK && err != Z_BUF_ERROR) |
| 633 | state->x.have = 0; |
| 634 | |
| 635 | /* set error code, and if no message, then done */ |
| 636 | state->err = err; |
| 637 | if (msg == NULL) |
| 638 | return; |
| 639 | |
| 640 | /* for an out of memory error, return literal string when requested */ |
| 641 | if (err == Z_MEM_ERROR) |
| 642 | return; |
| 643 | |
| 644 | /* construct error message with path */ |
| 645 | if ((state->msg = (char *)malloc(strlen(state->path) + strlen(msg) + 3)) == |
| 646 | NULL) |
| 647 | { |
| 648 | state->err = Z_MEM_ERROR; |
| 649 | return; |
| 650 | } |
| 651 | #if !defined(NO_snprintf) && !defined(NO_vsnprintf) |
| 652 | (void)snprintf(state->msg, strlen(state->path) + strlen(msg) + 3, |
| 653 | "%s%s%s", state->path, ": ", msg); |
| 654 | #else |
| 655 | strcpy(state->msg, state->path); |
| 656 | strcat(state->msg, ": "); |
| 657 | strcat(state->msg, msg); |
| 658 | #endif |
| 659 | } |
| 660 | |
| 661 | #ifndef INT_MAX |
| 662 | /* portably return maximum value for an int (when limits.h presumed not |
no outgoing calls
no test coverage detected