Skip to content

Commit 0b6cdf6

Browse files
committed
mctpd: Avoid realloc(size=0)
This doesn't change program behaviour, but avoids a noisy warning from valgrind. Signed-off-by: Matt Johnston <[email protected]>
1 parent 64c41d7 commit 0b6cdf6

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/mctpd.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,12 +1472,17 @@ static int remove_peer(struct peer *peer)
14721472
memmove(ctx->peers + idx, ctx->peers + idx + 1,
14731473
(ctx->num_peers - idx) * sizeof(struct peer *));
14741474

1475-
tmp = realloc(ctx->peers, ctx->num_peers * sizeof(struct peer *));
1476-
if (!tmp && ctx->num_peers) {
1477-
warn("%s: peer realloc(reduce!) failed", __func__);
1478-
// we'll re-try on next add/remove
1475+
if (ctx->num_peers > 0) {
1476+
tmp = realloc(ctx->peers, ctx->num_peers * sizeof(struct peer *));
1477+
if (!tmp) {
1478+
warn("%s: peer realloc(reduce!) failed", __func__);
1479+
// we'll re-try on next add/remove
1480+
} else {
1481+
ctx->peers = tmp;
1482+
}
14791483
} else {
1480-
ctx->peers = tmp;
1484+
free(ctx->peers);
1485+
ctx->peers = NULL;
14811486
}
14821487

14831488
free(peer);

0 commit comments

Comments
 (0)