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