Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

general/cppcheck: Address cppcheck memory related errors #9892

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions src/detect-engine-address.c
Original file line number Diff line number Diff line change
Expand Up @@ -1362,23 +1362,28 @@ void DetectAddressMapFree(DetectEngineCtx *de_ctx)
return;
}

static int DetectAddressMapAdd(DetectEngineCtx *de_ctx, const char *string,
DetectAddressHead *address, bool contains_negation)
static bool DetectAddressMapAdd(DetectEngineCtx *de_ctx, const char *string,
DetectAddressHead *address, bool contains_negation)
{
DetectAddressMap *map = SCCalloc(1, sizeof(*map));
if (map == NULL)
return -1;
return false;

map->string = SCStrdup(string);
if (map->string == NULL) {
SCFree(map);
return -1;
return false;
}
map->address = address;
map->contains_negation = contains_negation;

BUG_ON(HashListTableAdd(de_ctx->address_table, (void *)map, 0) != 0);
return 0;
if (HashListTableAdd(de_ctx->address_table, (void *)map, 0) != 0) {
SCFree(map->string);
SCFree(map);
return false;
}

return true;
}

static const DetectAddressMap *DetectAddressMapLookup(DetectEngineCtx *de_ctx,
Expand Down Expand Up @@ -1471,8 +1476,11 @@ const DetectAddressHead *DetectParseAddress(DetectEngineCtx *de_ctx,
*contains_negation = false;
}

DetectAddressMapAdd((DetectEngineCtx *)de_ctx, string, head,
*contains_negation);
if (!DetectAddressMapAdd((DetectEngineCtx *)de_ctx, string, head, *contains_negation)) {
DetectAddressHeadFree(head);
return NULL;
}

return head;
}

Expand Down
21 changes: 12 additions & 9 deletions src/util-streaming-buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -842,16 +842,11 @@ static inline void StreamingBufferSlideToOffsetWithRegions(
r = next;
}
SCLogDebug("to_shift %p", to_shift);
} else {
to_shift = &sb->region;
SCLogDebug("shift start region %p", to_shift);
}

// this region is main, or will xfer its buffer to main
if (to_shift) {
SCLogDebug("main: offset %" PRIu64 " buf %p size %u offset %u", to_shift->stream_offset,
to_shift->buf, to_shift->buf_size, to_shift->buf_offset);
if (to_shift != &sb->region) {
// this region is main, or will xfer its buffer to main
if (to_shift && to_shift != &sb->region) {
SCLogDebug("main: offset %" PRIu64 " buf %p size %u offset %u", to_shift->stream_offset,
to_shift->buf, to_shift->buf_size, to_shift->buf_offset);
DEBUG_VALIDATE_BUG_ON(sb->region.buf != NULL);

sb->region.buf = to_shift->buf;
Expand All @@ -860,12 +855,20 @@ static inline void StreamingBufferSlideToOffsetWithRegions(
sb->region.buf_size = to_shift->buf_size;
sb->region.next = to_shift->next;

BUG_ON(to_shift == &sb->region);
FREE(cfg, to_shift, sizeof(*to_shift));
to_shift = &sb->region;
sb->regions--;
DEBUG_VALIDATE_BUG_ON(sb->regions == 0);
}

} else {
to_shift = &sb->region;
SCLogDebug("shift start region %p", to_shift);
}

// this region is main, or will xfer its buffer to main
if (to_shift) {
// Do the shift. If new region is exactly at the slide offset we can skip this.
DEBUG_VALIDATE_BUG_ON(to_shift->stream_offset > slide_offset);
const uint32_t s = slide_offset - to_shift->stream_offset;
Expand Down
Loading