mirror of
https://github.com/python/cpython.git
synced 2025-08-30 21:48:47 +00:00
gh-104432: Use memcpy()
to avoid misaligned loads (#104433)
Fix potential unaligned memory access on C APIs involving returned sequences of `char *` pointers within the :mod:`grp` and :mod:`socket` modules. These were revealed using a ``-fsaniziter=alignment`` build on ARM macOS.
This commit is contained in:
parent
983305268e
commit
f01e4cedba
3 changed files with 29 additions and 7 deletions
|
@ -5779,9 +5779,15 @@ gethost_common(socket_state *state, struct hostent *h, struct sockaddr *addr,
|
|||
|
||||
/* SF #1511317: h_aliases can be NULL */
|
||||
if (h->h_aliases) {
|
||||
for (pch = h->h_aliases; *pch != NULL; pch++) {
|
||||
for (pch = h->h_aliases; ; pch++) {
|
||||
int status;
|
||||
tmp = PyUnicode_FromString(*pch);
|
||||
char *host_alias;
|
||||
// pch can be misaligned
|
||||
memcpy(&host_alias, pch, sizeof(host_alias));
|
||||
if (host_alias == NULL) {
|
||||
break;
|
||||
}
|
||||
tmp = PyUnicode_FromString(host_alias);
|
||||
if (tmp == NULL)
|
||||
goto err;
|
||||
|
||||
|
@ -5793,8 +5799,14 @@ gethost_common(socket_state *state, struct hostent *h, struct sockaddr *addr,
|
|||
}
|
||||
}
|
||||
|
||||
for (pch = h->h_addr_list; *pch != NULL; pch++) {
|
||||
for (pch = h->h_addr_list; ; pch++) {
|
||||
int status;
|
||||
char *host_address;
|
||||
// pch can be misaligned
|
||||
memcpy(&host_address, pch, sizeof(host_address));
|
||||
if (host_address == NULL) {
|
||||
break;
|
||||
}
|
||||
|
||||
switch (af) {
|
||||
|
||||
|
@ -5806,7 +5818,7 @@ gethost_common(socket_state *state, struct hostent *h, struct sockaddr *addr,
|
|||
#ifdef HAVE_SOCKADDR_SA_LEN
|
||||
sin.sin_len = sizeof(sin);
|
||||
#endif
|
||||
memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr));
|
||||
memcpy(&sin.sin_addr, host_address, sizeof(sin.sin_addr));
|
||||
tmp = make_ipv4_addr(&sin);
|
||||
|
||||
if (pch == h->h_addr_list && alen >= sizeof(sin))
|
||||
|
@ -5823,7 +5835,7 @@ gethost_common(socket_state *state, struct hostent *h, struct sockaddr *addr,
|
|||
#ifdef HAVE_SOCKADDR_SA_LEN
|
||||
sin6.sin6_len = sizeof(sin6);
|
||||
#endif
|
||||
memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr));
|
||||
memcpy(&sin6.sin6_addr, host_address, sizeof(sin6.sin6_addr));
|
||||
tmp = make_ipv6_addr(&sin6);
|
||||
|
||||
if (pch == h->h_addr_list && alen >= sizeof(sin6))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue