From d8e4c83e9651f69195b3e618097b05f52505624d Mon Sep 17 00:00:00 2001 From: closer-claudio Date: Sat, 21 Nov 2026 15:20:51 -0800 Subject: [PATCH] decoders: an input that ends before its stream does is an error foxenflac and microflac exited 0 with an empty output on an input that ended before the stream info (a file that is not FLAC, a few bytes, a cut inside STREAMINFO), and claxon exited 0 with a short output on a stream cut at a frame boundary. `flac -d` exits 1 in each case, and each README promises 1 on any error. These libraries are fed bytes, or iterate to the end of them, and report nothing wrong: the program is the only place that knows. foxenflac and microflac now refuse an input whose stream info never came, and claxon holds its output to the frame count STREAMINFO declares, as drflac, foxenflac, microflac and cscoreflac already did. symphonia needs nothing: Symphonia reports "unexpected end of file" there itself. scripts/decoder-smoke.sh gains two rows for every program, neither of which may exit 0: the 16-bit stream cut to its first three bytes, and cut at a frame boundary. On darwin-arm64 the first fails on foxenflac before this change and the second on claxon. Co-Authored-By: Claude Fable 5.2 Signed-off-by: closer-claudio --- claxon/README.md | 5 +++-- claxon/src/main.rs | 13 ++++++++++++- cscoreflac/README.md | 3 ++- drflac/README.md | 3 ++- foxenflac/README.md | 5 +++-- foxenflac/main.c | 3 +++ microflac/README.md | 7 ++++--- microflac/main.cpp | 3 +++ scripts/decoder-smoke.sh | 26 +++++++++++++++++++++++--- symphonia/README.md | 3 ++- 10 files changed, 57 insertions(+), 14 deletions(-) diff --git a/claxon/README.md b/claxon/README.md index 9a7bae3..0c7328c 100644 --- a/claxon/README.md +++ b/claxon/README.md @@ -12,14 +12,15 @@ claxon The output is what `flac -d --force-raw-format --endian=little --sign=signed` writes: interleaved, signed, little-endian samples at the stream's own bit depth (8, 16 or 24 bits; another depth is refused). The stream's parameters go to stderr. Exit status 0 on success, 1 on -any error. Claxon predates RFC 9639: it reads frame-header depth codes for 8, 12, 16, 20 and 24 +any error, a stream that ends before the frame count its STREAMINFO declares included. Claxon predates RFC 9639: it reads frame-header depth codes for 8, 12, 16, 20 and 24 bits, refuses the 32-bit code as reserved, and refuses a header that leaves the depth to the stream info, so a 32-bit stream, or one of a depth without a code of its own, does not decode. - `src/main.rs` is the program around the crate, this repository's own. - `smoke.sh` runs `scripts/decoder-smoke.sh`, the test every decoder program here is held to: PCM encoded by the `flac` build at 8, 16 and 24 bits comes back byte for byte, with the stream's - parameters on stderr, and without arguments the program prints its usage and exits 1. + parameters on stderr, without arguments the program prints its usage and exits 1, and an input cut short is an + error. ## Licence diff --git a/claxon/src/main.rs b/claxon/src/main.rs index 686304d..22df1f5 100644 --- a/claxon/src/main.rs +++ b/claxon/src/main.rs @@ -27,13 +27,24 @@ fn run(input: &str, output: &str) -> Result<(), String> { ); let mut out = BufWriter::new(File::create(output).map_err(|e| format!("{output}: {e}"))?); + let mut written: u64 = 0; for sample in reader.samples() { let sample = sample.map_err(|e| format!("{input}: {e}"))?; // The low bytes of the two's complement are the sample at its depth. out.write_all(&sample.to_le_bytes()[..width]) .map_err(|e| format!("{output}: {e}"))?; + written += 1; + } + out.flush().map_err(|e| format!("{output}: {e}"))?; + // Claxon ends without an error where the input does, at a frame boundary. + let channels = u64::from(info.channels); + match info.samples { + Some(declared) if declared > 0 && written != declared * channels => Err(format!( + "{input}: decoded {} frames, the stream declares {declared}", + written / channels + )), + _ => Ok(()), } - out.flush().map_err(|e| format!("{output}: {e}")) } fn main() -> ExitCode { diff --git a/cscoreflac/README.md b/cscoreflac/README.md index dd66fe0..ffe2a40 100644 --- a/cscoreflac/README.md +++ b/cscoreflac/README.md @@ -38,7 +38,8 @@ does not let a program catch; the process dies, with a nonzero exit status. `just cscoreflac-lock` rewrites it. - `smoke.sh` runs `scripts/decoder-smoke.sh`, the test every decoder program here is held to: PCM encoded by the `flac` build at 8, 16 and 24 bits comes back byte for byte, with the stream's - parameters on stderr, and without arguments the program prints its usage and exits 1. + parameters on stderr, without arguments the program prints its usage and exits 1, and an input cut short is an + error. ## Licence diff --git a/drflac/README.md b/drflac/README.md index 2a4c844..7ea7346 100644 --- a/drflac/README.md +++ b/drflac/README.md @@ -17,7 +17,8 @@ any error. - `build.sh` compiles it for one zig target. - `smoke.sh` runs `scripts/decoder-smoke.sh`, the test every decoder program here is held to: PCM encoded by the `flac` build at 8, 16, 24 and 32 bits comes back byte for byte, with the stream's - parameters on stderr, and without arguments the program prints its usage and exits 1. + parameters on stderr, without arguments the program prints its usage and exits 1, and an input cut short is an + error. ## Licence diff --git a/foxenflac/README.md b/foxenflac/README.md index bf95cd1..cf68868 100644 --- a/foxenflac/README.md +++ b/foxenflac/README.md @@ -16,13 +16,14 @@ libfoxenflac reads the frame-header depth codes for 8, 12, 16, 20 and 24 bits an 32-bit stream does not decode; 12 and 20 bits decode but have no whole-byte raw form and are refused. libfoxenflac left-justifies every depth in 32 bits; the program shifts each sample back. The stream's parameters go to stderr. Exit status 0 on success, 1 on any error, a stream that -ends before the frame count its STREAMINFO declares included. +ends before its stream info or before the frame count its STREAMINFO declares included. - `main.c` is the program around the library, this repository's own. - `build.sh` compiles it and `src/foxen-flac.c` for one zig target. - `smoke.sh` runs `scripts/decoder-smoke.sh`, the test every decoder program here is held to: PCM encoded by the `flac` build at 8, 16 and 24 bits comes back byte for byte, with the stream's - parameters on stderr, and without arguments the program prints its usage and exits 1. + parameters on stderr, without arguments the program prints its usage and exits 1, and an input cut short is an + error. ## Licence diff --git a/foxenflac/main.c b/foxenflac/main.c index eb22c19..8797e1b 100644 --- a/foxenflac/main.c +++ b/foxenflac/main.c @@ -89,6 +89,9 @@ int main(int argc, char **argv) { if (have == sizeof(buf)) return fail(input, "the decoder makes no progress"); } } + /* libfoxenflac is fed bytes and never told they ended: only here is it + * known that the stream info never came. */ + if (width == 0) return fail(input, "the input ends before the stream info"); if (channels > 0 && declared > 0 && written != (unsigned long long)(declared * channels)) { fprintf(stderr, "foxenflac: %s: decoded %llu frames, the stream declares %lld\n", input, written / (unsigned long long)channels, declared); diff --git a/microflac/README.md b/microflac/README.md index dfd32ae..ede0110 100644 --- a/microflac/README.md +++ b/microflac/README.md @@ -15,15 +15,16 @@ The output is what `flac -d --force-raw-format --endian=little --sign=signed` wr interleaved, signed, little-endian samples at the stream's own bit depth (8, 16, 24 or 32 bits; another depth is refused), which is microFLAC's own byte-packed output on every host. Frame CRCs are checked (microFLAC's default). The stream's parameters go to stderr. Exit status 0 on -success, 1 on any error, a stream that ends before the frame count its STREAMINFO declares -included. +success, 1 on any error, a stream that ends before its stream info or before the frame count its +STREAMINFO declares included. - `main.cpp` is the program around the library, this repository's own. - `build.sh` compiles it and microFLAC's decoder sources (`cmake/sources.cmake`'s) for one zig target. - `smoke.sh` runs `scripts/decoder-smoke.sh`, the test every decoder program here is held to: PCM encoded by the `flac` build at 8, 16, 24 and 32 bits comes back byte for byte, with the stream's - parameters on stderr, and without arguments the program prints its usage and exits 1. + parameters on stderr, without arguments the program prints its usage and exits 1, and an input cut short is an + error. ## Licence diff --git a/microflac/main.cpp b/microflac/main.cpp index 4db12c3..06939d6 100644 --- a/microflac/main.cpp +++ b/microflac/main.cpp @@ -91,6 +91,9 @@ int main(int argc, char **argv) { } if (std::fclose(out) != 0) return fail(output, "write error"); + // microFLAC is fed bytes and never told they ended: only here is it known + // that the stream info never came. + if (channels == 0) return fail(input, "the input ends before the stream info"); const uint64_t declared = decoder.get_stream_info().total_samples_per_channel(); if (channels > 0 && declared > 0 && written != declared * channels) { std::fprintf(stderr, "microflac: %s: decoded %llu frames, the stream declares %llu\n", input, diff --git a/scripts/decoder-smoke.sh b/scripts/decoder-smoke.sh index f329845..58f64eb 100755 --- a/scripts/decoder-smoke.sh +++ b/scripts/decoder-smoke.sh @@ -2,9 +2,11 @@ # What each FLAC decoder program here is held to: its own part, the program # around someone else's decoder. The bytes flac encoded come back, in the raw # form its README names, at every depth given; the stream's parameters go to -# stderr; and without arguments it prints its usage and exits 1. What the -# library under it does with a stream it gets wrong or cannot read is the -# library's, and is not tested here. +# stderr; without arguments it prints its usage and exits 1; and an input that +# ends before its stream does is an error, which only the program can know: +# the libraries are fed bytes and never told they ended. What the library +# under it does with a stream it gets wrong or cannot read is the library's, +# and is not tested here. # decoder-smoke.sh # The depths are one argument ("8 16 24"). set -euo pipefail @@ -46,3 +48,21 @@ for bps in "${depths[@]}"; do { cat "${work}/stderr" >&2; echo "${name}: ${bps}-bit: the stream's parameters are not on stderr" >&2; exit 1; } echo "${name}: ${bps}-bit round trip through flac identical" done + +# An input that ends early is an error, as it is to `flac -d`: inside the +# stream marker, and at a frame boundary, where a library sees nothing wrong. +# The second is the whole stream cut to the length of a stream of its first +# blocks: without a seek table the two differ in their stream info alone. +head -c 3 "${work}/a-16.flac" > "${work}/marker.flac" +part=$((bytes / 2 / 16384 * 16384)) +head -c "${part}" "${work}/in.raw" > "${work}/part.raw" +for stream in in part; do + "${flac}" -s "${raw[@]}" --no-seektable --channels=2 --bps=16 --sample-rate=44100 -o "${work}/${stream}.flac" "${work}/${stream}.raw" +done +head -c "$(wc -c < "${work}/part.flac" | tr -d ' ')" "${work}/in.flac" > "${work}/boundary.flac" +for cut in marker boundary; do + status=0 + "${decoder}" "${work}/${cut}.flac" "${work}/${cut}.raw" 2> "${work}/stderr" || status=$? + [ "${status}" -ne 0 ] || { echo "${name}: an input cut short (${cut}): exit status 0" >&2; exit 1; } + echo "${name}: an input cut short (${cut}) is an error" +done diff --git a/symphonia/README.md b/symphonia/README.md index 82d8a3f..198de57 100644 --- a/symphonia/README.md +++ b/symphonia/README.md @@ -21,7 +21,8 @@ success, 1 on any error, a frame Symphonia cannot decode included. - `src/main.rs` is the program around the crates, this repository's own. - `smoke.sh` runs `scripts/decoder-smoke.sh`, the test every decoder program here is held to: PCM encoded by the `flac` build at 8, 16 and 24 bits comes back byte for byte, with the stream's - parameters on stderr, and without arguments the program prints its usage and exits 1. Not at 32 + parameters on stderr, without arguments the program prints its usage and exits 1, and an input cut short is an + error. Not at 32 bits, for the reason above. ## Licence