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)
| 580 | allocation failure constructing the error message, then convert the error to |
| 581 | out of memory. */ |
| 582 | void ZLIB_INTERNAL gz_error(state, err, msg) |
| 583 | gz_statep state; |
| 584 | int err; |
| 585 | const char *msg; |
| 586 | { |
| 587 | /* free previously allocated message and clear */ |
| 588 | if (state->msg != NULL) { |
| 589 | if (state->err != Z_MEM_ERROR) |
| 590 | free(state->msg); |
| 591 | state->msg = NULL; |
| 592 | } |
| 593 | |
| 594 | /* if fatal, set state->x.have to 0 so that the gzgetc() macro fails */ |
| 595 | if (err != Z_OK && err != Z_BUF_ERROR) |
| 596 | state->x.have = 0; |
| 597 | |
| 598 | /* set error code, and if no message, then done */ |
| 599 | state->err = err; |
| 600 | if (msg == NULL) |
| 601 | return; |
| 602 | |
| 603 | /* for an out of memory error, return literal string when requested */ |
| 604 | if (err == Z_MEM_ERROR) |
| 605 | return; |
| 606 | |
| 607 | /* construct error message with path */ |
| 608 | if ((state->msg = (char *)malloc(strlen(state->path) + strlen(msg) + 3)) == |
| 609 | NULL) { |
| 610 | state->err = Z_MEM_ERROR; |
| 611 | return; |
| 612 | } |
| 613 | #if !defined(NO_snprintf) && !defined(NO_vsnprintf) |
| 614 | (void)snprintf(state->msg, strlen(state->path) + strlen(msg) + 3, |
| 615 | "%s%s%s", state->path, ": ", msg); |
| 616 | #else |
| 617 | strcpy(state->msg, state->path); |
| 618 | strcat(state->msg, ": "); |
| 619 | strcat(state->msg, msg); |
| 620 | #endif |
| 621 | } |
| 622 | |
| 623 | #ifndef INT_MAX |
| 624 | /* portably return maximum value for an int (when limits.h presumed not |
no test coverage detected