Skip to content

Commit faf7d11

Browse files
imikejacksonclaude
andcommitted
BUG: Add error on write/fill to borrowed CalcBuffer, fix unreachable fallback
Replace silent no-ops with std::runtime_error throws when write() or fill() is called on a read-only Borrowed CalcBuffer. Replace null pointer dereference fallback in array() with a throw. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent d17bb99 commit faf7d11

1 file changed

Lines changed: 11 additions & 13 deletions

File tree

src/Plugins/SimplnxCore/src/SimplnxCore/Filters/Algorithms/ArrayCalculator.cpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <cmath>
1212
#include <numbers>
1313
#include <stack>
14+
#include <stdexcept>
1415

1516
using namespace nx::core;
1617

@@ -403,7 +404,7 @@ void CalcBuffer::write(usize index, float64 value)
403404
(*m_OutputArray)[index] = value;
404405
return;
405406
case Storage::Borrowed:
406-
return; // read-only — should not be called
407+
throw std::runtime_error("CalcBuffer::write() called on a read-only Borrowed buffer");
407408
}
408409
}
409410

@@ -418,7 +419,7 @@ void CalcBuffer::fill(float64 value)
418419
m_OutputArray->fill(value);
419420
return;
420421
case Storage::Borrowed:
421-
return; // read-only
422+
throw std::runtime_error("CalcBuffer::fill() called on a read-only Borrowed buffer");
422423
}
423424
}
424425

@@ -523,8 +524,7 @@ const Float64Array& CalcBuffer::array() const
523524
case Storage::OutputDirect:
524525
return *m_OutputArray;
525526
}
526-
// Should never reach here; return owned as fallback
527-
return *m_OwnedArray;
527+
throw std::runtime_error("CalcBuffer::array() called on buffer with unknown storage mode");
528528
}
529529

530530
// ---------------------------------------------------------------------------
@@ -1818,9 +1818,7 @@ Result<> ArrayCalculatorParser::evaluateInto(DataStructure& dataStructure, const
18181818
// 2. Create local temp DataStructure for intermediate arrays
18191819
DataStructure tempDS;
18201820
usize scratchCounter = 0;
1821-
auto nextScratchName = [&scratchCounter]() -> std::string {
1822-
return "_calc_" + std::to_string(scratchCounter++);
1823-
};
1821+
auto nextScratchName = [&scratchCounter]() -> std::string { return "_calc_" + std::to_string(scratchCounter++); };
18241822

18251823
// 3. Pre-scan RPN to find the index of the last operator/extract item
18261824
// for the OutputDirect optimization
@@ -1891,8 +1889,8 @@ Result<> ArrayCalculatorParser::evaluateInto(DataStructure& dataStructure, const
18911889
std::vector<usize> resultCompShape = operand.compShape();
18921890
usize totalSize = operand.size();
18931891

1894-
CalcBuffer result = (isLastOp && outputIsFloat64) ? CalcBuffer::wrapOutput(dataStructure.getDataRefAs<DataArray<float64>>(outputPath))
1895-
: CalcBuffer::allocate(tempDS, nextScratchName(), resultTupleShape, resultCompShape);
1892+
CalcBuffer result = (isLastOp && outputIsFloat64) ? CalcBuffer::wrapOutput(dataStructure.getDataRefAs<DataArray<float64>>(outputPath)) :
1893+
CalcBuffer::allocate(tempDS, nextScratchName(), resultTupleShape, resultCompShape);
18961894

18971895
for(usize i = 0; i < totalSize; i++)
18981896
{
@@ -1956,8 +1954,8 @@ Result<> ArrayCalculatorParser::evaluateInto(DataStructure& dataStructure, const
19561954
totalSize *= d;
19571955
}
19581956

1959-
CalcBuffer result = (isLastOp && outputIsFloat64) ? CalcBuffer::wrapOutput(dataStructure.getDataRefAs<DataArray<float64>>(outputPath))
1960-
: CalcBuffer::allocate(tempDS, nextScratchName(), outTupleShape, outCompShape);
1957+
CalcBuffer result = (isLastOp && outputIsFloat64) ? CalcBuffer::wrapOutput(dataStructure.getDataRefAs<DataArray<float64>>(outputPath)) :
1958+
CalcBuffer::allocate(tempDS, nextScratchName(), outTupleShape, outCompShape);
19611959

19621960
bool leftIsScalar = left.isScalar();
19631961
bool rightIsScalar = right.isScalar();
@@ -2000,8 +1998,8 @@ Result<> ArrayCalculatorParser::evaluateInto(DataStructure& dataStructure, const
20001998
return MakeErrorResult(static_cast<int>(CalculatorErrorCode::ComponentOutOfRange), fmt::format("Component index {} is out of range for array with {} components.", compIdx, numComps));
20011999
}
20022000

2003-
CalcBuffer result = (isLastOp && outputIsFloat64) ? CalcBuffer::wrapOutput(dataStructure.getDataRefAs<DataArray<float64>>(outputPath))
2004-
: CalcBuffer::allocate(tempDS, nextScratchName(), operand.tupleShape(), std::vector<usize>{1});
2001+
CalcBuffer result = (isLastOp && outputIsFloat64) ? CalcBuffer::wrapOutput(dataStructure.getDataRefAs<DataArray<float64>>(outputPath)) :
2002+
CalcBuffer::allocate(tempDS, nextScratchName(), operand.tupleShape(), std::vector<usize>{1});
20052003

20062004
for(usize t = 0; t < numTuples; ++t)
20072005
{

0 commit comments

Comments
 (0)