mirror of
https://github.com/python/cpython.git
synced 2026-01-04 14:32:21 +00:00
Issue #15144: Fix possible integer overflow when handling pointers as integer values, by using Py_uintptr_t instead of size_t.
Patch by Serhiy Storchaka.
This commit is contained in:
parent
1c47222a25
commit
ca8aa4acf6
11 changed files with 44 additions and 45 deletions
|
|
@ -2,9 +2,6 @@
|
|||
|
||||
#if STRINGLIB_IS_UNICODE
|
||||
|
||||
/* Mask to check or force alignment of a pointer to C 'long' boundaries */
|
||||
#define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1)
|
||||
|
||||
/* Mask to quickly check whether a C 'long' contains a
|
||||
non-ASCII, UTF8-encoded char. */
|
||||
#if (SIZEOF_LONG == 8)
|
||||
|
|
@ -25,7 +22,7 @@ STRINGLIB(utf8_decode)(const char **inptr, const char *end,
|
|||
{
|
||||
Py_UCS4 ch;
|
||||
const char *s = *inptr;
|
||||
const char *aligned_end = (const char *) ((size_t) end & ~LONG_PTR_MASK);
|
||||
const char *aligned_end = (const char *) _Py_ALIGN_DOWN(end, SIZEOF_LONG);
|
||||
STRINGLIB_CHAR *p = dest + *outpos;
|
||||
|
||||
while (s < end) {
|
||||
|
|
@ -39,7 +36,7 @@ STRINGLIB(utf8_decode)(const char **inptr, const char *end,
|
|||
First, check if we can do an aligned read, as most CPUs have
|
||||
a penalty for unaligned reads.
|
||||
*/
|
||||
if (!((size_t) s & LONG_PTR_MASK)) {
|
||||
if (_Py_IS_ALIGNED(s, SIZEOF_LONG)) {
|
||||
/* Help register allocation */
|
||||
register const char *_s = s;
|
||||
register STRINGLIB_CHAR *_p = p;
|
||||
|
|
@ -453,7 +450,7 @@ STRINGLIB(utf16_decode)(const unsigned char **inptr, const unsigned char *e,
|
|||
{
|
||||
Py_UCS4 ch;
|
||||
const unsigned char *aligned_end =
|
||||
(const unsigned char *) ((size_t) e & ~LONG_PTR_MASK);
|
||||
(const unsigned char *) _Py_ALIGN_DOWN(e, SIZEOF_LONG);
|
||||
const unsigned char *q = *inptr;
|
||||
STRINGLIB_CHAR *p = dest + *outpos;
|
||||
/* Offsets from q for retrieving byte pairs in the right order. */
|
||||
|
|
@ -468,7 +465,7 @@ STRINGLIB(utf16_decode)(const unsigned char **inptr, const unsigned char *e,
|
|||
Py_UCS4 ch2;
|
||||
/* First check for possible aligned read of a C 'long'. Unaligned
|
||||
reads are more expensive, better to defer to another iteration. */
|
||||
if (!((size_t) q & LONG_PTR_MASK)) {
|
||||
if (_Py_IS_ALIGNED(q, SIZEOF_LONG)) {
|
||||
/* Fast path for runs of in-range non-surrogate chars. */
|
||||
register const unsigned char *_q = q;
|
||||
while (_q < aligned_end) {
|
||||
|
|
@ -565,7 +562,6 @@ STRINGLIB(utf16_decode)(const unsigned char **inptr, const unsigned char *e,
|
|||
#undef FAST_CHAR_MASK
|
||||
#undef STRIPPED_MASK
|
||||
#undef SWAB
|
||||
#undef LONG_PTR_MASK
|
||||
|
||||
|
||||
Py_LOCAL_INLINE(void)
|
||||
|
|
@ -588,7 +584,7 @@ STRINGLIB(utf16_encode)(unsigned short *out,
|
|||
_PyUnicode_CONVERT_BYTES(STRINGLIB_CHAR, unsigned short, in, end, out);
|
||||
# endif
|
||||
} else {
|
||||
const STRINGLIB_CHAR *unrolled_end = in + (len & ~ (Py_ssize_t) 3);
|
||||
const STRINGLIB_CHAR *unrolled_end = in + _Py_SIZE_ROUND_DOWN(len, 4);
|
||||
while (in < unrolled_end) {
|
||||
out[0] = SWAB2(in[0]);
|
||||
out[1] = SWAB2(in[1]);
|
||||
|
|
|
|||
|
|
@ -43,8 +43,7 @@ STRINGLIB(fastsearch_memchr_1char)(const STRINGLIB_CHAR* s, Py_ssize_t n,
|
|||
|
||||
#define DO_MEMCHR(memchr, s, needle, nchars) do { \
|
||||
candidate = memchr((const void *) (s), (needle), (nchars) * sizeof(STRINGLIB_CHAR)); \
|
||||
found = (const STRINGLIB_CHAR *) \
|
||||
((Py_ssize_t) candidate & (~ ((Py_ssize_t) sizeof(STRINGLIB_CHAR) - 1))); \
|
||||
found = (const STRINGLIB_CHAR *) _Py_ALIGN_DOWN(candidate, sizeof(STRINGLIB_CHAR)); \
|
||||
} while (0)
|
||||
|
||||
if (mode == FAST_SEARCH) {
|
||||
|
|
|
|||
|
|
@ -2,9 +2,6 @@
|
|||
|
||||
#if STRINGLIB_IS_UNICODE
|
||||
|
||||
/* Mask to check or force alignment of a pointer to C 'long' boundaries */
|
||||
#define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1)
|
||||
|
||||
/* Mask to quickly check whether a C 'long' contains a
|
||||
non-ASCII, UTF8-encoded char. */
|
||||
#if (SIZEOF_LONG == 8)
|
||||
|
|
@ -21,10 +18,11 @@ Py_LOCAL_INLINE(Py_UCS4)
|
|||
STRINGLIB(find_max_char)(const STRINGLIB_CHAR *begin, const STRINGLIB_CHAR *end)
|
||||
{
|
||||
const unsigned char *p = (const unsigned char *) begin;
|
||||
const unsigned char *aligned_end = (const unsigned char *) ((size_t) end & ~LONG_PTR_MASK);
|
||||
const unsigned char *aligned_end =
|
||||
(const unsigned char *) _Py_ALIGN_DOWN(end, SIZEOF_LONG);
|
||||
|
||||
while (p < end) {
|
||||
if (!((size_t) p & LONG_PTR_MASK)) {
|
||||
if (_Py_IS_ALIGNED(p, SIZEOF_LONG)) {
|
||||
/* Help register allocation */
|
||||
register const unsigned char *_p = p;
|
||||
while (_p < aligned_end) {
|
||||
|
|
@ -43,7 +41,6 @@ STRINGLIB(find_max_char)(const STRINGLIB_CHAR *begin, const STRINGLIB_CHAR *end)
|
|||
return 127;
|
||||
}
|
||||
|
||||
#undef LONG_PTR_MASK
|
||||
#undef ASCII_CHAR_MASK
|
||||
|
||||
#else /* STRINGLIB_SIZEOF_CHAR == 1 */
|
||||
|
|
@ -72,7 +69,7 @@ STRINGLIB(find_max_char)(const STRINGLIB_CHAR *begin, const STRINGLIB_CHAR *end)
|
|||
register Py_UCS4 mask;
|
||||
Py_ssize_t n = end - begin;
|
||||
const STRINGLIB_CHAR *p = begin;
|
||||
const STRINGLIB_CHAR *unrolled_end = begin + (n & ~ (Py_ssize_t) 3);
|
||||
const STRINGLIB_CHAR *unrolled_end = begin + _Py_SIZE_ROUND_DOWN(n, 4);
|
||||
Py_UCS4 max_char;
|
||||
|
||||
max_char = MAX_CHAR_ASCII;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue