New submission from Oren Milman: ------------ the proposed changes ------------ I believe the following checks are redundant: 1. in Objects/longobject.c in long_add: In case both a and b are negative, their absolute values are added using x_add, with the result stored in z. If (z != NULL), it must be that x_add succeeded, and also it must be that (Py_SIZE(z) > 0), as it is guaranteed that the absolute values of a and b are both bigger than zero. Thus, the check (Py_SIZE(z) != 0) here is redundant. 2. in Objects/longobject.c in long_sub: In case a is negative, the absolute values of a and b are subtracted or added using x_sub or x_add, with the result stored in z. Later on, if (z != NULL && Py_SIZE(z) != 0), then Py_SIZE(z) is negated. However, even though it might be that Py_SIZE(z) == 0, it doesn't really matter. doing 'Py_SIZE(z) = -(Py_SIZE(z));' in that case would do nothing. Thus, the check (Py_SIZE(z) != 0) here is redundant.
The original versions of both of these checks were added in revision 443 (November 1991!). Back then, ob_size's was implemented using one's complement, and negating it was actually doing 'z->ob_size = ~z->ob_size;'. Of course, in that case the check (z->ob_size != 0) was necessary, but then, in revision 590, ob_size was changed to use two's complement, and the check (z->ob_size != 0) was left untouched, and remained there to this day. ------------ diff ------------ The patches diff is attached. ------------ tests ------------ I built the patched CPython for x86, and played with it a little. Everything seemed to work as usual. In addition, I ran 'python -m test' (on my 64-bit Windows 10) before and after applying the patch, and got quite the same output. the outputs of both runs are attached. ---------- components: Interpreter Core files: issue.diff keywords: patch messages: 265989 nosy: Oren Milman priority: normal severity: normal status: open title: redundant checks in long_add and long_sub Added file: http://bugs.python.org/file42920/issue.diff _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue27073> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com