Skip to content

Commit 1c73f43

Browse files
hsbtclaude
andcommitted
Harden the libfyaml backend after code review
Mark the reconstructed SyntaxError message UTF-8 instead of US-ASCII, so a diagnostic that embeds a multibyte snippet of the input does not raise Encoding::CompatibilityError when concatenated with UTF-8. Add RB_GC_GUARD for the anchor and tag strings in the emitter (matching the existing guard on the scalar value) so their C pointers cannot dangle if a GC runs inside fy_emit_event_create. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 67b6365 commit 1c73f43

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

ext/psych/psych_emitter_fy.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,8 @@ static VALUE scalar(VALUE self, VALUE value, VALUE anchor, VALUE tag,
313313

314314
do_emit(e, event);
315315
RB_GC_GUARD(value);
316+
RB_GC_GUARD(anchor);
317+
RB_GC_GUARD(tag);
316318
return self;
317319
}
318320

@@ -333,6 +335,8 @@ static VALUE start_sequence(VALUE self, VALUE anchor, VALUE tag,
333335
NIL_P(tag) ? NULL : StringValueCStr(tag));
334336

335337
do_emit(e, event);
338+
RB_GC_GUARD(anchor);
339+
RB_GC_GUARD(tag);
336340
return self;
337341
}
338342

@@ -362,6 +366,8 @@ static VALUE start_mapping(VALUE self, VALUE anchor, VALUE tag,
362366
NIL_P(tag) ? NULL : StringValueCStr(tag));
363367

364368
do_emit(e, event);
369+
RB_GC_GUARD(anchor);
370+
RB_GC_GUARD(tag);
365371
return self;
366372
}
367373

@@ -383,6 +389,7 @@ static VALUE alias(VALUE self, VALUE anchor)
383389

384390
do_emit(e, fy_emit_event_create(e->emit, FYET_ALIAS,
385391
NIL_P(anchor) ? NULL : StringValueCStr(anchor)));
392+
RB_GC_GUARD(anchor);
386393
return self;
387394
}
388395

ext/psych/psych_parser_fy.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,11 @@ static VALUE make_exception(psych_fy_parser_t *parser, VALUE path)
112112
void *iter = NULL;
113113
struct fy_diag_error *err = fy_diag_errors_iterate(diag, &iter);
114114
if (err) {
115-
if (err->msg) problem = rb_usascii_str_new2(err->msg);
115+
/* The message may embed a snippet of the (possibly multibyte)
116+
* input, so mark it UTF-8 rather than US-ASCII. */
117+
if (err->msg) {
118+
problem = rb_enc_str_new_cstr(err->msg, rb_utf8_encoding());
119+
}
116120
if (err->line >= 0) line = (size_t)err->line;
117121
if (err->column >= 0) column = (size_t)err->column;
118122
}

0 commit comments

Comments
 (0)