Skip to content

Commit

Permalink
Fix coverity issue in the alignToBin (#1492)
Browse files Browse the repository at this point in the history
Those values should be always >=0 but coverity do not know it.
Assert should fix it.
  • Loading branch information
lplewa authored Sep 3, 2024
1 parent 2d5b10f commit 8152ba4
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/tbbmalloc/large_objects.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2005-2023 Intel Corporation
Copyright (c) 2005-2024 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -81,18 +81,25 @@ struct HugeBinStructureProps {

static size_t alignToBin(size_t size) {
MALLOC_ASSERT(size >= StepFactor, "Size must not be less than the StepFactor");
size_t minorStepExp = BitScanRev(size) - StepFactorExp;

int sizeExp = (int)BitScanRev(size);
MALLOC_ASSERT(sizeExp >= 0, "BitScanRev() cannot return -1, as size >= stepfactor > 0");
MALLOC_ASSERT(sizeExp >= StepFactorExp, "sizeExp >= StepFactorExp, because size >= stepFactor");
int minorStepExp = sizeExp - StepFactorExp;

return alignUp(size, 1ULL << minorStepExp);
}

// Sizes between the power of 2 values are approximated to StepFactor.
static int sizeToIdx(size_t size) {
MALLOC_ASSERT(MinSize <= size && size <= MaxSize, ASSERT_TEXT);

int sizeExp = (int)BitScanRev(size); // same as __TBB_Log2
MALLOC_ASSERT(sizeExp >= 0, "A shift amount (sizeExp) must not be negative");
size_t majorStepSize = 1ULL << sizeExp;
MALLOC_ASSERT(sizeExp >= 0, "BitScanRev() cannot return -1, as size >= stepfactor > 0");
MALLOC_ASSERT(sizeExp >= StepFactorExp, "sizeExp >= StepFactorExp, because size >= stepFactor");
int minorStepExp = sizeExp - StepFactorExp;
MALLOC_ASSERT(minorStepExp >= 0, "A shift amount (minorStepExp) must not be negative");

size_t majorStepSize = 1ULL << sizeExp;
int minorIdx = (size - majorStepSize) >> minorStepExp;
MALLOC_ASSERT(size == majorStepSize + ((size_t)minorIdx << minorStepExp),
"Size is not aligned on the bin");
Expand Down

0 comments on commit 8152ba4

Please sign in to comment.