This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Use Py_CLEAR to safe clear attributes
Type: crash Stage: resolved
Components: Extension Modules, Interpreter Core Versions: Python 3.3, Python 3.4, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: asvetlov, benjamin.peterson, georg.brandl, larry, pitrou, python-dev, serhiy.storchaka, skrah, vstinner
Priority: normal Keywords: patch

Created on 2014-01-29 13:49 by serhiy.storchaka, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
py_clear.spatch serhiy.storchaka, 2014-01-29 13:49
py_clear-3.4.patch serhiy.storchaka, 2014-01-29 13:50 review
py_clear-3.3.patch serhiy.storchaka, 2014-01-29 13:50 review
py_clear-2.7.patch serhiy.storchaka, 2014-01-29 13:50 review
py_xdecref_clear-3.4.patch serhiy.storchaka, 2014-01-29 17:40 review
Messages (7)
msg209640 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-01-29 13:49
Some time ago Victor proposed in issue16445 a large patch (275 changes) which replaces "Py_DECREF(expr); expr = NULL;" to safer "Py_CLEAR(expr);". That patch contained was so large because it included cases where expr is just a local variable, which are safe in general.

Here are much simpler patches which make replacements only for complex expressions.

These patches (as Victor patch) are generated automatically by the Coccinelle tool (http://coccinelle.lip6.fr/).
msg209641 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-01-29 14:00
Patches are generated by following command:

spatch --in-place --sp-file py_clear.spatch --dir .
msg209647 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2014-01-29 14:20
LGTM.  If it compiles cleanly and passes the unit test suite--which obviously it should--you may check the 3.4 patch to trunk.
msg209661 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-01-29 17:40
And here is a patch which replaces "Py_XDECREF(identifier); identifier = NULL;" to "Py_CLEAR(identifier);" (10 replaces in 7 files). It doesn't fix any bug (unless identifier is global variable), but makes a code a little cleaner. Compilers should generate equivalent code for both writing. It would be good to commit it too.
msg209686 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-01-29 22:14
Nice work, LGTM!
msg210750 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-02-09 11:56
New changeset 6b10943a5916 by Serhiy Storchaka in branch '2.7':
Issue #20437: Fixed 43 potential bugs when deleting objects references.
http://hg.python.org/cpython/rev/6b10943a5916

New changeset 6adac0d9b933 by Serhiy Storchaka in branch '3.3':
Issue #20437: Fixed 21 potential bugs when deleting objects references.
http://hg.python.org/cpython/rev/6adac0d9b933

New changeset 3110fb3095a2 by Serhiy Storchaka in branch 'default':
Issue #20437: Fixed 22 potential bugs when deleting objects references.
http://hg.python.org/cpython/rev/3110fb3095a2
msg210766 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-02-09 13:56
Thanks Larry and Antoine for your reviews.
History
Date User Action Args
2022-04-11 14:57:57adminsetgithub: 64636
2014-02-09 13:56:25serhiy.storchakasetstatus: open -> closed
messages: + msg210766

assignee: serhiy.storchaka
resolution: fixed
stage: patch review -> resolved
2014-02-09 11:56:21python-devsetnosy: + python-dev
messages: + msg210750
2014-01-29 22:14:11pitrousetnosy: + pitrou
messages: + msg209686
2014-01-29 18:16:52serhiy.storchakasetnosy: + georg.brandl, benjamin.peterson
2014-01-29 17:40:50serhiy.storchakasetfiles: + py_xdecref_clear-3.4.patch

messages: + msg209661
2014-01-29 14:20:09larrysetnosy: + larry
messages: + msg209647
2014-01-29 14:00:55serhiy.storchakasetmessages: + msg209641
2014-01-29 13:50:55serhiy.storchakasetfiles: + py_clear-2.7.patch
2014-01-29 13:50:38serhiy.storchakasetfiles: + py_clear-3.3.patch
2014-01-29 13:50:23serhiy.storchakasetfiles: + py_clear-3.4.patch
keywords: + patch
2014-01-29 13:49:54serhiy.storchakacreate