From 0a30d45f8f643bc3c3078fd9f341785a74df7fea Mon Sep 17 00:00:00 2001 From: drpetersonfernandes Date: Wed, 18 Nov 2026 16:46:54 -0300 Subject: [PATCH] fix: reject empty CLI paths and reserve room for FLAC frame headers `extractcd -o ""` now prints the usage error instead of crashing in `Directory.CreateDirectory("")`, and CdflCodec/AvHuffEncoder reserve 512 bytes of metadata per FLAC frame instead of 64 so CD audio with large Fixed subframes no longer fails with "FLAC frame too large". Adds regression tests for both, and bumps Meziantou.Analyzer 3.0.339 -> 3.0.341, coverlet.collector 10.1.0 -> 10.1.1 and Serilog.Extensions.Logging 10.0.0 -> 11.0.0. --- CHDSharpBattleTest/CHDSharpBattleTest.csproj | 2 +- CHDSharpBenchmark/CHDSharpBenchmark.csproj | 2 +- CHDSharpCli/CHDSharpCli.csproj | 4 ++-- CHDSharpCli/Program.cs | 8 +++++++ .../CHDSharpEncoderTest.csproj | 4 ++-- CHDSharpEncoderTest/CdflCodecTests.cs | 23 +++++++++++++++++++ CHDSharpLib/CHDSharpLib.csproj | 2 +- CHDSharpLib/Encoder/AvHuffEncoder.cs | 9 +++++++- CHDSharpLib/Encoder/CdflCodec.cs | 13 +++++++++-- CHDSharpTest/CHDSharpTest.csproj | 4 ++-- CHDSharpTest/CliIntegrationTests.cs | 17 ++++++++++++++ .../CHDSharpTestDataGeneration.csproj | 2 +- CHDSharpTester/CHDSharpTester.csproj | 4 ++-- VendoredFlac/VendoredFlac.csproj | 2 +- VendoredLZMA/VendoredLZMA.csproj | 2 +- VendoredZLib/VendoredZLib.csproj | 2 +- VendoredZSTD/VendoredZSTD.csproj | 2 +- 17 files changed, 83 insertions(+), 19 deletions(-) diff --git a/CHDSharpBattleTest/CHDSharpBattleTest.csproj b/CHDSharpBattleTest/CHDSharpBattleTest.csproj index 2db9add0..182f9f06 100644 --- a/CHDSharpBattleTest/CHDSharpBattleTest.csproj +++ b/CHDSharpBattleTest/CHDSharpBattleTest.csproj @@ -68,7 +68,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/CHDSharpBenchmark/CHDSharpBenchmark.csproj b/CHDSharpBenchmark/CHDSharpBenchmark.csproj index d977aa53..7c7507fa 100644 --- a/CHDSharpBenchmark/CHDSharpBenchmark.csproj +++ b/CHDSharpBenchmark/CHDSharpBenchmark.csproj @@ -9,7 +9,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/CHDSharpCli/CHDSharpCli.csproj b/CHDSharpCli/CHDSharpCli.csproj index 88b58c79..3c754511 100644 --- a/CHDSharpCli/CHDSharpCli.csproj +++ b/CHDSharpCli/CHDSharpCli.csproj @@ -11,7 +11,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -20,7 +20,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/CHDSharpCli/Program.cs b/CHDSharpCli/Program.cs index 0cdc7cab..930e43a7 100644 --- a/CHDSharpCli/Program.cs +++ b/CHDSharpCli/Program.cs @@ -4969,6 +4969,14 @@ private static (string? input, string? output, string[] rest) ParseCreateArgs(st rest.RemoveAt(0); } + // An explicitly empty argument (`-o ""`) is a missing path, not a valid one: passing it + // through used to reach Path.GetDirectoryName("")/Directory.CreateDirectory("") and crash + // with ArgumentException instead of printing the usage error. + if (string.IsNullOrWhiteSpace(input)) + input = null; + if (string.IsNullOrWhiteSpace(output)) + output = null; + return (input, output, rest.ToArray()); } diff --git a/CHDSharpEncoderTest/CHDSharpEncoderTest.csproj b/CHDSharpEncoderTest/CHDSharpEncoderTest.csproj index 20f6d51f..a4eceb85 100644 --- a/CHDSharpEncoderTest/CHDSharpEncoderTest.csproj +++ b/CHDSharpEncoderTest/CHDSharpEncoderTest.csproj @@ -8,11 +8,11 @@ $(NoWarn);xUnit1051 - + all runtime; build; native; contentfiles; analyzers; buildtransitive - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/CHDSharpEncoderTest/CdflCodecTests.cs b/CHDSharpEncoderTest/CdflCodecTests.cs index b0ba0631..d5370858 100644 --- a/CHDSharpEncoderTest/CdflCodecTests.cs +++ b/CHDSharpEncoderTest/CdflCodecTests.cs @@ -73,6 +73,29 @@ public void CdflCodec_Silence_CompressesWell() Assert.True(compressed.Length < 512); } + [Fact] + public void CdflCodec_OneFrameHunk_EncodesWithoutOverflowingTheFrameBuffer() + { + // A CHD with one CD frame per hunk makes the FLAC blocksize 588 samples. Fixed + // subframes can need more than 64 bytes of frame metadata, which used to overflow the + // per-hunk buffer ("FLAC frame too large: 2419 bytes (buffer 2416)"). + var hunk = new byte[CdConstants.FrameSize]; + for (var s = 0; s < 588; s++) + { + var sample = (int)(Math.Sin(s * 0.19) * 30000); + var offset = s * 4; + hunk[offset] = (byte)(sample >> 8); + hunk[offset + 1] = (byte)sample; + hunk[offset + 2] = (byte)(sample >> 8); + hunk[offset + 3] = (byte)sample; + } + + var codec = new CdflCodec(CdConstants.FrameSize); + var compressed = codec.Compress(hunk); + + Assert.NotNull(compressed); + } + [Fact] public void EncodeCd_WithCdfl_RoundTripsThroughChdSharpLib() { diff --git a/CHDSharpLib/CHDSharpLib.csproj b/CHDSharpLib/CHDSharpLib.csproj index 58b3446d..8787f5c0 100644 --- a/CHDSharpLib/CHDSharpLib.csproj +++ b/CHDSharpLib/CHDSharpLib.csproj @@ -29,7 +29,7 @@ true - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/CHDSharpLib/Encoder/AvHuffEncoder.cs b/CHDSharpLib/Encoder/AvHuffEncoder.cs index 5a244556..a611d13c 100644 --- a/CHDSharpLib/Encoder/AvHuffEncoder.cs +++ b/CHDSharpLib/Encoder/AvHuffEncoder.cs @@ -16,6 +16,13 @@ internal sealed class AvHuffEncoder { private const int RleAlphabetSize = 256 + 16; private const int MaxBits = 16; + + /// + /// Extra space reserved for a per-channel FLAC frame's header, subframe headers, Rice + /// parameters and footer. Fixed subframes can need more than 64 bytes (real CD audio + /// frames showed 67), so the earlier 64-byte allowance could overflow the encoder. + /// + private const int FlacFrameHeaderAllowance = 512; private readonly DeltaRleEncoder _cbContext = new(); private readonly DeltaRleEncoder _crContext = new(); @@ -171,7 +178,7 @@ int dstOffs dest[8] = 0xFF; dest[9] = 0xFF; - var flacOut = new byte[samples * 2 + 64]; + var flacOut = new byte[samples * 2 + FlacFrameHeaderAllowance]; var pcm = new short[samples]; for (var ch = 0; ch < channels; ch++) diff --git a/CHDSharpLib/Encoder/CdflCodec.cs b/CHDSharpLib/Encoder/CdflCodec.cs index a7e02665..44836194 100644 --- a/CHDSharpLib/Encoder/CdflCodec.cs +++ b/CHDSharpLib/Encoder/CdflCodec.cs @@ -14,6 +14,14 @@ namespace CHDSharp.Encoder; /// public sealed class CdflCodec : IChdCodec { + /// + /// Extra space reserved per FLAC frame for its header, subframe headers, Rice + /// partition parameters and footer. A frame's overhead can exceed 64 bytes (real + /// reports show 67 bytes for a 2352-byte CD frame with Fixed subframes), so 64 was + /// too tight and made valid audio fail with "FLAC frame too large". + /// + private const int FlacFrameHeaderAllowance = 512; + private readonly int _blockSize; private readonly int _dataBytes; private readonly byte[] _flacBuffer; @@ -38,8 +46,9 @@ public CdflCodec(uint hunkBytes) _blockSize /= 2; _leBuffer = new byte[_dataBytes]; - // worst case: verbatim subframes; add room for frame headers - _flacBuffer = new byte[_dataBytes + _framesPerHunk * 64]; + // Worst case: fixed/verbatim subframes; reserve room for the frame metadata of every + // frame (see FlacFrameHeaderAllowance). + _flacBuffer = new byte[_dataBytes + _framesPerHunk * FlacFrameHeaderAllowance]; } /// diff --git a/CHDSharpTest/CHDSharpTest.csproj b/CHDSharpTest/CHDSharpTest.csproj index 13533c7a..43e5a9fd 100644 --- a/CHDSharpTest/CHDSharpTest.csproj +++ b/CHDSharpTest/CHDSharpTest.csproj @@ -9,11 +9,11 @@ $(NoWarn);xUnit1051 - + all runtime; build; native; contentfiles; analyzers; buildtransitive - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/CHDSharpTest/CliIntegrationTests.cs b/CHDSharpTest/CliIntegrationTests.cs index 7f7ebf1b..b757ac5e 100644 --- a/CHDSharpTest/CliIntegrationTests.cs +++ b/CHDSharpTest/CliIntegrationTests.cs @@ -197,6 +197,23 @@ public void Createdvd_reports_usage_error_when_arguments_are_missing() ); } + [Fact] + public void Extractcd_reports_usage_error_for_an_empty_output_path() + { + // `-o ""` used to pass the null check, reach Directory.CreateDirectory("") and crash with + // an unhandled ArgumentException instead of printing the usage text. + var path = Path.Combine(TestDataDir, "v5_cd_default.chd"); + var (exitCode, log) = RunCli("extractcd", "--input", path, "--output", "\"\""); + + Assert.Equal(1, exitCode); + Assert.Contains( + "extractcd requires --input --output ", + log, + StringComparison.Ordinal + ); + Assert.DoesNotContain("Unhandled exception", log, StringComparison.Ordinal); + } + [Fact] public void Createcd_rejects_iso_size_that_is_not_sector_aligned() { diff --git a/CHDSharpTestDataGeneration/CHDSharpTestDataGeneration.csproj b/CHDSharpTestDataGeneration/CHDSharpTestDataGeneration.csproj index 678d7467..b122c47c 100644 --- a/CHDSharpTestDataGeneration/CHDSharpTestDataGeneration.csproj +++ b/CHDSharpTestDataGeneration/CHDSharpTestDataGeneration.csproj @@ -12,7 +12,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/CHDSharpTester/CHDSharpTester.csproj b/CHDSharpTester/CHDSharpTester.csproj index 462e0557..b234b1b2 100644 --- a/CHDSharpTester/CHDSharpTester.csproj +++ b/CHDSharpTester/CHDSharpTester.csproj @@ -11,7 +11,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -20,7 +20,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/VendoredFlac/VendoredFlac.csproj b/VendoredFlac/VendoredFlac.csproj index 05a54a17..72177ee9 100644 --- a/VendoredFlac/VendoredFlac.csproj +++ b/VendoredFlac/VendoredFlac.csproj @@ -10,7 +10,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/VendoredLZMA/VendoredLZMA.csproj b/VendoredLZMA/VendoredLZMA.csproj index 05e88200..f4f2c112 100644 --- a/VendoredLZMA/VendoredLZMA.csproj +++ b/VendoredLZMA/VendoredLZMA.csproj @@ -10,7 +10,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/VendoredZLib/VendoredZLib.csproj b/VendoredZLib/VendoredZLib.csproj index c44552f4..f490f484 100644 --- a/VendoredZLib/VendoredZLib.csproj +++ b/VendoredZLib/VendoredZLib.csproj @@ -11,7 +11,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/VendoredZSTD/VendoredZSTD.csproj b/VendoredZSTD/VendoredZSTD.csproj index 10cb30a1..5bd0f6ef 100644 --- a/VendoredZSTD/VendoredZSTD.csproj +++ b/VendoredZSTD/VendoredZSTD.csproj @@ -17,7 +17,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive