GeezerVison is showing github.com — quic: copy options.certs buffer instead of detaching · nodejs/node@45c1ebd
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/quic/data.cc +23Lines changed: 23 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -115,6 +115,29 @@ Maybe<Store> Store::From(Local<ArrayBufferView> view, Local<Value> detach_key) { | 115 | 115 | return Just(Store(std::move(backing), length, offset)); | 116 | 116 | } | 117 | 117 |
| | 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 | + | 118 | 141 | Local<Uint8Array> Store::ToUint8Array(Environment* env) const { | 119 | 142 | return !store_ | 120 | 143 | ? Uint8Array::New(ArrayBuffer::New(env->isolate(), 0), 0, 0) | |
Collapse file Copy file name to clipboardExpand all lines: src/quic/data.h +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -70,6 +70,14 @@ class Store final : public MemoryRetainer { | 70 | 70 | v8::Local<v8::ArrayBufferView> view, | 71 | 71 | v8::Local<v8::Value> detach_key = v8::Local<v8::Value>()); | 72 | 72 |
| | 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 | + | 73 | 81 | v8::Local<v8::Uint8Array> ToUint8Array(Environment* env) const; | 74 | 82 | inline v8::Local<v8::Uint8Array> ToUint8Array(Realm* realm) const { | 75 | 83 | return ToUint8Array(realm->env()); | |
Collapse file Copy file name to clipboardExpand all lines: src/quic/endpoint.cc +1-4Lines changed: 1 addition & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -154,10 +154,7 @@ bool SetOption(Environment* env, | 154 | 154 | env, "The %s option must be an ArrayBufferView", nameStr); | 155 | 155 | return false; | 156 | 156 | } | 157 | | - Store store; | 158 | | - if (!Store::From(value.As<ArrayBufferView>()).To(&store)) { | 159 | | - return false; | 160 | | - } | | 157 | + Store store = Store::CopyFrom(value.As<ArrayBufferView>()); | 161 | 158 | if (store.length() != TokenSecret::QUIC_TOKENSECRET_LEN) { | 162 | 159 | Utf8Value nameStr(env->isolate(), name); | 163 | 160 | THROW_ERR_INVALID_ARG_VALUE( | |
Collapse file Copy file name to clipboardExpand all lines: src/quic/tlscontext.cc +4-16Lines changed: 4 additions & 16 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -133,16 +133,10 @@ bool SetOption(Environment* env, | 133 | 133 | } | 134 | 134 | } else if constexpr (std::is_same<T, Store>::value) { | 135 | 135 | 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>()); | 140 | 137 | (options->*member).push_back(std::move(store)); | 141 | 138 | } 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>()); | 146 | 140 | (options->*member).push_back(std::move(store)); | 147 | 141 | } else { | 148 | 142 | Utf8Value namestr(env->isolate(), name); | @@ -168,16 +162,10 @@ bool SetOption(Environment* env, | 168 | 162 | } | 169 | 163 | } else if constexpr (std::is_same<T, Store>::value) { | 170 | 164 | 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>()); | 175 | 166 | (options->*member).push_back(std::move(store)); | 176 | 167 | } 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>()); | 181 | 169 | (options->*member).push_back(std::move(store)); | 182 | 170 | } else { | 183 | 171 | Utf8Value namestr(env->isolate(), name); | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-quic-handshake.mjs +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -38,6 +38,9 @@ const serverEndpoint = await listen(mustCall((serverSession) => { | 38 | 38 | })); | 39 | 39 | }), { keys, certs }); | 40 | 40 |
| | 41 | +// Buffer is not detached. | | 42 | +assert.strictEqual(certs.buffer.detached, false); | | 43 | + | 41 | 44 | // The server must have an address to connect to after listen resolves. | 42 | 45 | assert.ok(serverEndpoint.address !== undefined); | 43 | 46 |
| |
Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -28,16 +28,24 @@ assert.strictEqual(endpoint.address, undefined); | 28 | 28 | await assert.rejects(listen(123, { keys, certs, endpoint }), { | 29 | 29 | code: 'ERR_INVALID_ARG_TYPE', | 30 | 30 | }); | | 31 | +// Buffer is not detached. | | 32 | +assert.strictEqual(certs.buffer.detached, false); | 31 | 33 |
| 32 | 34 | await assert.rejects(listen(() => {}, 123), { | 33 | 35 | code: 'ERR_INVALID_ARG_TYPE', | 34 | 36 | }); | 35 | 37 |
| 36 | 38 | await listen(() => {}, { keys, certs, endpoint }); | | 39 | +// Buffer is not detached. | | 40 | +assert.strictEqual(certs.buffer.detached, false); | | 41 | + | 37 | 42 | await assert.rejects(listen(() => {}, { keys, certs, endpoint }), { | 38 | 43 | code: 'ERR_INVALID_STATE', | 39 | 44 | }); | 40 | 45 |
| | 46 | +// Buffer is not detached. | | 47 | +assert.strictEqual(certs.buffer.detached, false); | | 48 | + | 41 | 49 | assert.ok(endpoint[kState].isBound); | 42 | 50 | assert.ok(endpoint[kState].isReceiving); | 43 | 51 | assert.ok(endpoint[kState].isListening); | @@ -59,6 +67,9 @@ assert.ok(endpoint.destroyed); | 59 | 67 | await assert.rejects(listen(() => {}, { keys, certs, endpoint }), { | 60 | 68 | code: 'ERR_INVALID_STATE', | 61 | 69 | }); | | 70 | +// Buffer is not detached. | | 71 | +assert.strictEqual(certs.buffer.detached, false); | | 72 | + | 62 | 73 | assert.throws(() => { endpoint.busy = true; }, { | 63 | 74 | code: 'ERR_INVALID_STATE', | 64 | 75 | }); | |
|