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

This commit is contained in:
Xiang Zhang 2017-05-15 12:04:26 +08:00 committed by GitHub
parent 7c278a5eeb
commit 981096f98b
1 changed files with 9 additions and 5 deletions

View File

@ -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;