diff options
| author | nagachika <nagachika@ruby-lang.org> | 2022-05-05 19:18:37 +0900 |
|---|---|---|
| committer | nagachika <nagachika@ruby-lang.org> | 2022-05-05 19:18:37 +0900 |
| commit | e87640cd9d3dfe2cad004224eb356ee406865c8a (patch) | |
| tree | f7fce5472896db925934bea98b25b717b0aa41da | |
| parent | 5a578f10aef8c0935090ea8d4e66c7ad3094fc18 (diff) | |
merge revision(s) 1cbdedec895070df1df96d05370cf8da084ab6fa:
[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
---
ext/zlib/zlib.c | 12 +++++++++---
test/zlib/test_zlib.rb | 2 ++
2 files changed, 11 insertions(+), 3 deletions(-)
| -rw-r--r-- | ext/zlib/zlib.c | 12 | ||||
| -rw-r--r-- | test/zlib/test_zlib.rb | 2 | ||||
| -rw-r--r-- | version.h | 4 |
3 files changed, 13 insertions, 5 deletions
diff --git a/ext/zlib/zlib.c b/ext/zlib/zlib.c index be5f148bcd..4b9e08d0c3 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; diff --git a/test/zlib/test_zlib.rb b/test/zlib/test_zlib.rb index 1e57c5aa8c..a57e1fcb26 100644 --- a/test/zlib/test_zlib.rb +++ b/test/zlib/test_zlib.rb @@ -1303,6 +1303,7 @@ if defined? Zlib assert_equal(0x02820145, Zlib.adler32("foo")) assert_equal(0x02820145, Zlib.adler32("o", Zlib.adler32("fo"))) assert_equal(0x8a62c964, Zlib.adler32("abc\x01\x02\x03" * 10000)) + assert_equal(0x97d1a9f7, Zlib.adler32("p", -305419897)) Tempfile.create("test_zlib_gzip_file_to_io") {|t| File.binwrite(t.path, "foo") t.rewind @@ -1338,6 +1339,7 @@ if defined? Zlib assert_equal(0x8c736521, Zlib.crc32("foo")) assert_equal(0x8c736521, Zlib.crc32("o", Zlib.crc32("fo"))) assert_equal(0x07f0d68f, Zlib.crc32("abc\x01\x02\x03" * 10000)) + assert_equal(0xf136439b, Zlib.crc32("p", -305419897)) Tempfile.create("test_zlib_gzip_file_to_io") {|t| File.binwrite(t.path, "foo") t.rewind @@ -11,11 +11,11 @@ # define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR #define RUBY_VERSION_TEENY 3 #define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR -#define RUBY_PATCHLEVEL 29 +#define RUBY_PATCHLEVEL 30 #define RUBY_RELEASE_YEAR 2022 #define RUBY_RELEASE_MONTH 5 -#define RUBY_RELEASE_DAY 3 +#define RUBY_RELEASE_DAY 5 #include "ruby/version.h" |
