Skip to content

Commit d698ea0

Browse files
hsbtclaude
andcommitted
Clamp lengths to uInt by the width of rb_len_t
MAX_UINT and checksum_long compared SIZEOF_LONG, so on mswin they would cast a length to uInt instead of clamping it once rb_len_t widens. The unsigned lengths taken by zstream_expand_buffer_into and zstream_buffer_ungets become rb_ulen_t so that they reach the clamp unnarrowed. Nothing changes while rb_len_t is long. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 4a731d3 commit d698ea0

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

‎ext/zlib/zlib.c‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#ifndef HAVE_RB_LEN_T
1616
typedef long rb_len_t;
17+
typedef unsigned long rb_ulen_t;
18+
# define SIZEOF_RB_LEN_T SIZEOF_LONG
1719
# define NUM2LEN NUM2LONG
1820
# define PRIdLEN "ld"
1921
#endif
@@ -58,9 +60,9 @@ typedef uLong (*checksum_func)(uLong, const Bytef*, z_size_t);
5860
typedef uLong (*checksum_func)(uLong, const Bytef*, uInt);
5961
#endif
6062

61-
#if SIZEOF_LONG > SIZEOF_INT
63+
#if SIZEOF_RB_LEN_T > SIZEOF_INT
6264
static inline uInt
63-
max_uint(long n)
65+
max_uint(rb_len_t n)
6466
{
6567
if (n > UINT_MAX) n = UINT_MAX;
6668
return (uInt)n;
@@ -92,12 +94,12 @@ struct zstream_funcs;
9294
struct zstream_run_args;
9395
static void zstream_init(struct zstream*, const struct zstream_funcs*);
9496
static void zstream_expand_buffer(struct zstream*);
95-
static void zstream_expand_buffer_into(struct zstream*, unsigned long);
97+
static void zstream_expand_buffer_into(struct zstream*, rb_ulen_t);
9698
static int zstream_expand_buffer_non_stream(struct zstream *z);
9799
static void zstream_append_buffer(struct zstream*, const Bytef*, rb_len_t);
98100
static VALUE zstream_detach_buffer(struct zstream*);
99101
static VALUE zstream_shift_buffer(struct zstream*, rb_len_t, VALUE);
100-
static void zstream_buffer_ungets(struct zstream*, const Bytef*, unsigned long);
102+
static void zstream_buffer_ungets(struct zstream*, const Bytef*, rb_ulen_t);
101103
static void zstream_buffer_ungetbyte(struct zstream*, int);
102104
static void zstream_append_input(struct zstream*, const Bytef*, rb_len_t);
103105
static void zstream_discard_input(struct zstream*, rb_len_t);
@@ -394,7 +396,7 @@ rb_zlib_version(VALUE klass)
394396
# define mask32(x) (x)
395397
#endif
396398

397-
#if SIZEOF_LONG > SIZEOF_INT && !defined(HAVE_ZLIB_SIZE_T_FUNCS)
399+
#if SIZEOF_RB_LEN_T > SIZEOF_INT && !defined(HAVE_ZLIB_SIZE_T_FUNCS)
398400
static uLong
399401
checksum_long(uLong (*func)(uLong, const Bytef*, uInt), uLong sum, const Bytef *ptr, rb_len_t len)
400402
{
@@ -708,7 +710,7 @@ zstream_expand_buffer(struct zstream *z)
708710
}
709711

710712
static void
711-
zstream_expand_buffer_into(struct zstream *z, unsigned long size)
713+
zstream_expand_buffer_into(struct zstream *z, rb_ulen_t size)
712714
{
713715
if (NIL_P(z->buf)) {
714716
/* I uses rb_str_new here not rb_str_buf_new because
@@ -862,7 +864,7 @@ zstream_shift_buffer(struct zstream *z, rb_len_t len, VALUE dst)
862864
}
863865

864866
static void
865-
zstream_buffer_ungets(struct zstream *z, const Bytef *b, unsigned long len)
867+
zstream_buffer_ungets(struct zstream *z, const Bytef *b, rb_ulen_t len)
866868
{
867869
char *bufptr;
868870
rb_len_t filled;

0 commit comments

Comments
 (0)