mirror of
https://github.com/SerenityOS/serenity
synced 2024-10-15 12:23:15 +00:00
LibCore: Limit to the user buffer size when reading lines from a stream
Previously, this would cause an assert to fail if one reads a completely buffered line into a buffer that is smaller than the Stream's internal buffer.
This commit is contained in:
parent
ff0a2b1a60
commit
1d5dbd1b80
|
@ -630,6 +630,8 @@ public:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto readable_size = min(m_buffered_size, buffer.size());
|
||||||
|
|
||||||
// The intention here is to try to match all of the possible
|
// The intention here is to try to match all of the possible
|
||||||
// delimiter candidates and try to find the longest one we can
|
// delimiter candidates and try to find the longest one we can
|
||||||
// remove from the buffer after copying up to the delimiter to the
|
// remove from the buffer after copying up to the delimiter to the
|
||||||
|
@ -637,7 +639,7 @@ public:
|
||||||
Optional<size_t> longest_match;
|
Optional<size_t> longest_match;
|
||||||
size_t match_size = 0;
|
size_t match_size = 0;
|
||||||
for (auto& candidate : candidates) {
|
for (auto& candidate : candidates) {
|
||||||
auto result = AK::memmem_optional(m_buffer.data(), m_buffered_size, candidate.bytes().data(), candidate.bytes().size());
|
auto result = AK::memmem_optional(m_buffer.data(), readable_size, candidate.bytes().data(), candidate.bytes().size());
|
||||||
if (result.has_value()) {
|
if (result.has_value()) {
|
||||||
auto previous_match = longest_match.value_or(*result);
|
auto previous_match = longest_match.value_or(*result);
|
||||||
if ((previous_match < *result) || (previous_match == *result && match_size < candidate.length())) {
|
if ((previous_match < *result) || (previous_match == *result && match_size < candidate.length())) {
|
||||||
|
@ -662,7 +664,6 @@ public:
|
||||||
// If we still haven't found anything, then it's most likely the case
|
// If we still haven't found anything, then it's most likely the case
|
||||||
// that the delimiter ends beyond the length of the caller-passed
|
// that the delimiter ends beyond the length of the caller-passed
|
||||||
// buffer. Let's just fill the caller's buffer up.
|
// buffer. Let's just fill the caller's buffer up.
|
||||||
auto readable_size = min(m_buffered_size, buffer.size());
|
|
||||||
auto buffer_to_take = m_buffer.span().slice(0, readable_size);
|
auto buffer_to_take = m_buffer.span().slice(0, readable_size);
|
||||||
auto buffer_to_shift = m_buffer.span().slice(readable_size);
|
auto buffer_to_shift = m_buffer.span().slice(readable_size);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue