From 86f4b865339e375cc21d23e183a9fc67f9101006 Mon Sep 17 00:00:00 2001 From: Hiroshi SHIBATA Date: Sat, 10 Oct 2026 03:55:53 +0900 Subject: [PATCH 1/2] Use rb_len_t for the length of tag directives in the emitters It falls back to long on Ruby versions that do not define HAVE_RB_LEN_T, so the change also builds there. Co-Authored-By: Claude Opus 5.5 --- ext/psych/psych.h | 4 ++++ ext/psych/psych_emitter.c | 4 ++-- ext/psych/psych_emitter_fy.c | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/ext/psych/psych.h b/ext/psych/psych.h index 0e146588..3548ece3 100644 --- a/ext/psych/psych.h +++ b/ext/psych/psych.h @@ -4,6 +4,10 @@ #include #include +#ifndef HAVE_RB_LEN_T +typedef long rb_len_t; +#endif + #ifdef PSYCH_USE_LIBFYAML #include #else diff --git a/ext/psych/psych_emitter.c b/ext/psych/psych_emitter.c index 187aebc3..eed397b8 100644 --- a/ext/psych/psych_emitter.c +++ b/ext/psych/psych_emitter.c @@ -172,8 +172,8 @@ static VALUE start_document_try(VALUE d) } if(RTEST(tags)) { - long i = 0; - long len; + rb_len_t i = 0; + rb_len_t len; rb_encoding * encoding = rb_utf8_encoding(); Check_Type(tags, T_ARRAY); diff --git a/ext/psych/psych_emitter_fy.c b/ext/psych/psych_emitter_fy.c index 22bf23e5..5b101eb4 100644 --- a/ext/psych/psych_emitter_fy.c +++ b/ext/psych/psych_emitter_fy.c @@ -186,7 +186,7 @@ static VALUE start_document_try(VALUE d) if (RTEST(tags)) { rb_encoding *encoding = rb_utf8_encoding(); - long i, len; + rb_len_t i, len; Check_Type(tags, T_ARRAY); len = RARRAY_LEN(tags); if (len > 0) { From 2502de5a1bf3d055a9812fe9a77cb7252f7c2579 Mon Sep 17 00:00:00 2001 From: Hiroshi SHIBATA Date: Sat, 10 Oct 2026 04:03:32 +0900 Subject: [PATCH 2/2] Cast String lengths to rb_len_t instead of long A cast to long would cut these lengths to 32 bits before they reach rb_str_new on mswin once rb_len_t widens. Co-Authored-By: Claude Opus 5.5 --- ext/psych/psych_emitter.c | 2 +- ext/psych/psych_emitter_fy.c | 2 +- ext/psych/psych_parser.c | 2 +- ext/psych/psych_parser_fy.c | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ext/psych/psych_emitter.c b/ext/psych/psych_emitter.c index eed397b8..afe273d6 100644 --- a/ext/psych/psych_emitter.c +++ b/ext/psych/psych_emitter.c @@ -25,7 +25,7 @@ static void emit(yaml_emitter_t * emitter, yaml_event_t * event) static int writer(void *ctx, unsigned char *buffer, size_t size) { VALUE self = (VALUE)ctx, io = rb_attr_get(self, id_io); - VALUE str = rb_enc_str_new((const char *)buffer, (long)size, rb_utf8_encoding()); + VALUE str = rb_enc_str_new((const char *)buffer, (rb_len_t)size, rb_utf8_encoding()); VALUE wrote = rb_funcall(io, id_write, 1, str); return (int)NUM2INT(wrote); } diff --git a/ext/psych/psych_emitter_fy.c b/ext/psych/psych_emitter_fy.c index 5b101eb4..4834d9f7 100644 --- a/ext/psych/psych_emitter_fy.c +++ b/ext/psych/psych_emitter_fy.c @@ -33,7 +33,7 @@ static int emitter_output(struct fy_emitter *emit, enum fy_emitter_write_type ty { VALUE self = (VALUE)userdata; VALUE io = rb_attr_get(self, id_io); - VALUE s = rb_enc_str_new(str, (long)len, rb_utf8_encoding()); + VALUE s = rb_enc_str_new(str, (rb_len_t)len, rb_utf8_encoding()); rb_funcall(io, id_write, 1, s); return len; } diff --git a/ext/psych/psych_parser.c b/ext/psych/psych_parser.c index 2b50fcae..bb37623f 100644 --- a/ext/psych/psych_parser.c +++ b/ext/psych/psych_parser.c @@ -419,7 +419,7 @@ static VALUE parse_body(VALUE ptr) VALUE plain_implicit, quoted_implicit, style; VALUE val = rb_str_new( (const char *)event.data.scalar.value, - (long)event.data.scalar.length + (rb_len_t)event.data.scalar.length ); PSYCH_TRANSCODE(val, encoding, internal_enc); diff --git a/ext/psych/psych_parser_fy.c b/ext/psych/psych_parser_fy.c index ccb05776..e79ec360 100644 --- a/ext/psych/psych_parser_fy.c +++ b/ext/psych/psych_parser_fy.c @@ -256,7 +256,7 @@ static VALUE token_to_str(struct fy_token *tok, int encoding, rb_encoding *inter if (!text) { return Qnil; } - VALUE str = rb_str_new(text, (long)len); + VALUE str = rb_str_new(text, (rb_len_t)len); PSYCH_TRANSCODE(str, encoding, internal_enc); return str; }