Commit 982a17e0 authored by Xiang Zhang's avatar Xiang Zhang Committed by GitHub

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

parent bd82a070
......@@ -506,7 +506,7 @@ np_ubyte(char *p, PyObject *v, const formatdef *f)
"ubyte format requires 0 <= number <= 255");
return -1;
}
*p = (char)x;
*(unsigned char *)p = (unsigned char)x;
return 0;
}
......@@ -814,6 +814,7 @@ bp_int(char *p, PyObject *v, const formatdef *f)
{
long x;
Py_ssize_t i;
unsigned char *q = (unsigned char *)p;
if (get_long(v, &x) < 0)
return -1;
i = f->size;
......@@ -826,7 +827,7 @@ bp_int(char *p, PyObject *v, const formatdef *f)
#endif
}
do {
p[--i] = (char)x;
q[--i] = (unsigned char)(x & 0xffL);
x >>= 8;
} while (i > 0);
return 0;
......@@ -837,6 +838,7 @@ bp_uint(char *p, PyObject *v, const formatdef *f)
{
unsigned long x;
Py_ssize_t i;
unsigned char *q = (unsigned char *)p;
if (get_ulong(v, &x) < 0)
return -1;
i = f->size;
......@@ -847,7 +849,7 @@ bp_uint(char *p, PyObject *v, const formatdef *f)
return _range_error(f, 1);
}
do {
p[--i] = (char)x;
q[--i] = (unsigned char)(x & 0xffUL);
x >>= 8;
} while (i > 0);
return 0;
......@@ -1034,6 +1036,7 @@ lp_int(char *p, PyObject *v, const formatdef *f)
{
long x;
Py_ssize_t i;
unsigned char *q = (unsigned char *)p;
if (get_long(v, &x) < 0)
return -1;
i = f->size;
......@@ -1046,7 +1049,7 @@ lp_int(char *p, PyObject *v, const formatdef *f)
#endif
}
do {
*p++ = (char)x;
*q++ = (unsigned char)(x & 0xffL);
x >>= 8;
} while (--i > 0);
return 0;
......@@ -1057,6 +1060,7 @@ lp_uint(char *p, PyObject *v, const formatdef *f)
{
unsigned long x;
Py_ssize_t i;
unsigned char *q = (unsigned char *)p;
if (get_ulong(v, &x) < 0)
return -1;
i = f->size;
......@@ -1067,7 +1071,7 @@ lp_uint(char *p, PyObject *v, const formatdef *f)
return _range_error(f, 1);
}
do {
*p++ = (char)x;
*q++ = (unsigned char)(x & 0xffUL);
x >>= 8;
} while (--i > 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