🌐 US-Proxy
class="logged-out env-production page-responsive" style="word-wrap: break-word;" >
Skip to content

Validate NPY shape sizes before allocation - #6443

Merged
JanuszL merged 5 commits into
NVIDIA:mainfrom
JanuszL:fix-numpy-shape-overflow
Aug 11, 2026
Merged

JanuszL merged 5 commits into
NVIDIA:mainfrom
JanuszL:fix-numpy-shape-overflow

Conversation

@JanuszL

@JanuszL JanuszL commented Aug 7, 2026 •

Copy link
Copy Markdown
Contributor

Category:

Bug fix

Description:

Reject invalid and overflowing NumPy array shapes before an allocation or file read can use a wrapped element or byte count.

Addresses:
When parsing numpy (.npy) files, the ParseHeaderContents function reads arbitrary int64_t shape values from the file header. The volume() function multiplies all shape dimensions together without overflow checking. The result is used in HeaderData::nbytes() which computes type_info->size() * size() (both size_t). A crafted .npy file with shape dimensions whose product overflows int64_t (e.g., shape=(2^32, 2^32)) would cause volume() to return a small or negative value, leading to an undersized memory allocation in Tensor::Resize(), followed by a buffer overflow when reading file data into the undersized buffer.

Additional information:

Affected modules and functionalities:

  • NumPy .npy header parsing and HeaderData size calculation

Key points relevant for the review:

  • Reject negative and out-of-range dimensions.
  • Check both element-count and byte-count multiplication.
  • Errors report the requested multiplication terms and the maximum size_t limit.

Tests:

  • Existing tests apply
  • New tests added
    • Python tests
    • GTests
      • NumpyLoaderTest.RejectsInvalidOrOverflowingShape
    • Benchmark
    • Other
  • N/A

Checklist

Documentation

  • Existing documentation applies
  • Documentation updated
    • Docstring
    • Doxygen
    • RST
    • Jupyter
    • Other
  • N/A

DALI team only

Requirements

  • Implements new requirements
  • Affects existing requirements
  • N/A

REQ IDs: N/A

JIRA TASK: N/A

Signed-off-by: Janusz Lisiecki <jlisiecki@nvidia.com>
@greptile-apps

greptile-apps Bot commented Aug 7, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR validates NumPy shape dimensions and checks element-count and byte-count multiplication before allocation.

  • Rejects negative and out-of-range dimensions.
  • Detects overflow while calculating element and byte counts.
  • Adds targeted tests that independently exercise both overflow guards and verify errno preservation.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
dali/util/numpy.cc Adds range, dimension, element-count, and byte-count validation to NumPy header processing.
dali/util/numpy_test.cc Adds invalid-shape tests and now directly reaches the byte-count overflow guard while element-count calculation succeeds.

Reviews (5): Last reviewed commit: "Extract NumPy overflow checks from enfor..." | Re-trigger Greptile

Comment thread dali/util/numpy_test.cc
Signed-off-by: Janusz Lisiecki <jlisiecki@nvidia.com>
@JanuszL

JanuszL commented Aug 7, 2026

Copy link
Copy Markdown
Contributor Author

!build

@dali-automaton

Copy link
Copy Markdown
Collaborator

CI MESSAGE: [61556858]: BUILD STARTED

@dali-automaton

Copy link
Copy Markdown
Collaborator

CI MESSAGE: [61556858]: BUILD PASSED

Comment thread dali/util/numpy.cc
Comment thread dali/util/numpy.cc Outdated
Comment thread dali/util/numpy.cc Outdated
Signed-off-by: Janusz Lisiecki <jlisiecki@nvidia.com>
Signed-off-by: Janusz Lisiecki <jlisiecki@nvidia.com>
Signed-off-by: Janusz Lisiecki <jlisiecki@nvidia.com>
@JanuszL

JanuszL commented Aug 11, 2026

Copy link
Copy Markdown
Contributor Author

!build

@dali-automaton

Copy link
Copy Markdown
Collaborator

CI MESSAGE: [62108786]: BUILD STARTED

@dali-automaton

Copy link
Copy Markdown
Collaborator

CI MESSAGE: [62108786]: BUILD PASSED

@JanuszL
JanuszL merged commit bcbf303 into NVIDIA:main Aug 11, 2026
8 checks passed
@JanuszL
JanuszL deleted the fix-numpy-shape-overflow branch August 11, 2026 15:59
@JanuszL JanuszL added the important-fix Fixes an important issue in the software or development environment. label Aug 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

important-fix Fixes an important issue in the software or development environment.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants