diff options
author | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2022-04-02 16:06:11 +0900 |
---|---|---|
committer | git <svn-admin@ruby-lang.org> | 2022-04-02 17:01:33 +0900 |
commit | 1cbdedec895070df1df96d05370cf8da084ab6fa (patch) | |
tree | 3f905cd687bdbbe8e5ce873a38984c363dfb3c8e /ext | |
parent | 729b9a85439d5cec7b73f8c0c41c4377e6ce304c (diff) |
[ruby/zlib] Mask checksums to lower 32bits
Upper bits affect the result of `crc32` in zlib 1.2.12.
https://github.com/ruby/zlib/commit/9ab6d04af1
Diffstat (limited to 'ext')
-rw-r--r-- | ext/zlib/zlib.c | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/ext/zlib/zlib.c b/ext/zlib/zlib.c index 5cd45c9b26..168b3d0a0a 100644 --- a/ext/zlib/zlib.c +++ b/ext/zlib/zlib.c @@ -374,18 +374,24 @@ rb_zlib_version(VALUE klass) return rb_str_new2(zlibVersion()); } +#if SIZEOF_LONG * CHAR_BIT > 32 +# define mask32(x) ((x) & 0xffffffff) +#else +# define mask32(x) (x) +#endif + #if SIZEOF_LONG > SIZEOF_INT static uLong checksum_long(uLong (*func)(uLong, const Bytef*, uInt), uLong sum, const Bytef *ptr, long len) { if (len > UINT_MAX) { do { - sum = func(sum, ptr, UINT_MAX); + sum = func(mask32(sum), ptr, UINT_MAX); ptr += UINT_MAX; len -= UINT_MAX; } while (len >= UINT_MAX); } - if (len > 0) sum = func(sum, ptr, (uInt)len); + if (len > 0) sum = func(mask32(sum), ptr, (uInt)len); return sum; } #else @@ -411,7 +417,7 @@ do_checksum(int argc, VALUE *argv, uLong (*func)(uLong, const Bytef*, uInt)) } if (NIL_P(str)) { - sum = func(sum, Z_NULL, 0); + sum = func(mask32(sum), Z_NULL, 0); } else if (rb_obj_is_kind_of(str, rb_cIO)) { VALUE buf; |