2525# define VALGRIND_MAKE_MEM_UNDEFINED (p , n ) 0
2626#endif
2727
28- #define RUBY_ZLIB_VERSION "3.2.1 "
28+ #define RUBY_ZLIB_VERSION "3.2.3 "
2929
3030#ifndef RB_PASS_CALLED_KEYWORDS
3131# define rb_class_new_instance_kw (argc , argv , klass , kw_splat ) rb_class_new_instance(argc, argv, klass)
@@ -860,9 +860,7 @@ zstream_buffer_ungets(struct zstream *z, const Bytef *b, unsigned long len)
860860 char * bufptr ;
861861 long filled ;
862862
863- if (NIL_P (z -> buf ) || (long )rb_str_capacity (z -> buf ) <= ZSTREAM_BUF_FILLED (z )) {
864- zstream_expand_buffer_into (z , len );
865- }
863+ zstream_expand_buffer_into (z , len );
866864
867865 RSTRING_GETMEM (z -> buf , bufptr , filled );
868866 memmove (bufptr + len , bufptr , filled );
@@ -1094,8 +1092,9 @@ zstream_run_func(struct zstream_run_args *args)
10941092 break ;
10951093 }
10961094
1097- if (err != Z_OK && err != Z_BUF_ERROR )
1095+ if (err != Z_OK && err != Z_BUF_ERROR ) {
10981096 break ;
1097+ }
10991098
11001099 if (z -> stream .avail_out > 0 ) {
11011100 z -> flags |= ZSTREAM_FLAG_IN_STREAM ;
@@ -1170,12 +1169,17 @@ zstream_run_try(VALUE value_arg)
11701169 /* retry if no exception is thrown */
11711170 if (err == Z_OK && args -> interrupt ) {
11721171 args -> interrupt = 0 ;
1173- goto loop ;
1172+
1173+ /* Retry only if both avail_in > 0 (more input to process) and avail_out > 0
1174+ * (output buffer has space). If avail_out == 0, the buffer is full and should
1175+ * be consumed by the caller first. If avail_in == 0, there's nothing more to process. */
1176+ if (z -> stream .avail_in > 0 && z -> stream .avail_out > 0 ) {
1177+ goto loop ;
1178+ }
11741179 }
11751180
1176- if (flush != Z_FINISH && err == Z_BUF_ERROR
1177- && z -> stream .avail_out > 0 ) {
1178- z -> flags |= ZSTREAM_FLAG_IN_STREAM ;
1181+ if (flush != Z_FINISH && err == Z_BUF_ERROR && z -> stream .avail_out > 0 ) {
1182+ z -> flags |= ZSTREAM_FLAG_IN_STREAM ;
11791183 }
11801184
11811185 zstream_reset_input (z );
@@ -1456,6 +1460,7 @@ rb_zstream_finish(VALUE obj)
14561460 * call-seq:
14571461 * flush_next_in -> input
14581462 *
1463+ * Flushes input buffer and returns all data in that buffer.
14591464 */
14601465static VALUE
14611466rb_zstream_flush_next_in (VALUE obj )
@@ -2444,17 +2449,16 @@ struct gzfile {
24442449
24452450#define GZFILE_READ_SIZE 2048
24462451
2452+ enum { read_raw_arg_len , read_raw_arg_buf , read_raw_arg__count };
24472453struct read_raw_arg {
24482454 VALUE io ;
2449- union {
2450- const VALUE argv [2 ]; /* for rb_funcallv */
2451- struct {
2452- VALUE len ;
2453- VALUE buf ;
2454- } in ;
2455- } as ;
2455+ const VALUE argv [read_raw_arg__count ]; /* for rb_funcallv */
24562456};
24572457
2458+ #define read_raw_arg_argc (ra ) \
2459+ ((int)read_raw_arg__count - NIL_P((ra)->argv[read_raw_arg__count - 1]))
2460+ #define read_raw_arg_init (io , len , buf ) { io, { len, buf } }
2461+
24582462static void
24592463gzfile_mark (void * p )
24602464{
@@ -2580,9 +2584,9 @@ gzfile_read_raw_partial(VALUE arg)
25802584{
25812585 struct read_raw_arg * ra = (struct read_raw_arg * )arg ;
25822586 VALUE str ;
2583- int argc = NIL_P (ra -> as . argv [ 1 ]) ? 1 : 2 ;
2587+ int argc = read_raw_arg_argc (ra ) ;
25842588
2585- str = rb_funcallv (ra -> io , id_readpartial , argc , ra -> as . argv );
2589+ str = rb_funcallv (ra -> io , id_readpartial , argc , ra -> argv );
25862590 Check_Type (str , T_STRING );
25872591 return str ;
25882592}
@@ -2593,8 +2597,8 @@ gzfile_read_raw_rescue(VALUE arg, VALUE _)
25932597 struct read_raw_arg * ra = (struct read_raw_arg * )arg ;
25942598 VALUE str = Qnil ;
25952599 if (rb_obj_is_kind_of (rb_errinfo (), rb_eNoMethodError )) {
2596- int argc = NIL_P (ra -> as . argv [ 1 ]) ? 1 : 2 ;
2597- str = rb_funcallv (ra -> io , id_read , argc , ra -> as . argv );
2600+ int argc = read_raw_arg_argc (ra ) ;
2601+ str = rb_funcallv (ra -> io , id_read , argc , ra -> argv );
25982602 if (!NIL_P (str )) {
25992603 Check_Type (str , T_STRING );
26002604 }
@@ -2605,11 +2609,8 @@ gzfile_read_raw_rescue(VALUE arg, VALUE _)
26052609static VALUE
26062610gzfile_read_raw (struct gzfile * gz , VALUE outbuf )
26072611{
2608- struct read_raw_arg ra ;
2609-
2610- ra .io = gz -> io ;
2611- ra .as .in .len = INT2FIX (GZFILE_READ_SIZE );
2612- ra .as .in .buf = outbuf ;
2612+ struct read_raw_arg ra =
2613+ read_raw_arg_init (gz -> io , INT2FIX (GZFILE_READ_SIZE ), outbuf );
26132614
26142615 return rb_rescue2 (gzfile_read_raw_partial , (VALUE )& ra ,
26152616 gzfile_read_raw_rescue , (VALUE )& ra ,
0 commit comments