From: Victor Julien Date: Tue, 25 Apr 2023 11:28:05 +0000 (+0200) Subject: streaming: improve error handling X-Git-Tag: suricata-6.0.12~34 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5f1651282dbd5dc753dd7d0e9d117020662fc529;p=thirdparty%2Fsuricata.git streaming: improve error handling util-streaming-buffer.c:205:5: warning: Potential leak of memory pointed to by 'sbb2' [unix.Malloc] BUG_ON(sbb2->offset < sbb->len); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ./suricata-common.h:290:27: note: expanded from macro 'BUG_ON' #define BUG_ON(x) assert(!(x)) ^~~~~~~~~~~~ /usr/include/assert.h:99:28: note: expanded from macro 'assert' ? __ASSERT_VOID_CAST (0) \ ^ 1 warning generated. --- diff --git a/src/util-streaming-buffer.c b/src/util-streaming-buffer.c index bd5b5dd73f..4c31f75f09 100644 --- a/src/util-streaming-buffer.c +++ b/src/util-streaming-buffer.c @@ -183,26 +183,28 @@ static int WARN_UNUSED SBBInit(StreamingBuffer *sb, uint32_t rel_offset, uint32_ } sbb->offset = sb->stream_offset; sbb->len = sb->buf_offset; + (void)SBB_RB_INSERT(&sb->sbb_tree, sbb); + sb->sbb_size = sbb->len; + sb->head = sbb; StreamingBufferBlock *sbb2 = CALLOC(sb->cfg, 1, sizeof(*sbb2)); if (sbb2 == NULL) { - FREE(sb->cfg, sbb, sizeof(*sbb)); return -1; } sbb2->offset = sb->stream_offset + rel_offset; sbb2->len = data_len; - - sb->head = sbb; - sb->sbb_size = sbb->len + sbb2->len; - SBB_RB_INSERT(&sb->sbb_tree, sbb); - SBB_RB_INSERT(&sb->sbb_tree, sbb2); - SCLogDebug("sbb1 %"PRIu64", len %u, sbb2 %"PRIu64", len %u", sbb->offset, sbb->len, sbb2->offset, sbb2->len); + + sb->sbb_size += sbb2->len; + if (SBB_RB_INSERT(&sb->sbb_tree, sbb2) != NULL) { + FREE(sb->cfg, sbb2, sizeof(*sbb2)); + return -1; + } + #ifdef DEBUG SBBPrintList(sb); #endif - BUG_ON(sbb2->offset < sbb->len); return 0; } @@ -222,7 +224,7 @@ static int WARN_UNUSED SBBInitLeadingGap(StreamingBuffer *sb, uint64_t offset, u sb->head = sbb; sb->sbb_size = sbb->len; - SBB_RB_INSERT(&sb->sbb_tree, sbb); + (void)SBB_RB_INSERT(&sb->sbb_tree, sbb); SCLogDebug("sbb %"PRIu64", len %u", sbb->offset, sbb->len);