Skip to content

Add safety checks for edge cases and memory handling#1004

Open
jeongseok-meta wants to merge 1 commit intomainfrom
export-D91828286
Open

Add safety checks for edge cases and memory handling#1004
jeongseok-meta wants to merge 1 commit intomainfrom
export-D91828286

Conversation

@jeongseok-meta
Copy link
Copy Markdown
Contributor

Summary:
WHY:
The momentum/pymomentum libraries contain edge cases where improper inputs or unusual runtime conditions could cause crashes, undefined behavior, or memory leaks. These include division by zero in SIMD collision functions, out-of-bounds array access in character collision mapping, memory leaks in C API exception paths, and use-after-free in Python bindings.

WHAT:
Adds comprehensive safety checks across 8 files in momentum and pymomentum libraries to handle edge cases gracefully with proper error messages and fallback behaviors.

Issue Fix
Division by zero (collision solver) Safe distance clamping with kMinSafeDistance
Division by zero (rasterizer) Select fallback to infinity for invalid recipW
Out-of-bounds access (character) MT_THROW_IF bounds validation
Memory leak (C API) Pre-allocate buffer + cleanup on exception
Integer overflow (Python arrays) Size validation before pybind11::array_t creation
Use-after-free (PyBytesStreamBuffer) Store bytes_ member to preserve lifetime

HOW:

  • c_api.cpp: Pre-allocate output buffer; add delete[] in catch blocks
  • character.cpp: Add 3 MT_THROW_IF checks for collision parent bounds
  • simd_collision_error_function.cpp: Add kMinSafeDistance/kMaxInverseDistance constants; use drjit::maximum/minimum for safe division
  • legacy_json_io.cpp: Add empty-array checks before size() - 1 operations
  • rasterizer.cpp: Add validRecipW check; use drjit::select for safe fallback
  • momentum_io.cpp: Add MT_THROW_IF for size overflow; use static_cast<pybind11::ssize_t>
  • python_utility.cpp: Add null check for PyList_GetItem; store bytes_ copy
  • python_utility.h: Add bytes_ member to PyBytesStreamBuffer

Differential Revision: D91828286

Summary:
**WHY:**
The momentum/pymomentum libraries contain edge cases where improper inputs or unusual runtime conditions could cause crashes, undefined behavior, or memory leaks. These include division by zero in SIMD collision functions, out-of-bounds array access in character collision mapping, memory leaks in C API exception paths, and use-after-free in Python bindings.

**WHAT:**
Adds comprehensive safety checks across 8 files in momentum and pymomentum libraries to handle edge cases gracefully with proper error messages and fallback behaviors.

| Issue | Fix |
|-------|-----|
| Division by zero (collision solver) | Safe distance clamping with `kMinSafeDistance` |
| Division by zero (rasterizer) | Select fallback to infinity for invalid recipW |
| Out-of-bounds access (character) | `MT_THROW_IF` bounds validation |
| Memory leak (C API) | Pre-allocate buffer + cleanup on exception |
| Integer overflow (Python arrays) | Size validation before `pybind11::array_t` creation |
| Use-after-free (PyBytesStreamBuffer) | Store `bytes_` member to preserve lifetime |

**HOW:**
- `c_api.cpp`: Pre-allocate output buffer; add `delete[]` in catch blocks
- `character.cpp`: Add 3 `MT_THROW_IF` checks for collision parent bounds
- `simd_collision_error_function.cpp`: Add `kMinSafeDistance`/`kMaxInverseDistance` constants; use `drjit::maximum/minimum` for safe division
- `legacy_json_io.cpp`: Add empty-array checks before `size() - 1` operations
- `rasterizer.cpp`: Add `validRecipW` check; use `drjit::select` for safe fallback
- `momentum_io.cpp`: Add `MT_THROW_IF` for size overflow; use `static_cast<pybind11::ssize_t>`
- `python_utility.cpp`: Add null check for `PyList_GetItem`; store `bytes_` copy
- `python_utility.h`: Add `bytes_` member to `PyBytesStreamBuffer`

Differential Revision: D91828286
@meta-codesync
Copy link
Copy Markdown
Contributor

meta-codesync Bot commented Feb 6, 2026

@jeongseok-meta has exported this pull request. If you are a Meta employee, you can view the originating Diff in D91828286.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Feb 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot. fb-exported meta-exported

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant