Skip to content

Commit dbd7885

Browse files
github-actions[bot]GrabYourPitchforkscarlossanlop
authored
[release/8.0] Remove implicit narrowing conversions from zlib (#91962)
* Make zlib compile clean against MSVC C4244 and clang implicit-int-conversion Descriptions of each changes are in the respective .patch files. * Unbreak mono build --------- Co-authored-by: Levi Broderick <[email protected]> Co-authored-by: Carlos Sánchez López <[email protected]>
1 parent adc47a1 commit dbd7885

File tree

12 files changed

+150
-23
lines changed

12 files changed

+150
-23
lines changed

src/mono/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ if (MSVC)
1515
if(EXISTS ${CLR_SOURCELINK_FILE_PATH})
1616
add_link_options("/sourcelink:${CLR_SOURCELINK_FILE_PATH}")
1717
endif()
18+
19+
# FIXME: Remove the line below when https://github.com/dotnet/runtime/issues/91249 is fixed.
20+
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:/wd4244>) # conversion from 'type1' to 'type2', possible loss of data
1821
endif(MSVC)
1922

2023
set(CROSS_ROOTFS $ENV{ROOTFS_DIR})
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
From edabaf799fd071a328e0adb743a98628df6649f0 Mon Sep 17 00:00:00 2001
2+
From: Levi Broderick <[email protected]>
3+
Date: Mon, 28 Aug 2023 15:26:38 -0700
4+
Subject: [PATCH] Make zlib-intel compile clean against C4244 clang equivalent
5+
is "implicit-int-conversion" warning
6+
7+
The change to deflate.c is legal because 'len' has an upper bound of
8+
MAX_STORED, which means it fits cleanly into a 16-bit integer. So
9+
writing out 2x 8-bit values will not result in data loss.
10+
11+
The change to trees.c is legal because within this loop, 'count' is
12+
intended to have an upper bound of 138, with the target assignment
13+
only executing if 'count' is bounded by 4. Neither the 'count' local
14+
in isolation nor the addition that's part of the target line is
15+
expected to result in integer overflow. But even if it did, that's a
16+
matter for a different warning code and doesn't impact the correctness
17+
of the narrowing cast being considered here.
18+
19+
The change to slide_sse.c is legal because 'w_size' is limited to
20+
1 << 15 (see deflateInit2_ in deflate.c), so this fits cleanly into
21+
a 16-bit value.
22+
---
23+
src/native/external/zlib-intel/deflate.c | 8 ++++----
24+
src/native/external/zlib-intel/slide_sse.c | 2 +-
25+
src/native/external/zlib-intel/trees.c | 2 +-
26+
3 files changed, 6 insertions(+), 6 deletions(-)
27+
28+
diff --git a/src/native/external/zlib-intel/deflate.c b/src/native/external/zlib-intel/deflate.c
29+
index bd5e95774a6..108b1a187af 100644
30+
--- a/src/native/external/zlib-intel/deflate.c
31+
+++ b/src/native/external/zlib-intel/deflate.c
32+
@@ -1553,10 +1553,10 @@ local block_state deflate_stored(s, flush)
33+
_tr_stored_block(s, (char *)0, 0L, last);
34+
35+
/* Replace the lengths in the dummy stored block with len. */
36+
- s->pending_buf[s->pending - 4] = len;
37+
- s->pending_buf[s->pending - 3] = len >> 8;
38+
- s->pending_buf[s->pending - 2] = ~len;
39+
- s->pending_buf[s->pending - 1] = ~len >> 8;
40+
+ s->pending_buf[s->pending - 4] = (Bytef)len;
41+
+ s->pending_buf[s->pending - 3] = (Bytef)(len >> 8);
42+
+ s->pending_buf[s->pending - 2] = (Bytef)~len;
43+
+ s->pending_buf[s->pending - 1] = (Bytef)(~len >> 8);
44+
45+
/* Write the stored block header bytes. */
46+
flush_pending(s->strm);
47+
diff --git a/src/native/external/zlib-intel/slide_sse.c b/src/native/external/zlib-intel/slide_sse.c
48+
index 342fd562dd1..eb74202c5a0 100644
49+
--- a/src/native/external/zlib-intel/slide_sse.c
50+
+++ b/src/native/external/zlib-intel/slide_sse.c
51+
@@ -18,7 +18,7 @@ void slide_hash_sse(deflate_state *s)
52+
unsigned n;
53+
Posf *p;
54+
uInt wsize = s->w_size;
55+
- z_const __m128i xmm_wsize = _mm_set1_epi16(s->w_size);
56+
+ z_const __m128i xmm_wsize = _mm_set1_epi16((short)s->w_size);
57+
58+
n = s->hash_size;
59+
p = &s->head[n] - 8;
60+
diff --git a/src/native/external/zlib-intel/trees.c b/src/native/external/zlib-intel/trees.c
61+
index 35462a1313a..f78b7d8c63e 100644
62+
--- a/src/native/external/zlib-intel/trees.c
63+
+++ b/src/native/external/zlib-intel/trees.c
64+
@@ -717,7 +717,7 @@ local void scan_tree(s, tree, max_code)
65+
if (++count < max_count && curlen == nextlen) {
66+
continue;
67+
} else if (count < min_count) {
68+
- s->bl_tree[curlen].Freq += count;
69+
+ s->bl_tree[curlen].Freq += (ush)count;
70+
} else if (curlen != 0) {
71+
if (curlen != prevlen) s->bl_tree[curlen].Freq++;
72+
s->bl_tree[REP_3_6].Freq++;
73+
--
74+
2.42.0.windows.1
75+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
From 86d96652ddd60f61dc7b0c94b601f6d156d34632 Mon Sep 17 00:00:00 2001
2+
From: Levi Broderick <[email protected]>
3+
Date: Mon, 28 Aug 2023 15:26:38 -0700
4+
Subject: [PATCH] Make zlib compile clean against C4244 clang equivalent is
5+
"implicit-int-conversion" warning
6+
7+
The change to deflate.c is legal because 'len' has an upper bound of
8+
MAX_STORED, which means it fits cleanly into a 16-bit integer. So
9+
writing out 2x 8-bit values will not result in data loss.
10+
11+
The change to trees.c is legal because within this loop, 'count' is
12+
intended to have an upper bound of 138, with the target assignment
13+
only executing if 'count' is bounded by 4. Neither the 'count' local
14+
in isolation nor the addition that's part of the target line is
15+
expected to result in integer overflow. But even if it did, that's a
16+
matter for a different warning code and doesn't impact the correctness
17+
of the narrowing cast being considered here.
18+
---
19+
src/native/external/zlib/deflate.c | 8 ++++----
20+
src/native/external/zlib/trees.c | 2 +-
21+
2 files changed, 5 insertions(+), 5 deletions(-)
22+
23+
diff --git a/src/native/external/zlib/deflate.c b/src/native/external/zlib/deflate.c
24+
index d2e1106ef5d..b7636639754 100644
25+
--- a/src/native/external/zlib/deflate.c
26+
+++ b/src/native/external/zlib/deflate.c
27+
@@ -1738,10 +1738,10 @@ local block_state deflate_stored(s, flush)
28+
_tr_stored_block(s, (char *)0, 0L, last);
29+
30+
/* Replace the lengths in the dummy stored block with len. */
31+
- s->pending_buf[s->pending - 4] = len;
32+
- s->pending_buf[s->pending - 3] = len >> 8;
33+
- s->pending_buf[s->pending - 2] = ~len;
34+
- s->pending_buf[s->pending - 1] = ~len >> 8;
35+
+ s->pending_buf[s->pending - 4] = (Bytef)len;
36+
+ s->pending_buf[s->pending - 3] = (Bytef)(len >> 8);
37+
+ s->pending_buf[s->pending - 2] = (Bytef)~len;
38+
+ s->pending_buf[s->pending - 1] = (Bytef)(~len >> 8);
39+
40+
/* Write the stored block header bytes. */
41+
flush_pending(s->strm);
42+
diff --git a/src/native/external/zlib/trees.c b/src/native/external/zlib/trees.c
43+
index 5f305c47221..8a3eec559e5 100644
44+
--- a/src/native/external/zlib/trees.c
45+
+++ b/src/native/external/zlib/trees.c
46+
@@ -721,7 +721,7 @@ local void scan_tree(s, tree, max_code)
47+
if (++count < max_count && curlen == nextlen) {
48+
continue;
49+
} else if (count < min_count) {
50+
- s->bl_tree[curlen].Freq += count;
51+
+ s->bl_tree[curlen].Freq += (ush)count;
52+
} else if (curlen != 0) {
53+
if (curlen != prevlen) s->bl_tree[curlen].Freq++;
54+
s->bl_tree[REP_3_6].Freq++;
55+
--
56+
2.42.0.windows.1
57+

src/native/external/zlib-intel-version.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ copied into our repo in order to build. Since then, we've just updated only
1212
those files in-place, ignoring other files in the intel/zlib repo. If new files
1313
are introduced which are necessary for building the product, feel free to bring
1414
those down as well.
15+
16+
We have also applied the custom patches under the patches/zlib-intel folder.

src/native/external/zlib-intel.cmake

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
if(MSVC)
2-
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:/wd4244>) # conversion from 'type1' to 'type2', possible loss of data
3-
else(CMAKE_C_COMPILER_ID MATCHES "Clang")
4-
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:-Wno-implicit-int-conversion>)
5-
endif()
6-
71
set(ZLIB_SOURCES_BASE
82
adler32.c
93
compress.c

src/native/external/zlib-intel/deflate.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,10 +1553,10 @@ local block_state deflate_stored(s, flush)
15531553
_tr_stored_block(s, (char *)0, 0L, last);
15541554

15551555
/* Replace the lengths in the dummy stored block with len. */
1556-
s->pending_buf[s->pending - 4] = len;
1557-
s->pending_buf[s->pending - 3] = len >> 8;
1558-
s->pending_buf[s->pending - 2] = ~len;
1559-
s->pending_buf[s->pending - 1] = ~len >> 8;
1556+
s->pending_buf[s->pending - 4] = (Bytef)len;
1557+
s->pending_buf[s->pending - 3] = (Bytef)(len >> 8);
1558+
s->pending_buf[s->pending - 2] = (Bytef)~len;
1559+
s->pending_buf[s->pending - 1] = (Bytef)(~len >> 8);
15601560

15611561
/* Write the stored block header bytes. */
15621562
flush_pending(s->strm);

src/native/external/zlib-intel/slide_sse.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ void slide_hash_sse(deflate_state *s)
1818
unsigned n;
1919
Posf *p;
2020
uInt wsize = s->w_size;
21-
z_const __m128i xmm_wsize = _mm_set1_epi16(s->w_size);
21+
z_const __m128i xmm_wsize = _mm_set1_epi16((short)s->w_size);
2222

2323
n = s->hash_size;
2424
p = &s->head[n] - 8;

src/native/external/zlib-intel/trees.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ local void scan_tree(s, tree, max_code)
717717
if (++count < max_count && curlen == nextlen) {
718718
continue;
719719
} else if (count < min_count) {
720-
s->bl_tree[curlen].Freq += count;
720+
s->bl_tree[curlen].Freq += (ush)count;
721721
} else if (curlen != 0) {
722722
if (curlen != prevlen) s->bl_tree[curlen].Freq++;
723723
s->bl_tree[REP_3_6].Freq++;

src/native/external/zlib-version.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ We have also cherry-picked into our local copy:
1313
This patch only affects memLevel 9 compression. .NET doesn't currently use this
1414
memLevel, but we'll take this patch out of an abundance of caution just in case
1515
we enable this functionality in a future release.
16+
17+
We have also applied the custom patches under the patches/zlib folder.

src/native/external/zlib.cmake

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
if(MSVC)
2-
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:/wd4244>) # conversion from 'type1' to 'type2', possible loss of data
3-
else(CMAKE_C_COMPILER_ID MATCHES "Clang")
4-
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:-Wno-implicit-int-conversion>)
5-
endif()
6-
71
set(ZLIB_SOURCES_BASE
82
adler32.c
93
compress.c

0 commit comments

Comments
 (0)