Skip to content

Commit 6aaf9f3

Browse files
authored
fix: Root HTML rewriter variables to ensure no GC (#1202)
1 parent 806dadd commit 6aaf9f3

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

runtime/fastly/builtins/html-rewriter.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,10 @@ bool HTMLRewritingStream::onElement(JSContext *cx, unsigned argc, JS::Value *vp)
353353

354354
struct OutputContextData {
355355
JSContext *cx;
356-
JSObject *self;
356+
JS::Heap<JSObject *> self;
357+
bool enqueue_failed;
358+
359+
OutputContextData(JSContext *cx, JSObject *self) : cx(cx), self(self), enqueue_failed(false) {}
357360
};
358361

359362
void HTMLRewritingStream::finalize(JS::GCContext *gcx, JSObject *self) {
@@ -387,7 +390,7 @@ void HTMLRewritingStream::finalize(JS::GCContext *gcx, JSObject *self) {
387390
static void output_callback(const char *chunk, size_t chunk_len, void *user_data) {
388391
auto *ctx = static_cast<OutputContextData *>(user_data);
389392
JSContext *cx = ctx->cx;
390-
JSObject *self = ctx->self;
393+
JS::RootedObject self(cx, ctx->self);
391394

392395
JS::RootedObject out_obj(cx, JS_NewUint8Array(cx, chunk_len));
393396
if (!out_obj) {
@@ -405,6 +408,7 @@ static void output_callback(const char *chunk, size_t chunk_len, void *user_data
405408
JS::RootedValue out_chunk(cx, JS::ObjectValue(*out_obj));
406409

407410
if (!TransformStreamDefaultController::Enqueue(cx, controller, out_chunk)) {
411+
ctx->enqueue_failed = true;
408412
return;
409413
}
410414
}
@@ -468,6 +472,13 @@ bool HTMLRewritingStream::transformAlgorithm(JSContext *cx, unsigned argc, JS::V
468472
return false;
469473
}
470474

475+
auto output_context = static_cast<OutputContextData *>(
476+
JS::GetReservedSlot(self, HTMLRewritingStream::Slots::OutputContext).toPrivate());
477+
if (output_context->enqueue_failed) {
478+
JS_ReportErrorASCII(cx, "Error processing HTML: output stream enqueue failed");
479+
return false;
480+
}
481+
471482
args.rval().setUndefined();
472483
return true;
473484
}

0 commit comments

Comments
 (0)