aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2024-06-10 13:40:32 +0800
committercrupest <crupest@outlook.com>2024-06-10 23:48:32 +0800
commit2f5651cd1a1efb136179cdbcb3b29ed0cc11ca2a (patch)
tree400236cca0b1d28b0361f51c09ce4e2bf7d89d6c /src
parent633c77d7cd2dd5cd22018aca17da1490e34d94ec (diff)
downloadcru-2f5651cd1a1efb136179cdbcb3b29ed0cc11ca2a.tar.gz
cru-2f5651cd1a1efb136179cdbcb3b29ed0cc11ca2a.tar.bz2
cru-2f5651cd1a1efb136179cdbcb3b29ed0cc11ca2a.zip
HALF WORK: still need to fix invalid size value in Buffer.
NEED TEST: BufferStream, AutoReadStream, SubProcess.
Diffstat (limited to 'src')
-rw-r--r--src/common/Buffer.cpp10
-rw-r--r--src/common/io/AutoReadStream.cpp1
-rw-r--r--src/common/io/BufferStream.cpp2
3 files changed, 12 insertions, 1 deletions
diff --git a/src/common/Buffer.cpp b/src/common/Buffer.cpp
index af0871ca..e37fddc7 100644
--- a/src/common/Buffer.cpp
+++ b/src/common/Buffer.cpp
@@ -58,6 +58,8 @@ void Buffer::AssignBytes(Index dst_offset, std::byte* src, Index src_offset,
}
void Buffer::ResizeBuffer(Index new_size, bool preserve_used) {
+ CheckSize(new_size);
+
if (new_size == 0) {
Delete_();
ptr_ = nullptr;
@@ -83,6 +85,8 @@ void Buffer::ResizeBuffer(Index new_size, bool preserve_used) {
Index Buffer::PushFront(const std::byte* other, Index other_size,
bool use_memmove) {
+ CheckSize(other_size);
+
auto copy_size = std::min(used_begin_, other_size);
if (copy_size) {
@@ -105,6 +109,8 @@ bool Buffer::PushBack(std::byte b) {
Index Buffer::PushBack(const std::byte* other, Index other_size,
bool use_memmove) {
+ CheckSize(other_size);
+
auto copy_size = std::min(size_ - used_end_, other_size);
if (copy_size) {
@@ -117,12 +123,16 @@ Index Buffer::PushBack(const std::byte* other, Index other_size,
}
Index Buffer::PopFront(Index size) {
+ CheckSize(size);
+
auto move = std::min(used_begin_, size);
used_begin_ -= move;
return move;
}
Index Buffer::PopFront(std::byte* buffer, Index size, bool use_memmove) {
+ CheckSize(size);
+
auto pop_size = std::min(GetUsedSize(), size);
if (pop_size) {
diff --git a/src/common/io/AutoReadStream.cpp b/src/common/io/AutoReadStream.cpp
index edb77a47..bc43fa81 100644
--- a/src/common/io/AutoReadStream.cpp
+++ b/src/common/io/AutoReadStream.cpp
@@ -6,6 +6,7 @@ namespace cru::io {
AutoReadStream::AutoReadStream(Stream* stream, bool auto_delete,
const AutoReadStreamOptions& options) {
auto buffer_stream_options = options.GetBufferStreamOptions();
+ stream_ = stream;
size_per_read_ = buffer_stream_options.GetBlockSizeOrDefault();
buffer_stream_ = std::make_unique<BufferStream>(buffer_stream_options);
background_thread_ = std::thread(&AutoReadStream::BackgroundThreadRun, this);
diff --git a/src/common/io/BufferStream.cpp b/src/common/io/BufferStream.cpp
index b2467a17..3060a608 100644
--- a/src/common/io/BufferStream.cpp
+++ b/src/common/io/BufferStream.cpp
@@ -67,7 +67,7 @@ Index BufferStream::Write(const std::byte* buffer, Index offset, Index size) {
}
condition_variable_.wait(lock, [this] {
- return buffer_list_.size() < max_block_count_ ||
+ return max_block_count_ <= 0 || buffer_list_.size() < max_block_count_ ||
buffer_list_.back().GetBackFree() > 0;
});