From ad2ab22d00cb05f006a59f05cc2ff6879e7e7ba0 Mon Sep 17 00:00:00 2001 From: Xavier Del Campo Romero Date: Sat, 29 Apr 2023 00:31:12 +0200 Subject: Return error if write_ctx_free fails Otherwise, write_body_mem and write_body_mem would silently fail, causing undefined behaviour. Notes: The return value for write_ctx_free is currently assigned to that of fclose(3), which can be either 0 on success or EOF on failure. However, it makes sense for write_body_mem and write_body_mem to simply check against non-zero. Also, it would not be sensible to return EOF to caller functions, which expect either 0 (success), -1 (fatal error) or 1 (input error). --- http.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/http.c b/http.c index 5308fa5..feb74eb 100644 --- a/http.c +++ b/http.c @@ -599,7 +599,7 @@ static int write_body_mem(struct http_ctx *const h, bool *const close) struct write_ctx *const w = &h->wctx; const struct http_response *const r = &w->r; const size_t rem = r->n - w->n; - int res = h->cfg.write((const char *)r->buf.ro + w->n, rem, + const int res = h->cfg.write((const char *)r->buf.ro + w->n, rem, h->cfg.user); if (res <= 0) @@ -608,8 +608,11 @@ static int write_body_mem(struct http_ctx *const h, bool *const close) { const bool close_pending = w->close; - if ((res = write_ctx_free(w))) + if (write_ctx_free(w)) + { fprintf(stderr, "%s: write_ctx_free failed\n", __func__); + return -1; + } else if (close_pending) *close = true; @@ -634,7 +637,7 @@ static int write_body_file(struct http_ctx *const h, bool *const close) return -1; } - int res = h->cfg.write(buf, rem, h->cfg.user); + const int res = h->cfg.write(buf, rem, h->cfg.user); if (res <= 0) return rw_error(res, close); @@ -642,8 +645,11 @@ static int write_body_file(struct http_ctx *const h, bool *const close) { const bool close_pending = w->close; - if ((res = write_ctx_free(w))) + if (write_ctx_free(w)) + { fprintf(stderr, "%s: write_ctx_free failed\n", __func__); + return -1; + } else if (close_pending) *close = true; } -- cgit v1.2.3