Skip to content

Commit

Permalink
fix undefined behavior found by clang static analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbkoch committed Apr 12, 2024
1 parent fe50e13 commit 43319de
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 8 deletions.
4 changes: 3 additions & 1 deletion python/interpret-core/interpret/visual/_inline.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ def _build_javascript(viz_obj, id_str=None, default_key=-1, js_url=None):
else:
# if no root directory then fallback to the git source location
root_path = os.path.join(interpret_path, "..", "..", "..")
js_path = os.path.join(root_path, "shared", "vis", "dist", "interpret-inline.js")
js_path = os.path.join(
root_path, "shared", "vis", "dist", "interpret-inline.js"
)
js_last_modified = time.ctime(os.path.getmtime(js_path))

with open(js_path, "r", encoding="utf-8") as f:
Expand Down
2 changes: 1 addition & 1 deletion shared/libebm/GenerateTermUpdate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ EBM_API_BODY ErrorEbm EBM_CALLING_CONVENTION GenerateTermUpdate(void* rng,
// are going to remain having 0 splits.
pBoosterShell->GetInnerTermUpdate()->Reset();

if(IntEbm{0} == lastDimensionLeavesMax || 1 != cRealDimensions && MONOTONE_NONE != significantDirection) {
if(IntEbm{0} == lastDimensionLeavesMax || (1 != cRealDimensions && MONOTONE_NONE != significantDirection)) {
// this is kind of hacky where if any one of a number of things occurs (like we have only 1 leaf)
// we sum everything into a single bin. The alternative would be to always sum into the tensor bins
// but then collapse them afterwards into a single bin, but that's more work.
Expand Down
1 change: 0 additions & 1 deletion shared/libebm/PartitionOneDimensionalBoosting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,6 @@ static int FindBestSplitGain(RandomDeterministic* const pRng,

EBM_ASSERT(FloatCalc{0} <= k_gainMin);
FloatCalc bestGain = k_gainMin; // it must at least be this, and maybe it needs to be more
EBM_ASSERT(0 <= cSamplesLeafMin);
EBM_ASSERT(0.0 < hessianMin);
EBM_ASSERT(pBinLast != pBinCur); // then we would be non-splitable and would have exited above
do {
Expand Down
2 changes: 0 additions & 2 deletions shared/libebm/PartitionTwoDimensionalBoosting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ static FloatCalc SweepMultiDimensional(const size_t cRuntimeScores,
auto* const aGradientPairsHigh =
bUseStackMemory ? binHigh.GetGradientPairs() : p_DO_NOT_USE_DIRECTLY_High->GetGradientPairs();

EBM_ASSERT(0 <= cSamplesLeafMin);
EBM_ASSERT(0.0 < hessianMin);

const bool bUseLogitBoost = bHessian && !(TermBoostFlags_DisableNewtonGain & flags);
Expand Down Expand Up @@ -301,7 +300,6 @@ template<bool bHessian, size_t cCompilerScores> class PartitionTwoDimensionalBoo
auto* pTotals1HighLowBest = IndexBin(aAuxiliaryBins, cBytesPerBin * 2);
auto* pTotals1HighHighBest = IndexBin(aAuxiliaryBins, cBytesPerBin * 3);

EBM_ASSERT(0 <= cSamplesLeafMin);
EBM_ASSERT(0.0 < hessianMin);

LOG_0(Trace_Verbose, "PartitionTwoDimensionalBoostingInternal Starting FIRST bin sweep loop");
Expand Down
1 change: 0 additions & 1 deletion shared/libebm/PartitionTwoDimensionalInteraction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ template<bool bHessian, size_t cCompilerScores> class PartitionTwoDimensionalInt
auto* const aGradientPairs11 =
bUseStackMemory ? bin11.GetGradientPairs() : p_DO_NOT_USE_DIRECTLY_11->GetGradientPairs();

EBM_ASSERT(0 <= cSamplesLeafMin);
EBM_ASSERT(0.0 < hessianMin);

#ifndef NDEBUG
Expand Down
10 changes: 8 additions & 2 deletions shared/libebm/compute/BinSumsBoosting.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,7 @@ struct BitPack final {
INLINE_ALWAYS static void Func(BinSumsBoostingBridge* const pParams) {

static_assert(!bCollapsed, "Cannot be bCollapsed since there would be no bitpacking");
static_assert(cCompilerPack <= COUNT_BITS(typename TFloat::TInt::T), "cCompilerPack must fit into the bitpack");

if(cCompilerPack == pParams->m_cPack) {
size_t cSamples = pParams->m_cSamples;
Expand All @@ -967,11 +968,15 @@ struct BitPack final {
}
pParams->m_cSamples = cSamples;
if(bWeight) {
EBM_ASSERT(nullptr != pParams->m_aWeights);
pParams->m_aWeights = IndexByte(pParams->m_aWeights, sizeof(typename TFloat::T) * cRemnants);
} else {
EBM_ASSERT(nullptr == pParams->m_aWeights);
}

const size_t cScores = GET_COUNT_SCORES(cCompilerScores, pParams->m_cScores);

EBM_ASSERT(nullptr != pParams->m_aGradientsAndHessians);
pParams->m_aGradientsAndHessians = IndexByte(pParams->m_aGradientsAndHessians,
sizeof(typename TFloat::T) * (bHessian ? size_t{2} : size_t{1}) * cScores * cRemnants);
}
Expand All @@ -984,7 +989,7 @@ struct BitPack final {
bHessian,
bWeight,
cCompilerScores,
GetNextBitPack<TFloat>(cCompilerPack, k_cItemsPerBitPackBoostingMin)>::Func(pParams);
GetNextBitPack<typename TFloat::TInt::T>(cCompilerPack, k_cItemsPerBitPackBoostingMin)>::Func(pParams);
}
}
};
Expand Down Expand Up @@ -1020,7 +1025,8 @@ INLINE_RELEASE_TEMPLATED static void BitPackBoosting(BinSumsBoostingBridge* cons
bHessian,
bWeight,
cCompilerScores,
GetFirstBitPack<TFloat>(k_cItemsPerBitPackBoostingMax, k_cItemsPerBitPackBoostingMin)>::Func(pParams);
GetFirstBitPack<typename TFloat::TInt::T>(
k_cItemsPerBitPackBoostingMax, k_cItemsPerBitPackBoostingMin)>::Func(pParams);
}
template<typename TFloat,
bool bParallel,
Expand Down
9 changes: 9 additions & 0 deletions shared/libebm/compute/Objective.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,11 @@ struct BitPackObjective final {
pData->m_cSamples = cSamples;

if(bWeight) {
EBM_ASSERT(nullptr != pData->m_aWeights);
pData->m_aWeights =
IndexByte(pData->m_aWeights, sizeof(typename TObjective::TFloatInternal::T) * cRemnants);
} else {
EBM_ASSERT(nullptr == pData->m_aWeights);
}

const size_t cScores = GET_COUNT_SCORES(cCompilerScores, pData->m_cScores);
Expand All @@ -149,6 +152,9 @@ struct BitPackObjective final {
}

if(!TObjective::k_bRmse) {
EBM_ASSERT(nullptr != pData->m_aTargets);
EBM_ASSERT(nullptr != pData->m_aSampleScores);

constexpr bool bClassification = Task_GeneralClassification == TObjective::k_task;
if(bClassification) {
// targets are integers
Expand All @@ -161,6 +167,9 @@ struct BitPackObjective final {
}
pData->m_aSampleScores = IndexByte(
pData->m_aSampleScores, sizeof(typename TObjective::TFloatInternal::T) * cScores * cRemnants);
} else {
EBM_ASSERT(nullptr == pData->m_aTargets);
EBM_ASSERT(nullptr == pData->m_aSampleScores);
}
}
DoneBitpacking<TObjective,
Expand Down

0 comments on commit 43319de

Please sign in to comment.