Skip to content

Add check for malformed audio files - #1079

Merged
Akshay Sonawane (apsonawane) merged 2 commits into
mainfrom
asonawane/speech
Jun 12, 2026
Merged

Add check for malformed audio files#1079
Akshay Sonawane (apsonawane) merged 2 commits into
mainfrom
asonawane/speech

Conversation

@apsonawane

@apsonawane Akshay Sonawane (apsonawane) commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

This pull request improves the robustness of the LogMel feature extraction by adding input validation and new regression tests to handle malformed audio data that previously could cause crashes or out-of-bounds reads. The main focus is on ensuring that degenerate or malformed input data is detected early and handled gracefully, preventing potential heap-buffer-overflow errors.

Input validation and error handling:

  • Enhanced the LogMel::Compute method to check the input tensor shape and reject degenerate spectrograms (e.g., too few time steps or frequency bins), returning a clear error instead of asserting or crashing.

Testing and regression coverage:

  • Added a regression test (MalformedAudioDoesNotCrash) to verify that decoding malformed audio data fails gracefully without crashing, addressing a previously reported heap-buffer-overflow issue.
  • Added a regression test (MalformedAudioLogMelDoesNotCrash) to ensure the LogMel feature extractor rejects malformed audio that produces a degenerate spectrogram, rather than crashing.
  • Included the shared/api/speech_extractor.h header in the test file to support the new tests.

Onnxruntime-genai validation check: microsoft/onnxruntime-genai#2225

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens the speech feature-extraction pipeline against malformed/degenerate audio inputs by adding runtime validation in LogMel::Compute and introducing regression tests intended to prevent prior crash/OOB-read scenarios.

Changes:

  • Replaced a debug-only assert in LogMel::Compute with runtime shape validation and added a guard against degenerate STFT outputs.
  • Added new regression tests around malformed audio inputs to ensure failures are handled gracefully.
  • Updated test includes to support feature-extractor-based testing.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
test/pp_api_test/test_decode_audio.cc Adds regression tests intended to ensure malformed audio does not crash decode/extraction paths.
shared/api/speech_features.hpp Adds input validation to LogMel::Compute to reject invalid shapes and degenerate STFT outputs before unsafe operations.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread test/pp_api_test/test_decode_audio.cc Outdated
Comment thread test/pp_api_test/test_decode_audio.cc Outdated
Comment thread test/pp_api_test/test_decode_audio.cc
Comment thread test/pp_api_test/test_decode_audio.cc Outdated
Comment thread test/pp_api_test/test_decode_audio.cc Outdated
@apsonawane
Akshay Sonawane (apsonawane) merged commit de4c4dd into main Jun 12, 2026
38 checks passed
@apsonawane
Akshay Sonawane (apsonawane) deleted the asonawane/speech branch June 12, 2026 19:46
Akshay Sonawane (apsonawane) added a commit to microsoft/onnxruntime-genai that referenced this pull request Jun 30, 2026
This pull request improves the robustness of audio buffer handling by
adding a minimum size check for audio data and introduces a regression
test to ensure malformed buffers are properly rejected. These changes
prevent crashes due to undersized or malformed audio buffers and improve
error reporting.

**Audio buffer validation:**
* Added a minimum buffer size check (`kMinAudioBufferSize = 44`) in
`LoadAudiosFromBuffers` to reject audio buffers that are too small to
contain a valid audio header, throwing a clear runtime error if
violated.

**Testing improvements:**
* Added a regression test (`LoadAudiosFromBuffersRejectsTooSmallBuffer`)
to ensure that audio buffers smaller than the minimum required size are
rejected with a descriptive error, preventing heap-buffer-overflow
crashes.

Onnxruntime-extensions validation check:
microsoft/onnxruntime-extensions#1079
Tianlei Wu (tianleiwu) pushed a commit to microsoft/onnxruntime-genai that referenced this pull request Jul 11, 2026
This pull request improves the robustness of audio buffer handling by
adding a minimum size check for audio data and introduces a regression
test to ensure malformed buffers are properly rejected. These changes
prevent crashes due to undersized or malformed audio buffers and improve
error reporting.

**Audio buffer validation:**
* Added a minimum buffer size check (`kMinAudioBufferSize = 44`) in
`LoadAudiosFromBuffers` to reject audio buffers that are too small to
contain a valid audio header, throwing a clear runtime error if
violated.

**Testing improvements:**
* Added a regression test (`LoadAudiosFromBuffersRejectsTooSmallBuffer`)
to ensure that audio buffers smaller than the minimum required size are
rejected with a descriptive error, preventing heap-buffer-overflow
crashes.

Onnxruntime-extensions validation check:
microsoft/onnxruntime-extensions#1079
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.

3 participants