From 583c6e860c1f9eee81ebf09fd87e0f921e8c0dd2 Mon Sep 17 00:00:00 2001 From: Mark Dickinson Date: Mon, 29 Aug 2016 16:40:29 +0100 Subject: [PATCH] Issue #27214: Fix potential bug and remove useless optimization in long_invert. Thanks Oren Milman. --- Misc/NEWS | 4 ++++ Objects/longobject.c | 6 ++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS b/Misc/NEWS index 36cf5899016..32144d1311f 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,6 +10,10 @@ What's New in Python 3.6.0 beta 1 Core and Builtins ----------------- +- Issue #27214: In long_invert, be more careful about modifying object + returned by long_add, and remove an unnecessary check for small longs. + Thanks Oren Milman for analysis and patch. + - Issue #27506: Support passing the bytes/bytearray.translate() "delete" argument by keyword. diff --git a/Objects/longobject.c b/Objects/longobject.c index 38e707220a2..89b68626057 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -4170,8 +4170,10 @@ long_invert(PyLongObject *v) Py_DECREF(w); if (x == NULL) return NULL; - Py_SIZE(x) = -(Py_SIZE(x)); - return (PyObject *)maybe_small_long(x); + _PyLong_Negate(&x); + /* No need for maybe_small_long here, since any small + longs will have been caught in the Py_SIZE <= 1 fast path. */ + return (PyObject *)x; } static PyObject *