8000 gh-104432: Use `memcpy()` to avoid misaligned loads by chrstphrchvz · Pull Request #104433 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-104432: Use memcpy() to avoid misaligned loads #104433

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 4 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to 10000
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
gh-104432: Use memcpy() to avoid misaligned loads
  • Loading branch information
chrstphrchvz authored May 12, 2023
commit 382eb9dcf2326b8d9fbd29486fa5cb95a7b98d40
9 changes: 7 additions & 2 deletions Modules/grpmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,13 @@ mkgrent(PyObject *module, struct group *p)
Py_DECREF(v);
return NULL;
}
for (member = p->gr_mem; *member != NULL; member++) {
PyObject *x = PyUnicode_DecodeFSDefault(*member);
for (member = p->gr_mem; ; member++) {
char *s;
memcpy(&s, member, sizeof(s)); // member can be misaligned
if (s == NULL) {
break;
}
PyObject *x = PyUnicode_DecodeFSDefault(s);
if (x == NULL || PyList_Append(w, x) != 0) {
Py_XDECREF(x);
Py_DECREF(w);
Expand Down
20 changes: 15 additions & 5 deletions Modules/socketmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -5787,9 +5787,14 @@ 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 *s;
memcpy(&s, pch, sizeof(s)); // pch can be misaligned
if (s == NULL) {
break;
}
tmp = PyUnicode_FromString(s);
if (tmp == NULL)
goto err;

Expand All @@ -5801,8 +5806,13 @@ 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 *s;
memcpy(&s, pch, sizeof(s)); // pch can be misaligned
if (s == NULL) {
break;
}

switch (af) {

Expand All @@ -5814,7 +5824,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, s, sizeof(sin.sin_addr));
tmp = make_ipv4_addr(&sin);

if (pch == h->h_addr_list && alen >= sizeof(sin))
Expand All @@ -5831,7 +5841,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, s, sizeof(sin6.sin6_addr));
tmp = make_ipv6_addr(&sin6);

if (pch == h->h_addr_list && alen >= sizeof(sin6))
Expand Down
0