summaryrefslogtreecommitdiffstats
path: root/Objects/intobject.c
diff options
context:
space:
mode:
authorMartin v. Löwis <martin@v.loewis.de>2006-10-04 05:47:34 (GMT)
committerMartin v. Löwis <martin@v.loewis.de>2006-10-04 05:47:34 (GMT)
commit820d6ac9d7c2743e6804914471159a1ca74cbd41 (patch)
tree9fd0052d3b6b9a183a029df061b67d569f820384 /Objects/intobject.c
parent82271f13e7eab69b909d538556e4781e971f7584 (diff)
downloadcpython-820d6ac9d7c2743e6804914471159a1ca74cbd41.zip
cpython-820d6ac9d7c2743e6804914471159a1ca74cbd41.tar.gz
cpython-820d6ac9d7c2743e6804914471159a1ca74cbd41.tar.bz2
Fix integer negation and absolute value to not rely
on undefined behaviour of the C compiler anymore. Will backport to 2.5 and 2.4.
Diffstat (limited to 'Objects/intobject.c')
-rw-r--r--Objects/intobject.c7
1 files changed, 3 insertions, 4 deletions
diff --git a/Objects/intobject.c b/Objects/intobject.c
index cbca495..4e1f04f 100644
--- a/Objects/intobject.c
+++ b/Objects/intobject.c
@@ -754,10 +754,9 @@ int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
static PyObject *
int_neg(PyIntObject *v)
{
- register long a, x;
+ register long a;
a = v->ob_ival;
- x = -a;
- if (a < 0 && x < 0) {
+ if (a < 0 && (unsigned long)a == 0-(unsigned long)a) {
PyObject *o = PyLong_FromLong(a);
if (o != NULL) {
PyObject *result = PyNumber_Negative(o);
@@ -766,7 +765,7 @@ int_neg(PyIntObject *v)
}
return NULL;
}
- return PyInt_FromLong(x);
+ return PyInt_FromLong(-a);
}
static PyObject *