Skip to content

Commit c175b71

Browse files
authored
Cleanup minor pointer arithmetic issue (#4495)
1 parent b10809c commit c175b71

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/workerd/jsg/buffersource-test.c++

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,29 @@ struct BufferSourceContext: public jsg::Object, public jsg::ContextGlobal {
5555
return BufferSource(js, BackingStore::alloc<v8::ArrayBuffer>(js, 3));
5656
}
5757

58+
bool doTest(jsg::Lock& js, jsg::BufferSource buf) {
59+
buf.asArrayPtr()[0] = 1;
60+
buf.asArrayPtr()[1] = 2;
61+
buf.asArrayPtr()[2] = 3;
62+
buf.asArrayPtr()[3] = 4;
63+
buf.asArrayPtr()[4] = 5;
64+
buf.asArrayPtr()[5] = 6;
65+
buf.asArrayPtr()[6] = 7;
66+
buf.asArrayPtr()[7] = 8;
67+
68+
auto ptr = buf.asArrayPtr<uint32_t>();
69+
KJ_ASSERT(ptr.size() == 2);
70+
KJ_ASSERT(ptr[0] == 0x04030201);
71+
KJ_ASSERT(ptr[1] == 0x08070605);
72+
return true;
73+
}
74+
5875
JSG_RESOURCE_TYPE(BufferSourceContext) {
5976
JSG_METHOD(takeBufferSource);
6077
JSG_METHOD(takeUint8Array);
6178
JSG_METHOD(makeBufferSource);
6279
JSG_METHOD(makeArrayBuffer);
80+
JSG_METHOD(doTest);
6381
}
6482
};
6583
JSG_DECLARE_ISOLATE_TYPE(BufferSourceIsolate, BufferSourceContext);
@@ -99,6 +117,8 @@ KJ_TEST("BufferSource works") {
99117
"u8.byteLength === 0 && u2.byteLength === 1 && u2 instanceof Uint8Array && "
100118
"u2.buffer.byteLength === 4 && u2.byteOffset === 1 && u8 !== u2",
101119
"boolean", "true");
120+
121+
e.expectEval("const buf = new Uint8Array(12); doTest(buf.subarray(4))", "boolean", "true");
102122
}
103123

104124
} // namespace

src/workerd/jsg/buffersource.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ class BackingStore {
130130
inline kj::ArrayPtr<T> asArrayPtr() KJ_LIFETIMEBOUND {
131131
KJ_ASSERT(backingStore != nullptr, "Invalid access after move.");
132132
KJ_ASSERT(byteLength % sizeof(T) == 0);
133-
return kj::ArrayPtr<T>(
134-
static_cast<T*>(backingStore->Data()) + byteOffset, byteLength / sizeof(T));
133+
kj::byte* data = static_cast<kj::byte*>(backingStore->Data());
134+
return kj::ArrayPtr<T>(reinterpret_cast<T*>(data + byteOffset), byteLength / sizeof(T));
135135
}
136136

137137
template <typename T = kj::byte>

0 commit comments

Comments
 (0)