Zeugma440 / atldotnet

Fully managed, portable and easy-to-use C# library to read and edit audio data and metadata (tags) from various audio formats, playlists and CUE sheets
MIT License
460 stars 61 forks source link

Insufficient Characters Read #266

Closed j4587698 closed 4 months ago

j4587698 commented 4 months ago

The problem

When the stream read speed is not very fast (such as reading from a network data stream), there may be an issue of incomplete reads if the byte read length is too long. During testing, when retrieving built-in images, a significant number of images were only partially read. Upon inspecting the source code, it was discovered that the program's reading process does not handle read lengths, and the code directly assumes a completed read with lines like source.Read(buffer, 0, buffer.Length);. When the stream is slow, this may result in only a portion of the content being read, leading to problems.

Environment

Details

37468c59-bf83-4042-86e7-0fed580b6d67

Code To Reproduce Issue [ Good To Have ]

Please remember that with sample code it's easier to reproduce the bug and it's much faster to fix it.

Zeugma440 commented 4 months ago

I've changed the way pictures are read to rely on a read buffer instead of trying to read everything in one go.

You can control its size using ATL.Settings.FileBufferSize.

Can you test using the source code or do you need a release?

j4587698 commented 4 months ago

I apologize for not noticing your reply earlier. I have now addressed the issue by using an intermediary stream. I think you can go ahead and release it directly.

Zeugma440 commented 4 months ago

Fix is available in today's v5.24. Please close the issue if it works on your side.

j4587698 commented 4 months ago

There still seems to be a problem with the test. Looking at the code, for instance around line 1079 in ID3v2.cs, it appears that byte.Read is still being used.

int picSize = dataSize - (int)(source.Position - position);

byte[] data;
if (tag.UsesUnsynchronisation)
{
    data = decodeUnsynchronizedStream(source, picSize);
}
else
{
    data = new byte[picSize];
    source.Read(data, 0, picSize);
}
PictureInfo picInfo = PictureInfo.fromBinaryData(data, picType, getImplementedTagType(), picCode, picturePosition);
picInfo.Description = description;
Zeugma440 commented 4 months ago

There still seems to be a problem with the test. Looking at the code, for instance around line 1079 in ID3v2.cs, it appears that byte.Read is still being used.

Sorry, I forgot about that one. Thanks for letting me know. The fix will be available in the next release 😅

Zeugma440 commented 4 months ago

Fix is available in today's v5.25. Please close the issue if it works on your side.