From 83aabf01858e733338fb72f96b28d955b459bbb3 Mon Sep 17 00:00:00 2001 From: Xavier Del Campo Romero Date: Sun, 30 Apr 2023 17:07:00 +0200 Subject: Avoid memory leak on failed realloc(3) --- src/net/win9x/src/serial.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'src/net') diff --git a/src/net/win9x/src/serial.c b/src/net/win9x/src/serial.c index 57d7261..cf77b9b 100644 --- a/src/net/win9x/src/serial.c +++ b/src/net/win9x/src/serial.c @@ -104,12 +104,16 @@ static int free_req(struct net_host_domain *const h, struct req *const r) if (n) { - if (!(h->reqs = realloc(h->reqs, n * sizeof *h->reqs))) + struct req *const r = realloc(h->reqs, n * sizeof *h->reqs); + + if (!r) { fprintf(stderr, "%s: realloc(3) failed: %s\n", __func__, strerror(errno)); return -1; } + + h->reqs = r; } else { @@ -127,13 +131,18 @@ static int free_req(struct net_host_domain *const h, struct req *const r) static struct req *alloc_req(struct net_host_domain *const h, const void *const buf, const size_t n, const enum req_type t) { - if (!(h->reqs = realloc(h->reqs, (h->n_reqs + 1) * sizeof *h->reqs))) + struct req *const reqs = realloc(h->reqs, + (h->n_reqs + 1) * sizeof *h->reqs); + + if (!reqs) { fprintf(stderr, "%s: realloc(3) failed: %s\n", __func__, strerror(errno)); return NULL; } + h->reqs = reqs; + struct req *const r = &h->reqs[h->n_reqs++]; *r = (const struct req) -- cgit v1.2.3