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

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

üst 7c278a5e
...@@ -543,7 +543,7 @@ np_ubyte(char *p, PyObject *v, const formatdef *f) ...@@ -543,7 +543,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;
} }
...@@ -864,6 +864,7 @@ bp_int(char *p, PyObject *v, const formatdef *f) ...@@ -864,6 +864,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;
...@@ -876,7 +877,7 @@ bp_int(char *p, PyObject *v, const formatdef *f) ...@@ -876,7 +877,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;
...@@ -887,6 +888,7 @@ bp_uint(char *p, PyObject *v, const formatdef *f) ...@@ -887,6 +888,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;
...@@ -897,7 +899,7 @@ bp_uint(char *p, PyObject *v, const formatdef *f) ...@@ -897,7 +899,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;
...@@ -1077,6 +1079,7 @@ lp_int(char *p, PyObject *v, const formatdef *f) ...@@ -1077,6 +1079,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;
...@@ -1089,7 +1092,7 @@ lp_int(char *p, PyObject *v, const formatdef *f) ...@@ -1089,7 +1092,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;
...@@ -1100,6 +1103,7 @@ lp_uint(char *p, PyObject *v, const formatdef *f) ...@@ -1100,6 +1103,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;
...@@ -1110,7 +1114,7 @@ lp_uint(char *p, PyObject *v, const formatdef *f) ...@@ -1110,7 +1114,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