Skip to content

Reject NaN in is_bad_src_ratio - #238

Open
Perennos wants to merge 1 commit into
libsndfile:masterfrom
Perennos:fix-nan-src-ratio
Open

Perennos wants to merge 1 commit into
libsndfile:masterfrom
Perennos:fix-nan-src-ratio

Conversation

@Perennos

Copy link
Copy Markdown

Fixes #237

Problem

is_bad_src_ratio() checked ratio < min || ratio > max. Because both comparisons evaluate to false for NaN, a NaN ratio passed validation in src_simple, src_process, and src_set_ratio. In the sinc converters, NaN reaches the input-index arithmetic, lrint(NaN) produces a garbage index, and calc_output_single reads before the filter buffer. The linear and ZOH converters also accepted NaN.

Fix

Rewrite the check in src/common.h as !(ratio >= min && ratio <= max), which evaluates to true for NaN. With this one-line change, every caller of is_bad_src_ratio returns SRC_ERR_BAD_SRC_RATIO on NaN. The extra negative-index guard in calc_output_single proposed in the issue is omitted because NaN can no longer reach that path.

Testing

Added tests/nan_ratio_test.c (registered in tests/CMakeLists.txt) to verify for every converter that src_is_valid_ratio(NAN) is false and that src_simple, src_process, and src_set_ratio reject a NaN ratio.

Built in Release mode with -fsanitize=address,undefined and -DNDEBUG, then ran ctest:

  • Before the fix: 13/14 pass (nan_ratio_test fails). A small program calling src_simple with NAN and SRC_SINC_FASTEST crashes in calc_output_single (src/src_sinc.c:420) under ASan.
  • After the fix: 14/14 pass. The same program returns error 6 (SRC_ERR_BAD_SRC_RATIO) with no ASan report.

AI-assisted: drafted with an AI coding assistant; reproduced and tested locally before submitting.

NaN fails every ordered comparison, so a NaN ratio passed validation and reached the sinc converters, where it caused an out-of-bounds read. Add nan_ratio_test covering src_simple, src_process and src_set_ratio for all converters. Fixes libsndfile#237.

This branch has not been deployed

No deployments
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.

libsamplerate: NaN ratio bypasses validation, heap out-of-bounds read in sinc converter

1 participant