Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3dac559

Browse files
committed
SF bug #442520: test_struct fails on SPARC.
The ob_sval member of a string object isn't necessarily aligned to better than a native long, so the new "q" and "Q" struct codes can't get away w/ casting tricks on platforms where LONG_LONG requires stricter-than-long alignment. After I thought of a few elaborate workarounds, Guido bashed me over the head with the obvious memcpy approach, herewith implemented.
1 parent cdab3bf commit 3dac559

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

Modules/structmodule.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -547,13 +547,19 @@ nu_ulong(const char *p, const formatdef *f)
547547
static PyObject *
548548
nu_longlong(const char *p, const formatdef *f)
549549
{
550-
return PyLong_FromLongLong(*(LONG_LONG *)p);
550+
/* p may not be properly aligned */
551+
LONG_LONG x;
552+
memcpy(&x, p, sizeof(LONG_LONG));
553+
return PyLong_FromLongLong(x);
551554
}
552555

553556
static PyObject *
554557
nu_ulonglong(const char *p, const formatdef *f)
555558
{
556-
return PyLong_FromUnsignedLongLong(*(unsigned LONG_LONG *)p);
559+
/* p may not be properly aligned */
560+
unsigned LONG_LONG x;
561+
memcpy(&x, p, sizeof(unsigned LONG_LONG));
562+
return PyLong_FromUnsignedLongLong(x);
557563
}
558564
#endif
559565

@@ -700,7 +706,7 @@ np_longlong(char *p, PyObject *v, const formatdef *f)
700706
LONG_LONG x;
701707
if (get_longlong(v, &x) < 0)
702708
return -1;
703-
* (LONG_LONG *)p = x;
709+
memcpy(p, &x, sizeof(LONG_LONG));
704710
return 0;
705711
}
706712

@@ -710,7 +716,7 @@ np_ulonglong(char *p, PyObject *v, const formatdef *f)
710716
unsigned LONG_LONG x;
711717
if (get_ulonglong(v, &x) < 0)
712718
return -1;
713-
* (unsigned LONG_LONG *)p = x;
719+
memcpy(p, &x, sizeof(unsigned LONG_LONG));
714720
return 0;
715721
}
716722
#endif

0 commit comments

Comments
 (0)