BUG: DataStore constructors accept a tuple shape whose product overflows

未关闭
#1,883 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 6 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
54/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
cpp
领域
backend

调研方向

Read src/simplnx/DataStructure/DataStore.hpp, focusing on both constructors and resizeTuples(), and review EmptyDataStore::CheckedPlaceholderProduct in src/simplnx/DataStructure/EmptyDataStore.hpp for the zero-dimension rule. Start by checking the existing DataStore tests in test/DataArrayTest.cpp; add coverage for overflowing tuple, component, element, and byte counts in both constructors. Done when those cases reject overflow while shapes containing a zero dimension remain valid.

由索引模型根据 Issue 内容生成。

描述

bug

Problem

The DataStore constructors do not check the tuple-shape product or the component-shape product for overflow. When a product does not fit in usize, the count wraps around. The store then allocates a buffer for the wrapped count, but it keeps the original shape.

For example, DataStore<int32>({2^63, 2}, {1}, 7) succeeds on a 64-bit platform with 0 tuples and 0 values, and getTupleShape() returns {9223372036854775808, 2}. The expected behavior is that the constructor rejects the shape. Every later operation that uses the shape and the count together gets inconsistent values.

Occurs at develop commit 547f28a75e6325358fe7823e6dcb00defda81cd1.

Steps to Reproduce

  1. Build simplnx_test from develop (Release, 64-bit).
  2. Add the test in Verification Unit Test to test/DataArrayTest.cpp.
  3. Construct DataStore<int32> with the tuple shape {usize{1} << 63, 2}, the component shape {1} and the initial value 7.
  4. The constructor returns normally. No exception is thrown.

Cause

The (tupleShape, componentShape, initValue) constructor computes both counts with std::accumulate and no overflow check, then allocates getSize() values:

https://github.com/BlueQuartzSoftware/simplnx/blob/547f28a75e6325358fe7823e6dcb00defda81cd1/src/simplnx/DataStructure/DataStore.hpp#L89-L106

, m_NumComponents(std::accumulate(m_ComponentShape.cbegin(), m_ComponentShape.cend(), static_cast<usize>(1), std::multiplies<>()))
, m_NumTuples(std::accumulate(m_TupleShape.cbegin(), m_TupleShape.cend(), static_cast<usize>(1), std::multiplies<>()))
...
m_Data = std::unique_ptr<value_type[]>(new value_type[this->getSize()]);

2^63 * 2 is 2^64, which wraps to 0. getSize() is then 0, so new value_type[0] succeeds.

The constructor that takes ownership of a buffer computes the counts in the same way:

https://github.com/BlueQuartzSoftware/simplnx/blob/547f28a75e6325358fe7823e6dcb00defda81cd1/src/simplnx/DataStructure/DataStore.hpp#L117-L127

getNumberOfComponents() * getNumberOfTuples() (the element count) and the byte count of the allocation are not checked either.

Verification Unit Test

File: test/DataArrayTest.cpp

TEST_CASE("DataStore constructor rejects an overflowing tuple shape", "[simplnx][DataStore]")
{
  // The tuple-shape product 2^63 * 2 = 2^64 does not fit a 64-bit usize.
  const ShapeType overflowingTupleShape = {usize{1} << 63, 2};

  REQUIRE_THROWS(DataStore<int32>(overflowingTupleShape, ShapeType{1}, 7));
}

Command (from the build directory):

ctest -R "^DataStore constructor rejects an overflowing tuple shape$" --output-on-failure

Failure on develop (macOS arm64, AppleClang, Release):

DataArrayTest.cpp:860: FAILED:
  REQUIRE_THROWS( DataStore<int32>(overflowingTupleShape, ShapeType{1}, 7) )
because no exception was thrown where one was expected:
test cases: 1 | 1 failed
assertions: 1 | 1 failed

Proposed Fix

  • In both constructors, compute the tuple count, the component count, the element count and the byte count with overflow checks before the allocation, and throw (for example std::overflow_error) when a count does not fit in usize. A constructor cannot return a Result<>, so an exception is the only way to reject the shape.
  • A shape that contains a zero dimension has a count of zero. Check for a zero dimension before the multiplication, so that a large partial product of the other dimensions is not reported as an overflow. EmptyDataStore::CheckedPlaceholderProduct (src/simplnx/DataStructure/EmptyDataStore.hpp) already uses this approach.
  • Put the checked product in one helper that the constructors and resizeTuples() share.

Notes

  • PR #1834 adds the same tuple, element and byte overflow checks to DataStore::resizeTuples(), with the zero-dimension rule above. The constructors are not part of that PR.
  • StringStore and ListStore have the same unchecked product in resizeTuples() (#1884). An allocation above the AddressSanitizer limit is reported in #1885.
主要语言
C++
星标
18
派生
15
平均合并
4 天 23 小时
30 天内合并 PR
42

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

BlueQuartzSoftware/simplnx 的其他 Issue

查看 BlueQuartzSoftware/simplnx 的全部 Issue

相似的 Issue

更多 C++ Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。