From adf30005eace7ff3770c971235a49566180afbc3 Mon Sep 17 00:00:00 2001 From: Hiroshi SHIBATA Date: Wed, 7 Oct 2026 16:11:49 +0900 Subject: [PATCH] Use rb_len_t for the lengths left in long These hold String and Array lengths or offsets into them, so they would truncate on mswin once rb_len_t widens. A build with rb_len_t set to long long reported them. Co-Authored-By: Claude Opus 5.5 --- compile.c | 4 ++-- enumerator.c | 52 +++++++++++++++++++++--------------------- internal/ruby_parser.h | 2 +- io.c | 2 +- marshal.c | 2 +- parse.y | 2 +- ruby_parser.c | 2 +- win32/file.c | 3 ++- 8 files changed, 35 insertions(+), 34 deletions(-) diff --git a/compile.c b/compile.c index 6bb31404345..fb1bc8b5369 100644 --- a/compile.c +++ b/compile.c @@ -12946,11 +12946,11 @@ ibf_dump_write_byte(struct ibf_dump *dump, unsigned char byte) } static void -ibf_dump_overwrite(struct ibf_dump *dump, void *buff, unsigned int size, long offset) +ibf_dump_overwrite(struct ibf_dump *dump, void *buff, unsigned int size, rb_len_t offset) { VALUE str = dump->current_buffer->str; char *ptr = RSTRING_PTR(str); - if ((unsigned long)(size + offset) > (unsigned long)RSTRING_LEN(str)) + if ((rb_ulen_t)(size + offset) > (rb_ulen_t)RSTRING_LEN(str)) rb_bug("ibf_dump_overwrite: overflow"); memcpy(ptr + offset, buff, size); } diff --git a/enumerator.c b/enumerator.c index d69eb99b98c..8aac292369c 100644 --- a/enumerator.c +++ b/enumerator.c @@ -240,7 +240,7 @@ struct producer { VALUE size; }; -typedef struct MEMO *lazyenum_proc_func(VALUE, struct MEMO *, VALUE, long); +typedef struct MEMO *lazyenum_proc_func(VALUE, struct MEMO *, VALUE, rb_len_t); typedef VALUE lazyenum_size_func(VALUE, VALUE); typedef int lazyenum_precheck_func(VALUE proc_entry); typedef struct { @@ -1631,7 +1631,7 @@ lazy_init_block_i(RB_BLOCK_CALL_FUNC_ARGLIST(val, m)) rb_raise(rb_eArgError, "tried to call lazy " #func " without a block"); \ } -static VALUE lazy_yielder_result(struct MEMO *result, VALUE yielder, VALUE procs_array, VALUE memos, long i); +static VALUE lazy_yielder_result(struct MEMO *result, VALUE yielder, VALUE procs_array, VALUE memos, rb_len_t i); static VALUE lazy_init_yielder(RB_BLOCK_CALL_FUNC_ARGLIST(_, m)) @@ -1647,7 +1647,7 @@ lazy_init_yielder(RB_BLOCK_CALL_FUNC_ARGLIST(_, m)) } static VALUE -lazy_yielder_yield(struct MEMO *result, long memo_index, int argc, const VALUE *argv) +lazy_yielder_yield(struct MEMO *result, rb_len_t memo_index, int argc, const VALUE *argv) { VALUE m = result->v1; VALUE yielder = RARRAY_AREF(m, 0); @@ -1662,7 +1662,7 @@ lazy_yielder_yield(struct MEMO *result, long memo_index, int argc, const VALUE * } static VALUE -lazy_yielder_result(struct MEMO *result, VALUE yielder, VALUE procs_array, VALUE memos, long i) +lazy_yielder_result(struct MEMO *result, VALUE yielder, VALUE procs_array, VALUE memos, rb_len_t i) { int cont = 1; @@ -2058,7 +2058,7 @@ lazyenum_yield_values(VALUE proc_entry, struct MEMO *result) } static struct MEMO * -lazy_map_proc(VALUE proc_entry, struct MEMO *result, VALUE memos, long memo_index) +lazy_map_proc(VALUE proc_entry, struct MEMO *result, VALUE memos, rb_len_t memo_index) { VALUE value = lazyenum_yield_values(proc_entry, result); LAZY_MEMO_SET_VALUE(result, value); @@ -2105,11 +2105,11 @@ lazy_flat_map_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, y)) } static struct MEMO * -lazy_flat_map_proc(VALUE proc_entry, struct MEMO *result, VALUE memos, long memo_index) +lazy_flat_map_proc(VALUE proc_entry, struct MEMO *result, VALUE memos, rb_len_t memo_index) { VALUE value = lazyenum_yield_values(proc_entry, result); VALUE ary = 0; - const long proc_index = memo_index + 1; + const rb_len_t proc_index = memo_index + 1; int break_p = LAZY_MEMO_BREAK_P(result); if (RB_TYPE_P(value, T_ARRAY)) { @@ -2175,7 +2175,7 @@ lazy_flat_map(VALUE obj) } static struct MEMO * -lazy_select_proc(VALUE proc_entry, struct MEMO *result, VALUE memos, long memo_index) +lazy_select_proc(VALUE proc_entry, struct MEMO *result, VALUE memos, rb_len_t memo_index) { VALUE chain = lazyenum_yield(proc_entry, result); if (!RTEST(chain)) return 0; @@ -2202,7 +2202,7 @@ lazy_select(VALUE obj) } static struct MEMO * -lazy_filter_map_proc(VALUE proc_entry, struct MEMO *result, VALUE memos, long memo_index) +lazy_filter_map_proc(VALUE proc_entry, struct MEMO *result, VALUE memos, rb_len_t memo_index) { VALUE value = lazyenum_yield_values(proc_entry, result); if (!RTEST(value)) return 0; @@ -2233,7 +2233,7 @@ lazy_filter_map(VALUE obj) } static struct MEMO * -lazy_reject_proc(VALUE proc_entry, struct MEMO *result, VALUE memos, long memo_index) +lazy_reject_proc(VALUE proc_entry, struct MEMO *result, VALUE memos, rb_len_t memo_index) { VALUE chain = lazyenum_yield(proc_entry, result); if (RTEST(chain)) return 0; @@ -2259,7 +2259,7 @@ lazy_reject(VALUE obj) } static struct MEMO * -lazy_grep_proc(VALUE proc_entry, struct MEMO *result, VALUE memos, long memo_index) +lazy_grep_proc(VALUE proc_entry, struct MEMO *result, VALUE memos, rb_len_t memo_index) { struct proc_entry *entry = proc_entry_ptr(proc_entry); VALUE chain = rb_funcall(entry->memo, id_eqq, 1, result->memo_value); @@ -2268,7 +2268,7 @@ lazy_grep_proc(VALUE proc_entry, struct MEMO *result, VALUE memos, long memo_ind } static struct MEMO * -lazy_grep_iter_proc(VALUE proc_entry, struct MEMO *result, VALUE memos, long memo_index) +lazy_grep_iter_proc(VALUE proc_entry, struct MEMO *result, VALUE memos, rb_len_t memo_index) { struct proc_entry *entry = proc_entry_ptr(proc_entry); VALUE value, chain = rb_funcall(entry->memo, id_eqq, 1, result->memo_value); @@ -2308,7 +2308,7 @@ lazy_grep(VALUE obj, VALUE pattern) } static struct MEMO * -lazy_grep_v_proc(VALUE proc_entry, struct MEMO *result, VALUE memos, long memo_index) +lazy_grep_v_proc(VALUE proc_entry, struct MEMO *result, VALUE memos, rb_len_t memo_index) { struct proc_entry *entry = proc_entry_ptr(proc_entry); VALUE chain = rb_funcall(entry->memo, id_eqq, 1, result->memo_value); @@ -2317,7 +2317,7 @@ lazy_grep_v_proc(VALUE proc_entry, struct MEMO *result, VALUE memos, long memo_i } static struct MEMO * -lazy_grep_v_iter_proc(VALUE proc_entry, struct MEMO *result, VALUE memos, long memo_index) +lazy_grep_v_iter_proc(VALUE proc_entry, struct MEMO *result, VALUE memos, rb_len_t memo_index) { struct proc_entry *entry = proc_entry_ptr(proc_entry); VALUE value, chain = rb_funcall(entry->memo, id_eqq, 1, result->memo_value); @@ -2368,7 +2368,7 @@ next_stopped(VALUE obj, VALUE _) } static struct MEMO * -lazy_zip_arrays_func(VALUE proc_entry, struct MEMO *result, VALUE memos, long memo_index) +lazy_zip_arrays_func(VALUE proc_entry, struct MEMO *result, VALUE memos, rb_len_t memo_index) { struct proc_entry *entry = proc_entry_ptr(proc_entry); VALUE ary, arrays = entry->memo; @@ -2386,7 +2386,7 @@ lazy_zip_arrays_func(VALUE proc_entry, struct MEMO *result, VALUE memos, long me } static struct MEMO * -lazy_zip_func(VALUE proc_entry, struct MEMO *result, VALUE memos, long memo_index) +lazy_zip_func(VALUE proc_entry, struct MEMO *result, VALUE memos, rb_len_t memo_index) { struct proc_entry *entry = proc_entry_ptr(proc_entry); VALUE arg = rb_ary_entry(memos, memo_index); @@ -2458,7 +2458,7 @@ lazy_zip(int argc, VALUE *argv, VALUE obj) } static struct MEMO * -lazy_take_proc(VALUE proc_entry, struct MEMO *result, VALUE memos, long memo_index) +lazy_take_proc(VALUE proc_entry, struct MEMO *result, VALUE memos, rb_len_t memo_index) { rb_len_t remain; struct proc_entry *entry = proc_entry_ptr(proc_entry); @@ -2516,7 +2516,7 @@ lazy_take(VALUE obj, VALUE n) } static struct MEMO * -lazy_take_while_proc(VALUE proc_entry, struct MEMO *result, VALUE memos, long memo_index) +lazy_take_while_proc(VALUE proc_entry, struct MEMO *result, VALUE memos, rb_len_t memo_index) { VALUE take = lazyenum_yield_values(proc_entry, result); if (!RTEST(take)) { @@ -2558,7 +2558,7 @@ lazy_drop_size(VALUE proc_entry, VALUE receiver) } static struct MEMO * -lazy_drop_proc(VALUE proc_entry, struct MEMO *result, VALUE memos, long memo_index) +lazy_drop_proc(VALUE proc_entry, struct MEMO *result, VALUE memos, rb_len_t memo_index) { rb_len_t remain; struct proc_entry *entry = proc_entry_ptr(proc_entry); @@ -2604,7 +2604,7 @@ lazy_drop(VALUE obj, VALUE n) } static struct MEMO * -lazy_drop_while_proc(VALUE proc_entry, struct MEMO* result, VALUE memos, long memo_index) +lazy_drop_while_proc(VALUE proc_entry, struct MEMO* result, VALUE memos, rb_len_t memo_index) { struct proc_entry *entry = proc_entry_ptr(proc_entry); VALUE memo = rb_ary_entry(memos, memo_index); @@ -2640,7 +2640,7 @@ lazy_drop_while(VALUE obj) } static int -lazy_uniq_check(VALUE chain, VALUE memos, long memo_index) +lazy_uniq_check(VALUE chain, VALUE memos, rb_len_t memo_index) { VALUE set = rb_ary_entry(memos, memo_index); @@ -2653,14 +2653,14 @@ lazy_uniq_check(VALUE chain, VALUE memos, long memo_index) } static struct MEMO * -lazy_uniq_proc(VALUE proc_entry, struct MEMO *result, VALUE memos, long memo_index) +lazy_uniq_proc(VALUE proc_entry, struct MEMO *result, VALUE memos, rb_len_t memo_index) { if (lazy_uniq_check(result->memo_value, memos, memo_index)) return 0; return result; } static struct MEMO * -lazy_uniq_iter_proc(VALUE proc_entry, struct MEMO *result, VALUE memos, long memo_index) +lazy_uniq_iter_proc(VALUE proc_entry, struct MEMO *result, VALUE memos, rb_len_t memo_index) { VALUE chain = lazyenum_yield(proc_entry, result); @@ -2693,7 +2693,7 @@ lazy_uniq(VALUE obj) } static struct MEMO * -lazy_compact_proc(VALUE proc_entry, struct MEMO *result, VALUE memos, long memo_index) +lazy_compact_proc(VALUE proc_entry, struct MEMO *result, VALUE memos, rb_len_t memo_index) { if (NIL_P(result->memo_value)) return 0; return result; @@ -2717,7 +2717,7 @@ lazy_compact(VALUE obj) } static struct MEMO * -lazy_with_index_proc(VALUE proc_entry, struct MEMO* result, VALUE memos, long memo_index) +lazy_with_index_proc(VALUE proc_entry, struct MEMO* result, VALUE memos, rb_len_t memo_index) { struct proc_entry *entry = proc_entry_ptr(proc_entry); VALUE memo = rb_ary_entry(memos, memo_index); @@ -2803,7 +2803,7 @@ lazy_with_index(int argc, VALUE *argv, VALUE obj) } static struct MEMO * -lazy_tap_each_proc(VALUE proc_entry, struct MEMO *result, VALUE memos, long memo_index) +lazy_tap_each_proc(VALUE proc_entry, struct MEMO *result, VALUE memos, rb_len_t memo_index) { struct proc_entry *entry = proc_entry_ptr(proc_entry); diff --git a/internal/ruby_parser.h b/internal/ruby_parser.h index efa2e41e0f7..65b48ab4f3b 100644 --- a/internal/ruby_parser.h +++ b/internal/ruby_parser.h @@ -12,7 +12,7 @@ struct lex_pointer_string { VALUE str; - long ptr; + rb_len_t ptr; }; RUBY_SYMBOL_EXPORT_BEGIN diff --git a/io.c b/io.c index 15c2bf7a1a0..6146be9f2f8 100644 --- a/io.c +++ b/io.c @@ -983,7 +983,7 @@ io_ungetbyte(VALUE str, rb_io_t *fptr) static void io_restore_read_buffer(VALUE str, rb_io_t *fptr) { - long len = RSTRING_LEN(str); + rb_len_t len = RSTRING_LEN(str); if (len > INT_MAX - fptr->rbuf.len) { rb_raise(rb_eIOError, "read buffer too large"); diff --git a/marshal.c b/marshal.c index 15f9a91350b..69c4865d113 100644 --- a/marshal.c +++ b/marshal.c @@ -325,7 +325,7 @@ w_bytes(const char *s, rb_len_t n, struct dump_arg *arg) static void w_str_bytes(VALUE str, struct dump_arg *arg) { - long len = RSTRING_LEN(str); + rb_len_t len = RSTRING_LEN(str); const char *ptr = RSTRING_PTR(str); w_long(len, arg); if (RSTRING_PTR(str) != ptr || RSTRING_LEN(str) != len) { diff --git a/parse.y b/parse.y index 545cf17b18e..fc5786badf0 100644 --- a/parse.y +++ b/parse.y @@ -732,7 +732,7 @@ after_pop_stack(int len, struct parser_params *p) static inline int char_at_end(struct parser_params *p, VALUE str, int when_empty) { - long len = RSTRING_LEN(str); + rb_len_t len = RSTRING_LEN(str); return len > 0 ? (unsigned char)RSTRING_PTR(str)[len-1] : when_empty; } #endif diff --git a/ruby_parser.c b/ruby_parser.c index 4f690ef3f4d..b732b060248 100644 --- a/ruby_parser.c +++ b/ruby_parser.c @@ -605,7 +605,7 @@ rb_parser_string_t * rb_parser_lex_get_str(struct parser_params *p, struct lex_pointer_string *ptr_str) { char *beg, *end, *start; - long len; + rb_len_t len; VALUE s = ptr_str->str; beg = RSTRING_PTR(s); diff --git a/win32/file.c b/win32/file.c index f961c09ecb8..e74a2202a12 100644 --- a/win32/file.c +++ b/win32/file.c @@ -236,7 +236,8 @@ user_length_in_path(const wchar_t *wuser, size_t len) static VALUE append_wstr(VALUE dst, const WCHAR *ws, ssize_t len, UINT cp, rb_encoding *enc) { - long olen, nlen = (long)len; + rb_len_t olen; + long nlen = (long)len; if (cp != INVALID_CODE_PAGE) { if (len == -1) len = lstrlenW(ws);