From 19447052d8a7fc37c1ba0207f608b51a9fa03c04 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Mon, 12 Dec 2022 11:40:52 +0000 Subject: httpd: hoist out err_close from ferror|fclose A small tweak to simplify error reporting if we have any problems due to OOM or bugs. --- mwrap_httpd.h | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/mwrap_httpd.h b/mwrap_httpd.h index 6856208..b55bb02 100644 --- a/mwrap_httpd.h +++ b/mwrap_httpd.h @@ -212,6 +212,13 @@ static FILE *wbuf_new(struct mw_membuf *mb) return fp; } +static int err_close(FILE *fp) +{ + int e = ferror(fp) | fclose(fp); + if (e) fprintf(stderr, "ferror|fclose: %m\n"); + return e; +} + static enum mw_qev h1_res_oneshot(struct mw_h1 *h1, const char *buf, size_t len) { struct mw_membuf mb; @@ -220,10 +227,8 @@ static enum mw_qev h1_res_oneshot(struct mw_h1 *h1, const char *buf, size_t len) if (!fp) return h1_close(h1); fwrite(buf, 1, len, fp); - if (ferror(fp) | fclose(fp)) { - fprintf(stderr, "ferror|fclose: %m\n"); + if (err_close(fp)) return h1_close(h1); - } /* fill in the zero padding we added at wbuf_new */ mwrap_assert(!h1->wbuf); @@ -258,10 +263,8 @@ static enum mw_qev h1_200(struct mw_h1 *h1, FILE *fp, struct mw_membuf *mb) fprintf(fp, "%zu", (size_t)clen); FPUTS("\r\n\r\n", fp); - if (ferror(fp) | fclose(fp)) { - fprintf(stderr, "ferror|fclose: %m\n"); + if (err_close(fp)) return h1_close(h1); - } /* fill in the zero-padding we added at wbuf_new */ mwrap_assert(!h1->wbuf); @@ -462,8 +465,7 @@ static struct h1_src_loc *accumulate(unsigned long min, size_t *hslc, FILE *lp) rcu_read_unlock(); struct h1_src_loc *hslv; - if (ferror(fp) | fclose(fp)) { - fprintf(stderr, "ferror|fclose: %m\n"); + if (err_close(fp)) { hslv = NULL; } else { *hslc = mb.len / sizeof(*hslv); @@ -489,10 +491,9 @@ static enum mw_qev each_at(struct mw_h1 *h1, struct mw_h1req *h1r) FILE *lp = open_memstream(&lname.ptr, &lname.len); if (!lp) return h1_close(h1); if (write_loc_name(lp, l) < 0) return h1_close(h1); - if (ferror(lp) | fclose(lp)) { - fprintf(stderr, "ferror|fclose: %m\n"); + if (err_close(lp)) return h1_close(h1); - } + struct mw_membuf mb; FILE *fp = wbuf_new(&mb); if (!fp) return h1_close(h1); @@ -544,8 +545,7 @@ static enum mw_qev each_gt(struct mw_h1 *h1, struct mw_h1req *h1r, if (!hslv) return h1_close(h1); - if (ferror(lp) | fclose(lp)) { - fprintf(stderr, "ferror|fclose: %m\n"); + if (err_close(lp)) { free(hslv); return h1_close(h1); } @@ -863,8 +863,7 @@ static struct pollfd *poll_detach(struct mw_h1d *h1d, nfds_t *nfds) /* not sure how to best recover from ENOMEM errors in stdio */ if (h1d->pfp) { - if (ferror(h1d->pfp) | fclose(h1d->pfp)) { - fprintf(stderr, "ferror|fclose: %m\n"); + if (err_close(h1d->pfp)) { exit(EXIT_FAILURE); } else { mwrap_assert(h1d->pbuf.len % sizeof(*pfd) == 0); -- cgit v1.2.3-24-ge0c7