Skip to content

fix: return 400 for malformed option values - #1123

Merged
0xShug0 merged 1 commit into
0xShug0:mainfrom
Liquid4All:malformed-option-400
Oct 28, 2026
Merged

0xShug0 merged 1 commit into
0xShug0:mainfrom
Liquid4All:malformed-option-400

Conversation

@ykhrustalev

Copy link
Copy Markdown
Contributor

Problem
A non-numeric option value such as a JSON null, which reaches the option parsers as the string "null", made std::stoll/std::stof throw a bare stoll/stof error that the server answered with HTTP 500 without naming the option. A 64-bit unsigned option such as seed also accepted " -1" as UINT64_MAX.

Solution

  • Routes the std::sto* calls in options.cpp through a parse_number helper that throws InvalidRequestError naming the option for non-numeric, out-of-range and trailing-garbage values.
  • Throws InvalidRequestError instead of std::runtime_error from the range, finite, positive, size and bool checks, so the server answers 400.
  • Rejects a - anywhere in an unsigned option value, not only a leading one.
  • Maps InvalidRequestError to AUDIOCPP_ERR_INVALID_ARGUMENT in the C API guard.
  • Adds a malformed-option case to the C API path test and malformed seeds to cli_request_options_test, and makes lfm2_audio_session_test check that a malformed max_tokens error names the option.

Testing

  • macOS, Metal (M3 Ultra): cli_request_options_test, audiocpp_c_api_path_test and lfm2_audio_session_test pass; the first two fail with the options.cpp and audiocpp.cpp changes reverted (the C API test also with only audiocpp.cpp reverted); ctest has 0 failures (96 passed, 2 skipped for missing models).
  • macOS, audiocpp_server with Silero VAD on CPU, POST /v1/tasks/run: threshold "abc" or null and min_speech_duration_ms 99999999999 return 400 naming the option (500 without the change); threshold 0.5 returns 200.
  • macOS, audiocpp_cli with Silero VAD on CPU: --session-option/--request-option threshold=abc and a --request-sequence with "threshold": null exit 1 with threshold must be a float (without the change: stof: no conversion).

A non-numeric value such as a JSON null reached std::stoll, whose bare
"stoll" the server answered with 500. Option parse errors are now
InvalidRequestError naming the option (HTTP 400, C API invalid
argument), and an unsigned option no longer wraps " -1" to its maximum.
@0xShug0
0xShug0 merged commit 10a40c4 into 0xShug0:main Oct 28, 2026
7 checks passed
@0xShug0

0xShug0 commented Oct 28, 2026

Copy link
Copy Markdown
Owner

@ykhrustalev Thanks for the fix! Merged.

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.

2 participants