8000 gh-126024: fix UBSan failure in `unicodeobject.c:find_first_nonascii` by picnixz · Pull Request #127566 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-126024: fix UBSan failure in unicodeobject.c:find_first_nonascii #127566

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 6, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -5018,7 +5018,8 @@ ctz(size_t v)
#define HAVE_CTZ 0
#endif

#if HAVE_CTZ && PY_LITTLE_ENDIAN
#if PY_LITTLE_ENDIAN
# if HAVE_CTZ
// load p[0]..p[size-1] as a size_t without unaligned access nor read ahead.
static size_t
load_unaligned(const unsigned char *p, size_t size)
Expand Down Expand Up @@ -5064,7 +5065,21 @@ load_unaligned(const unsigned char *p, size_t size)
}
return u.s;
}
#endif
# endif
# if defined(_M_AMD64) || defined(_M_IX86) || defined(__x86_64__) || defined(__i386__)
// x86 and amd64 are little endian and can load unaligned memory.
# if defined(__clang__)
static inline size_t
__attribute__((no_sanitize("alignment")))
load_unaligned_x86_amd64(const unsigned char *p)
{
return *(const size_t *)p;
}
# else
# define load_unaligned_x86_amd64(p) *(const size_t *)p
# endif
# endif
#endif // PY_LITTLE_ENDIAN

/*
* Find the first non-ASCII character in a byte sequence.
Expand All @@ -5084,8 +5099,7 @@ find_first_nonascii(const unsigned char *start, const unsigned char *end)
#if PY_LITTLE_ENDIAN && HAVE_CTZ
if (p < p2) {
#if defined(_M_AMD64) || defined(_M_IX86) || defined(__x86_64__) || defined(__i386__)
// x86 and amd64 are little endian and can load unaligned memory.
size_t u = *(const size_t*)p & ASCII_CHAR_MASK;
size_t u = load_unaligned_x86_amd64(p) & ASCII_CHAR_MASK;
#else
size_t u = load_unaligned(p, p2 - p) & ASCII_CHAR_MASK;
#endif
Expand Down
Loading
0