| 640 | |
| 641 | #ifdef HAVE_GETDELIM |
| 642 | int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term) |
| 643 | { |
| 644 | ssize_t r; |
| 645 | |
| 646 | if (feof(fp)) |
| 647 | return EOF; |
| 648 | |
| 649 | strbuf_reset(sb); |
| 650 | |
| 651 | /* Translate slopbuf to NULL, as we cannot call realloc on it */ |
| 652 | if (!sb->alloc) |
| 653 | sb->buf = NULL; |
| 654 | errno = 0; |
| 655 | r = getdelim(&sb->buf, &sb->alloc, term, fp); |
| 656 | |
| 657 | if (r > 0) { |
| 658 | sb->len = r; |
| 659 | return 0; |
| 660 | } |
| 661 | assert(r == -1); |
| 662 | |
| 663 | /* |
| 664 | * Normally we would have called xrealloc, which will try to free |
| 665 | * memory and recover. But we have no way to tell getdelim() to do so. |
| 666 | * Worse, we cannot try to recover ENOMEM ourselves, because we have |
| 667 | * no idea how many bytes were read by getdelim. |
| 668 | * |
| 669 | * Dying here is reasonable. It mirrors what xrealloc would do on |
| 670 | * catastrophic memory failure. We skip the opportunity to free pack |
| 671 | * memory and retry, but that's unlikely to help for a malloc small |
| 672 | * enough to hold a single line of input, anyway. |
| 673 | */ |
| 674 | if (errno == ENOMEM) |
| 675 | die("Out of memory, getdelim failed"); |
| 676 | |
| 677 | /* |
| 678 | * Restore strbuf invariants; if getdelim left us with a NULL pointer, |
| 679 | * we can just re-init, but otherwise we should make sure that our |
| 680 | * length is empty, and that the result is NUL-terminated. |
| 681 | */ |
| 682 | if (!sb->buf) |
| 683 | strbuf_init(sb, 0); |
| 684 | else |
| 685 | strbuf_reset(sb); |
| 686 | return EOF; |
| 687 | } |
| 688 | #else |
| 689 | int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term) |
| 690 | { |
no test coverage detected