Skip to content

Commit 8b45c66

Browse files
committed
address review: use buffer to prevent incomplete changes
1 parent 8380c08 commit 8b45c66

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

Modules/arraymodule.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -643,9 +643,9 @@ d_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
643643
static PyObject *
644644
ce_getitem(arrayobject *ap, Py_ssize_t i)
645645
{
646-
double real = PyFloat_Unpack2(ap->ob_item + 2*sizeof(short)*i,
646+
double real = PyFloat_Unpack2(ap->ob_item + sizeof(short)*2*i,
647647
PY_LITTLE_ENDIAN);
648-
double imag = PyFloat_Unpack2(ap->ob_item + 2*sizeof(short)*i + sizeof(short),
648+
double imag = PyFloat_Unpack2(ap->ob_item + sizeof(short)*(2*i + 1),
649649
PY_LITTLE_ENDIAN);
650650

651651
return PyComplex_FromDoubles(real, imag);
@@ -664,13 +664,17 @@ ce_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
664664
CHECK_ARRAY_BOUNDS(ap, i);
665665

666666
if (i >= 0) {
667-
ret = PyFloat_Pack2(x.real, ap->ob_item + 2*sizeof(short)*i,
668-
PY_LITTLE_ENDIAN);
667+
char val[4];
668+
669+
ret = PyFloat_Pack2(x.real, val, PY_LITTLE_ENDIAN);
669670
if (ret) {
670671
return ret;
671672
}
672-
return PyFloat_Pack2(x.imag, ap->ob_item + 2*sizeof(short)*i + sizeof(short),
673-
PY_LITTLE_ENDIAN);
673+
ret = PyFloat_Pack2(x.imag, val + 2, PY_LITTLE_ENDIAN);
674+
if (!ret) {
675+
memcpy(ap->ob_item + sizeof(short)*2*i, val, 2*sizeof(short));
676+
}
677+
return ret;
674678
}
675679
return 0;
676680
}

0 commit comments

Comments
 (0)