diff options
| author | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2023-04-29 00:31:12 +0200 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2023-07-20 23:52:54 +0200 |
| commit | ad2ab22d00cb05f006a59f05cc2ff6879e7e7ba0 (patch) | |
| tree | 75d9d30a25424a933ec64ace75d16c3068079f3d | |
| parent | 0336d249d66b2626dfcca51426bb1a152401f996 (diff) | |
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).
| -rw-r--r-- | http.c | 14 |
1 files changed, 10 insertions, 4 deletions
@@ -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; } |
