Skip to content

Commit c882105

Browse files
TartanLlamazkat
andauthored
fix: Allocation failure checks (#1457)
Co-authored-by: Kat Marchán <kzm@zkat.tech>
1 parent 9b77977 commit c882105

2 files changed

Lines changed: 9 additions & 0 deletions

File tree

runtime/fastly/builtins/edge-rate-limiter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,9 @@ bool RateCounter::constructor(JSContext *cx, unsigned argc, JS::Value *vp) {
303303
return false;
304304
}
305305
JS::RootedString name_str(cx, JS_NewStringCopyN(cx, name.begin(), name.len));
306+
if (!name_str) {
307+
return false;
308+
}
306309
JS::SetReservedSlot(instance, static_cast<uint32_t>(Slots::Name), JS::StringValue(name_str));
307310
args.rval().setObject(*instance);
308311
return true;

runtime/fastly/builtins/fastly.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,9 @@ JS::Result<std::tuple<JS::UniqueChars, size_t>> convertBodyInit(JSContext *cx,
10191019
if (bodyObj && JS_IsArrayBufferViewObject(bodyObj)) {
10201020
length = JS_GetArrayBufferViewByteLength(bodyObj);
10211021
buf.reset(reinterpret_cast<char *>(JS_malloc(cx, length)));
1022+
if (!buf) {
1023+
return JS::Result<std::tuple<JS::UniqueChars, size_t>>(JS::Error());
1024+
}
10221025
// `maybeNoGC` needs to be populated for the lifetime of `buf` because
10231026
// short typed arrays have inline data which can move on GC, so assert
10241027
// that no GC happens. (Which it doesn't, because we're not allocating
@@ -1040,6 +1043,9 @@ JS::Result<std::tuple<JS::UniqueChars, size_t>> convertBodyInit(JSContext *cx,
10401043
} else if (bodyObj && URLSearchParams::is_instance(bodyObj)) {
10411044
jsurl::SpecSlice slice = URLSearchParams::serialize(cx, bodyObj);
10421045
buf.reset(reinterpret_cast<char *>(JS_malloc(cx, slice.len)));
1046+
if (!buf) {
1047+
return JS::Result<std::tuple<JS::UniqueChars, size_t>>(JS::Error());
1048+
}
10431049
std::memcpy(buf.get(), slice.data, slice.len);
10441050
length = slice.len;
10451051
return JS::Result<std::tuple<JS::UniqueChars, size_t>>(std::make_tuple(std::move(buf), length));

0 commit comments

Comments
 (0)