Kaydet (Commit) aad1caf5 authored tarafından Xiang Zhang's avatar Xiang Zhang Kaydeden (comit) GitHub

bpo-30242: resolve some undefined behaviours in struct (#1418) (#1586)

üst 0ce1f7e0
...@@ -539,7 +539,7 @@ np_ubyte(char *p, PyObject *v, const formatdef *f) ...@@ -539,7 +539,7 @@ np_ubyte(char *p, PyObject *v, const formatdef *f)
"ubyte format requires 0 <= number <= 255"); "ubyte format requires 0 <= number <= 255");
return -1; return -1;
} }
*p = (char)x; *(unsigned char *)p = (unsigned char)x;
return 0; return 0;
} }
...@@ -868,6 +868,7 @@ bp_int(char *p, PyObject *v, const formatdef *f) ...@@ -868,6 +868,7 @@ bp_int(char *p, PyObject *v, const formatdef *f)
{ {
long x; long x;
Py_ssize_t i; Py_ssize_t i;
unsigned char *q = (unsigned char *)p;
if (get_long(v, &x) < 0) if (get_long(v, &x) < 0)
return -1; return -1;
i = f->size; i = f->size;
...@@ -880,7 +881,7 @@ bp_int(char *p, PyObject *v, const formatdef *f) ...@@ -880,7 +881,7 @@ bp_int(char *p, PyObject *v, const formatdef *f)
#endif #endif
} }
do { do {
p[--i] = (char)x; q[--i] = (unsigned char)(x & 0xffL);
x >>= 8; x >>= 8;
} while (i > 0); } while (i > 0);
return 0; return 0;
...@@ -891,6 +892,7 @@ bp_uint(char *p, PyObject *v, const formatdef *f) ...@@ -891,6 +892,7 @@ bp_uint(char *p, PyObject *v, const formatdef *f)
{ {
unsigned long x; unsigned long x;
Py_ssize_t i; Py_ssize_t i;
unsigned char *q = (unsigned char *)p;
if (get_ulong(v, &x) < 0) if (get_ulong(v, &x) < 0)
return -1; return -1;
i = f->size; i = f->size;
...@@ -901,7 +903,7 @@ bp_uint(char *p, PyObject *v, const formatdef *f) ...@@ -901,7 +903,7 @@ bp_uint(char *p, PyObject *v, const formatdef *f)
RANGE_ERROR(x, f, 1, maxint - 1); RANGE_ERROR(x, f, 1, maxint - 1);
} }
do { do {
p[--i] = (char)x; q[--i] = (unsigned char)(x & 0xffUL);
x >>= 8; x >>= 8;
} while (i > 0); } while (i > 0);
return 0; return 0;
...@@ -1087,6 +1089,7 @@ lp_int(char *p, PyObject *v, const formatdef *f) ...@@ -1087,6 +1089,7 @@ lp_int(char *p, PyObject *v, const formatdef *f)
{ {
long x; long x;
Py_ssize_t i; Py_ssize_t i;
unsigned char *q = (unsigned char *)p;
if (get_long(v, &x) < 0) if (get_long(v, &x) < 0)
return -1; return -1;
i = f->size; i = f->size;
...@@ -1099,7 +1102,7 @@ lp_int(char *p, PyObject *v, const formatdef *f) ...@@ -1099,7 +1102,7 @@ lp_int(char *p, PyObject *v, const formatdef *f)
#endif #endif
} }
do { do {
*p++ = (char)x; *q++ = (unsigned char)(x & 0xffL);
x >>= 8; x >>= 8;
} while (--i > 0); } while (--i > 0);
return 0; return 0;
...@@ -1110,6 +1113,7 @@ lp_uint(char *p, PyObject *v, const formatdef *f) ...@@ -1110,6 +1113,7 @@ lp_uint(char *p, PyObject *v, const formatdef *f)
{ {
unsigned long x; unsigned long x;
Py_ssize_t i; Py_ssize_t i;
unsigned char *q = (unsigned char *)p;
if (get_ulong(v, &x) < 0) if (get_ulong(v, &x) < 0)
return -1; return -1;
i = f->size; i = f->size;
...@@ -1120,7 +1124,7 @@ lp_uint(char *p, PyObject *v, const formatdef *f) ...@@ -1120,7 +1124,7 @@ lp_uint(char *p, PyObject *v, const formatdef *f)
RANGE_ERROR(x, f, 1, maxint - 1); RANGE_ERROR(x, f, 1, maxint - 1);
} }
do { do {
*p++ = (char)x; *q++ = (unsigned char)(x & 0xffUL);
x >>= 8; x >>= 8;
} while (--i > 0); } while (--i > 0);
return 0; return 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment