Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1048fb5

Browse files
committed
Issue #19739: Try to fix compiler warnings on 32-bit Windows.
1 parent 2cf4b0f commit 1048fb5

1 file changed

Lines changed: 21 additions & 22 deletions

File tree

Modules/_pickle.c

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -710,17 +710,15 @@ _Pickler_ClearBuffer(PicklerObject *self)
710710
static void
711711
_write_size64(char *out, size_t value)
712712
{
713-
out[0] = (unsigned char)(value & 0xff);
714-
out[1] = (unsigned char)((value >> 8) & 0xff);
715-
out[2] = (unsigned char)((value >> 16) & 0xff);
716-
out[3] = (unsigned char)((value >> 24) & 0xff);
717-
if (sizeof(size_t) >= 8) {
718-
out[4] = (unsigned char)((value >> 32) & 0xff);
719-
out[5] = (unsigned char)((value >> 40) & 0xff);
720-
out[6] = (unsigned char)((value >> 48) & 0xff);
721-
out[7] = (unsigned char)((value >> 56) & 0xff);
722-
} else {
723-
out[4] = out[5] = out[6] = out[7] = 0;
713+
int i;
714+
715+
assert(sizeof(size_t) <= 8);
716+
717+
for (i = 0; i < sizeof(size_t); i++) {
718+
out[i] = (unsigned char)((value >> (8 * i)) & 0xff);
719+
}
720+
for (i = sizeof(size_t); i < 8; i++) {
721+
out[i] = 0;
724722
}
725723
}
726724

@@ -1644,8 +1642,16 @@ save_long(PicklerObject *self, PyObject *obj)
16441642
}
16451643
else if (self->bin &&
16461644
(sizeof(long) <= 4 ||
1647-
(val <= 0x7fffffffL && val >= -0x80000000L))) {
1648-
/* result fits in a signed 4-byte integer */
1645+
(val <= 0x7fffffffL && val >= (-0x7fffffffL - 1)))) {
1646+
/* result fits in a signed 4-byte integer.
1647+
1648+
Note: we can't use -0x80000000L in the above condition because some
1649+
compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type
1650+
before applying the unary minus when sizeof(long) <= 4. The
1651+
resulting value stays unsigned which is commonly not what we want,
1652+
so MSVC happily warns us about it. However, that result would have
1653+
been fine because we guard for sizeof(long) <= 4 which turns the
1654+
condition true in that particular case. */
16491655
char pdata[32];
16501656
Py_ssize_t len = 0;
16511657

@@ -1904,11 +1910,8 @@ save_bytes(PicklerObject *self, PyObject *obj)
19041910
len = 5;
19051911
}
19061912
else if (self->proto >= 4) {
1907-
int i;
19081913
header[0] = BINBYTES8;
1909-
for (i = 0; i < 8; i++) {
1910-
_write_size64(header + 1, size);
1911-
}
1914+
_write_size64(header + 1, size);
19121915
len = 8;
19131916
}
19141917
else {
@@ -2017,12 +2020,8 @@ write_utf8(PicklerObject *self, char *data, Py_ssize_t size)
20172020
len = 5;
20182021
}
20192022
else if (self->proto >= 4) {
2020-
int i;
2021-
20222023
header[0] = BINUNICODE8;
2023-
for (i = 0; i < 8; i++) {
2024-
_write_size64(header + 1, size);
2025-
}
2024+
_write_size64(header + 1, size);
20262025
len = 9;
20272026
}
20282027
else {

0 commit comments

Comments
 (0)