Skip to content

Commit 9f0ce45

Browse files
committed
src: fixup manual new/delete usages
Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: Opencode/Opus PR-URL: #65348 Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com>
1 parent e2cb91d commit 9f0ce45

20 files changed

Lines changed: 307 additions & 338 deletions

‎src/cares_wrap.cc‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,19 +1014,18 @@ void NodeAresTask::MemoryInfo(MemoryTracker* tracker) const {
10141014

10151015
/* Allocates and returns a new NodeAresTask */
10161016
NodeAresTask* NodeAresTask::Create(ChannelWrap* channel, ares_socket_t sock) {
1017-
auto task = new NodeAresTask();
1017+
auto task = std::make_unique<NodeAresTask>();
10181018

10191019
task->channel = channel;
10201020
task->sock = sock;
10211021

10221022
if (uv_poll_init_socket(channel->env()->event_loop(),
10231023
&task->poll_watcher, sock) < 0) {
10241024
/* This should never happen. */
1025-
delete task;
10261025
return nullptr;
10271026
}
10281027

1029-
return task;
1028+
return task.release();
10301029
}
10311030

10321031
void ChannelWrap::Setup() {

‎src/compile_cache.cc‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -170,28 +170,27 @@ void CompileCacheHandler::ReadCacheFile(CompileCacheEntry* entry) {
170170
size_t offset = headers_buf.len;
171171
size_t capacity = 4096; // Initial buffer capacity
172172
size_t total_read = 0;
173-
uint8_t* buffer = new uint8_t[capacity];
173+
auto buffer = std::make_unique<uint8_t[]>(capacity);
174174

175175
while (true) {
176176
// If there is not enough space to read more data, do a simple
177177
// realloc here (we don't actually realloc because V8 requires
178178
// the underlying buffer to be delete[]-able).
179179
if (total_read == capacity) {
180180
size_t new_capacity = capacity * 2;
181-
auto* new_buffer = new uint8_t[new_capacity];
182-
memcpy(new_buffer, buffer, capacity);
183-
delete[] buffer;
184-
buffer = new_buffer;
181+
auto new_buffer = std::make_unique<uint8_t[]>(new_capacity);
182+
memcpy(new_buffer.get(), buffer.get(), capacity);
183+
buffer = std::move(new_buffer);
185184
capacity = new_capacity;
186185
}
187186

188-
uv_buf_t iov = uv_buf_init(reinterpret_cast<char*>(buffer + total_read),
189-
capacity - total_read);
187+
uv_buf_t iov =
188+
uv_buf_init(reinterpret_cast<char*>(buffer.get() + total_read),
189+
capacity - total_read);
190190
int bytes_read =
191191
uv_fs_read(nullptr, &req, file, &iov, 1, offset + total_read, nullptr);
192192
if (req.result < 0) { // Error.
193193
// req will be cleaned up by scope leave.
194-
delete[] buffer;
195194
Debug(" %s\n", uv_strerror(req.result));
196195
return;
197196
}
@@ -209,7 +208,8 @@ void CompileCacheHandler::ReadCacheFile(CompileCacheEntry* entry) {
209208
total_read);
210209
return;
211210
}
212-
uint32_t cache_hash = GetHash(reinterpret_cast<char*>(buffer), total_read);
211+
uint32_t cache_hash =
212+
GetHash(reinterpret_cast<char*>(buffer.get()), total_read);
213213
if (headers[kCacheHashOffset] != cache_hash) {
214214
Debug("cache hash mismatch: expected %d, actual %d\n",
215215
headers[kCacheHashOffset],
@@ -218,7 +218,7 @@ void CompileCacheHandler::ReadCacheFile(CompileCacheEntry* entry) {
218218
}
219219

220220
entry->cache.reset(new ScriptCompiler::CachedData(
221-
buffer, total_read, ScriptCompiler::CachedData::BufferOwned));
221+
buffer.release(), total_read, ScriptCompiler::CachedData::BufferOwned));
222222
Debug(" success, size=%d\n", total_read);
223223
}
224224

‎src/crypto/crypto_bio.cc‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ int NodeBIO::Read(BIO* bio, char* out, int len) {
9999

100100
char* NodeBIO::Peek(size_t* size) {
101101
*size = read_head_->write_pos_ - read_head_->read_pos_;
102-
return read_head_->data_ + read_head_->read_pos_;
102+
return read_head_->data_.get() + read_head_->read_pos_;
103103
}
104104

105105

@@ -112,7 +112,7 @@ size_t NodeBIO::PeekMultiple(char** out, size_t* size, size_t* count) {
112112
for (i = 0; i < max; i++) {
113113
size[i] = pos->write_pos_ - pos->read_pos_;
114114
total += size[i];
115-
out[i] = pos->data_ + pos->read_pos_;
115+
out[i] = pos->data_.get() + pos->read_pos_;
116116

117117
/* Don't get past write head */
118118
if (pos == write_head_)
@@ -274,7 +274,8 @@ size_t NodeBIO::Read(char* out, size_t size) {
274274

275275
// Copy data
276276
if (out != nullptr)
277-
memcpy(out + offset, read_head_->data_ + read_head_->read_pos_, avail);
277+
memcpy(
278+
out + offset, read_head_->data_.get() + read_head_->read_pos_, avail);
278279
read_head_->read_pos_ += avail;
279280

280281
// Move pointers
@@ -330,7 +331,7 @@ size_t NodeBIO::IndexOf(char delim, size_t limit) {
330331
avail = left;
331332

332333
// Walk through data
333-
char* tmp = current->data_ + current->read_pos_;
334+
char* tmp = current->data_.get() + current->read_pos_;
334335
size_t off = 0;
335336
while (off < avail && *tmp != delim) {
336337
off++;
@@ -373,7 +374,7 @@ void NodeBIO::Write(const char* data, size_t size) {
373374
to_write = avail;
374375

375376
// Copy data
376-
memcpy(write_head_->data_ + write_head_->write_pos_,
377+
memcpy(write_head_->data_.get() + write_head_->write_pos_,
377378
data + offset,
378379
to_write);
379380

@@ -406,7 +407,7 @@ char* NodeBIO::PeekWritable(size_t* size) {
406407
if (*size == 0 || available <= *size)
407408
*size = available;
408409

409-
return write_head_->data_ + write_head_->write_pos_;
410+
return write_head_->data_.get() + write_head_->write_pos_;
410411
}
411412

412413

‎src/crypto/crypto_bio.h‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,19 +150,19 @@ class NodeBIO : public MemoryRetainer {
150150

151151
class Buffer {
152152
public:
153-
Buffer(Environment* env, size_t len) : env_(env),
154-
read_pos_(0),
155-
write_pos_(0),
156-
len_(len),
157-
next_(nullptr) {
158-
data_ = new char[len];
153+
Buffer(Environment* env, size_t len)
154+
: env_(env),
155+
read_pos_(0),
156+
write_pos_(0),
157+
len_(len),
158+
next_(nullptr),
159+
data_(new char[len]) {
159160
if (env_ != nullptr) {
160161
env_->external_memory_accounter()->Increase(env_->isolate(), len);
161162
}
162163
}
163164

164165
~Buffer() {
165-
delete[] data_;
166166
if (env_ != nullptr) {
167167
env_->external_memory_accounter()->Decrease(env_->isolate(), len_);
168168
}
@@ -173,7 +173,7 @@ class NodeBIO : public MemoryRetainer {
173173
size_t write_pos_;
174174
size_t len_;
175175
Buffer* next_;
176-
char* data_;
176+
std::unique_ptr<char[]> data_;
177177
};
178178

179179
Environment* env_ = nullptr;

0 commit comments

Comments
 (0)