Skip to content

Commit 56d9c48

Browse files
committed
crypto: fix excessive buffer allocation
Allocate buffer only if the next one isn't free.
1 parent e92f487 commit 56d9c48

2 files changed

Lines changed: 16 additions & 5 deletions

File tree

src/node_crypto_bio.cc

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -303,18 +303,26 @@ void NodeBIO::Write(const char* data, size_t size) {
303303

304304
// Go to next buffer if there still are some bytes to write
305305
if (left != 0) {
306-
if (write_head_->write_pos_ == kBufferLength) {
307-
Buffer* next = new Buffer();
308-
next->next_ = write_head_->next_;
309-
write_head_->next_ = next;
310-
}
306+
TryAllocateForWrite();
311307
write_head_ = write_head_->next_;
312308
}
313309
}
314310
assert(left == 0);
315311
}
316312

317313

314+
void NodeBIO::TryAllocateForWrite() {
315+
// If write head is full, next buffer is either read head or not empty.
316+
if (write_head_->write_pos_ == kBufferLength &&
317+
(write_head_->next_ == read_head_ ||
318+
write_head_->next_->write_pos_ != 0)) {
319+
Buffer* next = new Buffer();
320+
next->next_ = write_head_->next_;
321+
write_head_->next_ = next;
322+
}
323+
}
324+
325+
318326
void NodeBIO::Reset() {
319327
while (read_head_->read_pos_ != read_head_->write_pos_) {
320328
assert(read_head_->write_pos_ > read_head_->read_pos_);

src/node_crypto_bio.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ class NodeBIO {
5959

6060
~NodeBIO();
6161

62+
// Allocate new buffer for write if needed
63+
void TryAllocateForWrite();
64+
6265
// Read `len` bytes maximum into `out`, return actual number of read bytes
6366
size_t Read(char* out, size_t size);
6467

0 commit comments

Comments
 (0)