Skip to content

fix: handle std::bad_alloc and invalid formats on RAW file loading#662

Open
for13to1 wants to merge 2 commits into
IENT:developfrom
for13to1:fix-raw-bad-alloc
Open

fix: handle std::bad_alloc and invalid formats on RAW file loading#662
for13to1 wants to merge 2 commits into
IENT:developfrom
for13to1:fix-raw-bad-alloc

Conversation

@for13to1

Copy link
Copy Markdown
Contributor

Summary of Changes

This PR fixes crashes when opening RAW files with invalid parameters, corrupted headers, or filenames missing resolution indicators. It introduces a multi-layer defense mechanism to prevent integer overflows, out-of-memory crashes, and invalid optional dereferences.

Detailed Changes

  1. Resolution Sanity Boundary (Active Defense)

    • Added static constexpr unsigned MAX_DIMENSION = 16384 to the Size struct in YUViewLib/src/common/Typedef.h.
    • Updated Size::isValid() to ensure width and height do not exceed MAX_DIMENSION. This protects all subsequent memory allocation calculations from integer overflow (as 16384 * 16384 * 4 fits within 32-bit signed integer limits) and filters out invalid huge files early.
  2. Boundary & Safety Checks

    • Added nrBytes <= 0 and fileStartPos < 0 validation in FileSource.cpp and playlistItemRawFile.cpp to prevent negative container resizing.
    • Added isFormatValid() checking before frame processing in videoHandlerRGB and videoHandlerYUV.
    • Fixed a crash in videoHandlerYUV.h where FrameHandler::getFormatAsString() could return std::nullopt and get dereferenced without validation.
  3. Exception Safety & Logging (Passive Defense)

    • Wrapped critical memory allocation functions (readBytes, convertYUVToImage, convertRGBToImage) in try-catch blocks targeting std::bad_alloc.
    • Instead of silently swallowing allocation failures, warnings are logged via qWarning and the frame loader exits gracefully without updating cache indices.

Verification

  • Compiled successfully on macOS and Windows.
  • Verified that opening invalid RAW files no longer crashes the application.
  • All 1860 unit tests in the test suite passed successfully.

@for13to1

Copy link
Copy Markdown
Contributor Author

#661

@for13to1 for13to1 force-pushed the fix-raw-bad-alloc branch from f400261 to 6eee041 Compare June 27, 2026 18:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant