Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 45c1ebd

Browse filesBrowse files
legendecasaduh95
authored andcommitted
quic: copy options.certs buffer instead of detaching
The certs could be allocated in a pooled buffer, like `Buffer.from`, and `Buffer.allocUnsafe` (used by `fs.readFileSync`, etc). PR-URL: #61403 Refs: #61372 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent 722c0c3 commit 45c1ebd
Copy full SHA for 45c1ebd

6 files changed

+50-20Lines changed: 50 additions & 20 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/quic/data.cc‎

Copy file name to clipboardExpand all lines: src/quic/data.cc
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,29 @@ Maybe<Store> Store::From(Local<ArrayBufferView> view, Local<Value> detach_key) {
115115
return Just(Store(std::move(backing), length, offset));
116116
}
117117

118+
Store Store::CopyFrom(Local<ArrayBuffer> buffer) {
119+
v8::Isolate* isolate = v8::Isolate::GetCurrent();
120+
auto backing = buffer->GetBackingStore();
121+
auto length = buffer->ByteLength();
122+
auto dest = ArrayBuffer::NewBackingStore(
123+
isolate, length, v8::BackingStoreInitializationMode::kUninitialized);
124+
// copy content
125+
memcpy(dest->Data(), backing->Data(), length);
126+
return Store(std::move(dest), length, 0);
127+
}
128+
129+
Store Store::CopyFrom(Local<ArrayBufferView> view) {
130+
v8::Isolate* isolate = v8::Isolate::GetCurrent();
131+
auto backing = view->Buffer()->GetBackingStore();
132+
auto length = view->ByteLength();
133+
auto offset = view->ByteOffset();
134+
auto dest = ArrayBuffer::NewBackingStore(
135+
isolate, length, v8::BackingStoreInitializationMode::kUninitialized);
136+
// copy content
137+
memcpy(dest->Data(), static_cast<char*>(backing->Data()) + offset, length);
138+
return Store(std::move(dest), length, 0);
139+
}
140+
118141
Local<Uint8Array> Store::ToUint8Array(Environment* env) const {
119142
return !store_
120143
? Uint8Array::New(ArrayBuffer::New(env->isolate(), 0), 0, 0)
Collapse file

‎src/quic/data.h‎

Copy file name to clipboardExpand all lines: src/quic/data.h
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ class Store final : public MemoryRetainer {
7070
v8::Local<v8::ArrayBufferView> view,
7171
v8::Local<v8::Value> detach_key = v8::Local<v8::Value>());
7272

73+
// Creates a Store from the contents of an ArrayBuffer, always copying the
74+
// content.
75+
static Store CopyFrom(v8::Local<v8::ArrayBuffer> buffer);
76+
77+
// Creates a Store from the contents of an ArrayBufferView, always copying the
78+
// content.
79+
static Store CopyFrom(v8::Local<v8::ArrayBufferView> view);
80+
7381
v8::Local<v8::Uint8Array> ToUint8Array(Environment* env) const;
7482
inline v8::Local<v8::Uint8Array> ToUint8Array(Realm* realm) const {
7583
return ToUint8Array(realm->env());
Collapse file

‎src/quic/endpoint.cc‎

Copy file name to clipboardExpand all lines: src/quic/endpoint.cc
+1-4Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,7 @@ bool SetOption(Environment* env,
154154
env, "The %s option must be an ArrayBufferView", nameStr);
155155
return false;
156156
}
157-
Store store;
158-
if (!Store::From(value.As<ArrayBufferView>()).To(&store)) {
159-
return false;
160-
}
157+
Store store = Store::CopyFrom(value.As<ArrayBufferView>());
161158
if (store.length() != TokenSecret::QUIC_TOKENSECRET_LEN) {
162159
Utf8Value nameStr(env->isolate(), name);
163160
THROW_ERR_INVALID_ARG_VALUE(
Collapse file

‎src/quic/tlscontext.cc‎

Copy file name to clipboardExpand all lines: src/quic/tlscontext.cc
+4-16Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -133,16 +133,10 @@ bool SetOption(Environment* env,
133133
}
134134
} else if constexpr (std::is_same<T, Store>::value) {
135135
if (item->IsArrayBufferView()) {
136-
Store store;
137-
if (!Store::From(item.As<v8::ArrayBufferView>()).To(&store)) {
138-
return false;
139-
}
136+
Store store = Store::CopyFrom(item.As<v8::ArrayBufferView>());
140137
(options->*member).push_back(std::move(store));
141138
} else if (item->IsArrayBuffer()) {
142-
Store store;
143-
if (!Store::From(item.As<ArrayBuffer>()).To(&store)) {
144-
return false;
145-
}
139+
Store store = Store::CopyFrom(item.As<ArrayBuffer>());
146140
(options->*member).push_back(std::move(store));
147141
} else {
148142
Utf8Value namestr(env->isolate(), name);
@@ -168,16 +162,10 @@ bool SetOption(Environment* env,
168162
}
169163
} else if constexpr (std::is_same<T, Store>::value) {
170164
if (value->IsArrayBufferView()) {
171-
Store store;
172-
if (!Store::From(value.As<v8::ArrayBufferView>()).To(&store)) {
173-
return false;
174-
}
165+
Store store = Store::CopyFrom(value.As<v8::ArrayBufferView>());
175166
(options->*member).push_back(std::move(store));
176167
} else if (value->IsArrayBuffer()) {
177-
Store store;
178-
if (!Store::From(value.As<ArrayBuffer>()).To(&store)) {
179-
return false;
180-
}
168+
Store store = Store::CopyFrom(value.As<ArrayBuffer>());
181169
(options->*member).push_back(std::move(store));
182170
} else {
183171
Utf8Value namestr(env->isolate(), name);
Collapse file

‎test/parallel/test-quic-handshake.mjs‎

Copy file name to clipboardExpand all lines: test/parallel/test-quic-handshake.mjs
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ const serverEndpoint = await listen(mustCall((serverSession) => {
3838
}));
3939
}), { keys, certs });
4040

41+
// Buffer is not detached.
42+
assert.strictEqual(certs.buffer.detached, false);
43+
4144
// The server must have an address to connect to after listen resolves.
4245
assert.ok(serverEndpoint.address !== undefined);
4346

Collapse file

‎test/parallel/test-quic-internal-endpoint-listen-defaults.mjs‎

Copy file name to clipboardExpand all lines: test/parallel/test-quic-internal-endpoint-listen-defaults.mjs
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,24 @@ assert.strictEqual(endpoint.address, undefined);
2828
await assert.rejects(listen(123, { keys, certs, endpoint }), {
2929
code: 'ERR_INVALID_ARG_TYPE',
3030
});
31+
// Buffer is not detached.
32+
assert.strictEqual(certs.buffer.detached, false);
3133

3234
await assert.rejects(listen(() => {}, 123), {
3335
code: 'ERR_INVALID_ARG_TYPE',
3436
});
3537

3638
await listen(() => {}, { keys, certs, endpoint });
39+
// Buffer is not detached.
40+
assert.strictEqual(certs.buffer.detached, false);
41+
3742
await assert.rejects(listen(() => {}, { keys, certs, endpoint }), {
3843
code: 'ERR_INVALID_STATE',
3944
});
4045

46+
// Buffer is not detached.
47+
assert.strictEqual(certs.buffer.detached, false);
48+
4149
assert.ok(endpoint[kState].isBound);
4250
assert.ok(endpoint[kState].isReceiving);
4351
assert.ok(endpoint[kState].isListening);
@@ -59,6 +67,9 @@ assert.ok(endpoint.destroyed);
5967
await assert.rejects(listen(() => {}, { keys, certs, endpoint }), {
6068
code: 'ERR_INVALID_STATE',
6169
});
70+
// Buffer is not detached.
71+
assert.strictEqual(certs.buffer.detached, false);
72+
6273
assert.throws(() => { endpoint.busy = true; }, {
6374
code: 'ERR_INVALID_STATE',
6475
});

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.