summaryrefslogtreecommitdiffstats
path: root/Modules/_struct.c
diff options
context:
space:
mode:
authorXiang Zhang <angwerzx@126.com>2017-05-15 04:04:26 (GMT)
committerGitHub <noreply@github.com>2017-05-15 04:04:26 (GMT)
commit981096f98b9c131594b0ac85ad01b63cbd11aa0a (patch)
tree9442eb67424384cba025ca5f4c67569b5cf9bfcd /Modules/_struct.c
parent7c278a5eeb656c2b48a85bbd761ce165f1751bb6 (diff)
downloadcpython-981096f98b9c131594b0ac85ad01b63cbd11aa0a.zip
cpython-981096f98b9c131594b0ac85ad01b63cbd11aa0a.tar.gz
cpython-981096f98b9c131594b0ac85ad01b63cbd11aa0a.tar.bz2
bpo-30242: resolve some undefined behaviours in struct (#1418)
Diffstat (limited to 'Modules/_struct.c')
-rw-r--r--Modules/_struct.c14
1 files changed, 9 insertions, 5 deletions
diff --git a/Modules/_struct.c b/Modules/_struct.c
index bd4de8d..8e1e219 100644
--- a/Modules/_struct.c
+++ b/Modules/_struct.c
@@ -543,7 +543,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;
}
@@ -864,6 +864,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;
@@ -876,7 +877,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;
@@ -887,6 +888,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;
@@ -897,7 +899,7 @@ bp_uint(char *p, PyObject *v, const formatdef *f)
RANGE_ERROR(x, f, 1, maxint - 1);
}
do {
- p[--i] = (char)x;
+ q[--i] = (unsigned char)(x & 0xffUL);
x >>= 8;
} while (i > 0);
return 0;
@@ -1077,6 +1079,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;
@@ -1089,7 +1092,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;
@@ -1100,6 +1103,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;
@@ -1110,7 +1114,7 @@ lp_uint(char *p, PyObject *v, const formatdef *f)
RANGE_ERROR(x, f, 1, maxint - 1);
}
do {
- *p++ = (char)x;
+ *q++ = (unsigned char)(x & 0xffUL);
x >>= 8;
} while (--i > 0);
return 0;