Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix ~AcmStreamHeader finalizer exception #1199

Merged
merged 1 commit into from
Jan 4, 2025
Merged

Conversation

speige
Copy link
Contributor

@speige speige commented Jan 4, 2025

Fix application crash caused by unallocated GCHandle..Free in ~AcmStreamHeader finalizer. Occurs if corrupted data bytes cause exception during constructor.

@nightblade9
Copy link

Interesting. Do you know how to reproduce the crash that this fixes?

…eamHeader finalizer. Occurs if corrupted data bytes cause exception during constructor.
@speige
Copy link
Contributor Author

speige commented Jan 4, 2025

corruptFile.dmp
new Mp3FileReader(File.ReadAllBytes("corruptFile.dmp"))
The file is not a valid mp3.
DestBuffer = new byte[destBufferLength]; // destBufferLength is a negative value, throwing exception in AcmStreamHeader constructor before hDestBuffer has been allocated.
Exception during a ~finalizer will always crash a .net app with no possibility of try/catch because it's done on a separate GC thread outside user code.

@markheath markheath merged commit 7395d5c into naudio:master Jan 4, 2025
1 check passed
@nightblade9
Copy link

Oh, that's very cool. I'll add an automated test to cover it. Thank you for your contribution!

@nightblade9
Copy link

I'm not sure how to write a test for this. Both with and without your changes, I see the same OverflowException appear. Below is my test code.

[Test]
        public void Constructor_DoesNotThrow_IfFileIsInvalidOrCorrupted()
        {
            // Arrange
            var bytes = File.ReadAllBytes(Path.Join("TestData", "corruptFile.dmp"));
            var stream = new MemoryStream(bytes);

            // Act/Assert
            Assert.DoesNotThrow(() => new Mp3FileReader(stream));
        }

@speige
Copy link
Contributor Author

speige commented Jan 6, 2025 via email

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