Skip to content

Commit

Permalink
istream/Length: implement length checks in _FillBucketList()
Browse files Browse the repository at this point in the history
Fixes the new (failing) unit tests.
  • Loading branch information
MaxKellermann committed Aug 16, 2023
1 parent bcc1f74 commit eb45205
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/istream/LengthIstream.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// author: Max Kellermann <[email protected]>

#include "LengthIstream.hxx"
#include "Bucket.hxx"

#include <stdexcept>

Expand All @@ -21,6 +22,28 @@ LengthIstream::_Skip(off_t length) noexcept
return nbytes;
}

void
LengthIstream::_FillBucketList(IstreamBucketList &list)
{
IstreamBucketList tmp;
FillBucketListFromInput(tmp);

const bool maybe_more = tmp.HasMore() || tmp.HasNonBuffer();
const std::size_t size = tmp.GetTotalBufferSize();

if ((off_t)size > remaining) {
Destroy();
throw std::runtime_error{"Too much data in stream"};
}

if (!maybe_more && (off_t)size < remaining) {
Destroy();
throw std::runtime_error{"Premature end of stream"};
}

list.SpliceBuffersFrom(std::move(tmp));
}

Istream::ConsumeBucketResult
LengthIstream::_ConsumeBucketList(std::size_t nbytes) noexcept
{
Expand Down
1 change: 1 addition & 0 deletions src/istream/LengthIstream.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public:

off_t _GetAvailable(bool) noexcept override;
off_t _Skip(off_t length) noexcept override;
void _FillBucketList(IstreamBucketList &list) override;
ConsumeBucketResult _ConsumeBucketList(std::size_t nbytes) noexcept override;
void _ConsumeDirect(std::size_t nbytes) noexcept override;

Expand Down

0 comments on commit eb45205

Please sign in to comment.