AbstractReader.Skip() does not fully read bytes from non-local streams #214

Closed
opened 2026-01-29 22:08:30 +00:00 by claunia · 2 comments
Owner

Originally created by @heebaek on GitHub (Jul 14, 2017).

for (var i = 0; i < bytesToAdvance / skipBuffer.Length; i++)
{
rawStream.Read(skipBuffer, 0, skipBuffer.Length);
}
rawStream.Read(skipBuffer, 0, (int)(bytesToAdvance % skipBuffer.Length));

this code assume rawStream.Read always read until requested count.

but if you test with remote stream (from network like ftp), MoveToNextEntry() throw Unknown header exception. because skip function do not actually skip.

i fixed code like this, and it works fine for me.

                var bytesToAdvance = Entry.CompressedSize;
                int readBytes = 0;
                while (bytesToAdvance > 0)
                {
                    if (bytesToAdvance >= skipBuffer.Length)
                        readBytes = rawStream.Read(skipBuffer, 0, skipBuffer.Length);
                    else
                        readBytes = rawStream.Read(skipBuffer, 0, (int)bytesToAdvance);

                    bytesToAdvance -= readBytes;
                    if (readBytes == 0 && bytesToAdvance > 0)
                        throw new Exception("skip failed");
                }
                return;
            }

please let me know if it is not a bug.

Originally created by @heebaek on GitHub (Jul 14, 2017). for (var i = 0; i < bytesToAdvance / skipBuffer.Length; i++) { rawStream.Read(skipBuffer, 0, skipBuffer.Length); } rawStream.Read(skipBuffer, 0, (int)(bytesToAdvance % skipBuffer.Length)); this code assume rawStream.Read always read until requested count. but if you test with remote stream (from network like ftp), MoveToNextEntry() throw Unknown header exception. because skip function do not actually skip. i fixed code like this, and it works fine for me. var bytesToAdvance = Entry.CompressedSize; int readBytes = 0; while (bytesToAdvance > 0) { if (bytesToAdvance >= skipBuffer.Length) readBytes = rawStream.Read(skipBuffer, 0, skipBuffer.Length); else readBytes = rawStream.Read(skipBuffer, 0, (int)bytesToAdvance); bytesToAdvance -= readBytes; if (readBytes == 0 && bytesToAdvance > 0) throw new Exception("skip failed"); } return; } please let me know if it is not a bug.
claunia added the bug label 2026-01-29 22:08:30 +00:00
Author
Owner

@adamhathcock commented on GitHub (Jul 14, 2017):

You're probably correct in that I have some bad skip code. I've noticed recently there are several different implementations of this kind of thing in the codebase. I need to consolidate this at the very least.

I have Skip/Transfer code in Utility.cs that probably should be used instead and does what you're referring to.

I likely won't get time for a while to fix this but pull requests are welcome :)

@adamhathcock commented on GitHub (Jul 14, 2017): You're probably correct in that I have some bad skip code. I've noticed recently there are several different implementations of this kind of thing in the codebase. I need to consolidate this at the very least. I have Skip/Transfer code in Utility.cs that probably should be used instead and does what you're referring to. I likely won't get time for a while to fix this but pull requests are welcome :)
Author
Owner

@adamhathcock commented on GitHub (Jul 15, 2017):

Made a PR to fix this issue

@adamhathcock commented on GitHub (Jul 15, 2017): Made a PR to fix this issue
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#214