🌐
Python
bugs.python.org › issue41995
Issue 41995: five possible Null Pointer Dereference bugs. - Python tracker
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 · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/86161
🌐
Python
bugs.python.org › issue24044
Issue 24044: NULL pointer dereference in listsort() with key function - Python tracker
April 23, 2015 - 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 · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/68232
🌐
Python
bugs.python.org › issue34987
Issue 34987: A possible null pointer dereference in _pickle.c's save_reduce() - Python tracker
October 15, 2018 - 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 · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/79168
🌐
Python
bugs.python.org › issue27826
Issue 27826: Null-pointer dereference in tuplehash() function - Python tracker
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 · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/72013
🌐
Python
bugs.python.org › issue34824
Issue 34824: _ssl.c: Possible null pointer dereference - Python tracker
September 27, 2018 - 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 · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/79005
🌐
GitHub
github.com › python › cpython › issues › 126240
Possible null pointer dereference of variable args in _PyPegen_collect_call_seqs · Issue #126240 · python/cpython
October 31, 2024 - Crash report What happened? args can be NULL as a result of _Py_asdl_expr_seq_new, and then dereference happens. Return value of _Py_asdl_expr_seq_new` is usually checked, for example here and in o...
Author: python
🌐
GitHub
github.com › python › cpython › issues › 126106
Potential null pointer dereference in `PySSLSession_richcompare` · Issue #126106 · python/cpython
October 29, 2024 - Bug report Bug description: Pointer left is dereferenced here, but null-pointer check is done later. Correct code should look like this: int result; if (left == NULL || right == NULL) { PyErr_BadInternalCall(); return NULL; } PyTypeObjec...
Author: python
🌐
Python
bugs.python.org › issue36374
Issue 36374: A possible null pointer dereference in compile.c's merge_consts_recursive() - Python tracker
March 19, 2019 - 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 · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/80555
🌐
GitHub
github.com › python › cpython › issues › 96582
Possible NULL pointer dereference in _PyThread_CurrentFrames · Issue #96582 · python/cpython
September 5, 2022 - (Objects, Python, Grammar, and Parser dirs) 3.11 · only security fixes type-crash · A hard crash of the interpreter, possibly with a core dump 3.12 · bugs and security fixes labels · Sep 5, 2022 · kumaraditya303 self-assigned this · Sep 5, 2022 · This was referenced · Sep 5, 2022 · GH-96582: fix possible NULL pointer dereference in _PyThread_CurrentFrames #96584 ·
Author: python
Find elsewhere
🌐
Python
bugs.python.org › issue24704
Issue 24704: Dereferencing a Null Pointer - Python tracker
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 · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/68892
Top answer
1 of 8
20

EDIT 2021:

There is a new package that is sort of a hack featuring exactly this functionality in Python. Here is the repo: https://github.com/iann838/nullsafe-python

from nullsafe import undefined, _

value = _(variable).method()
assert value is undefined
assert not value
assert value == None

Works with AttributeError and KeyError aswell

dic = {}
assert _(dic)["nah"] is undefined
assert _(dic).nah is undefined

The wrapped object typings will remain effective.


  1. No, there isn't.

  2. But to check for None, you don't write if x:, you write if x is None:. This is an important distinction - x evaluates to False for quite a few values that are propably perfectly valid (most notably 0-equivalent numbers and empty collections), whereas x is None only evaluates to True if the reference x points to the singleton object None.

  3. From personal experience, such an operator would be needed very rarely. Yes, None is sometimes used to indicate no value. But somehow - maybe because idiomatic code returns null objects where sensible or throws exceptions to indicate critical failure - I only get an AttributeError: 'NoneType' object has no attribute '...' twice a month.

  4. I would argue that this might be a misfeature. null has two meanings - "forgot to initialize" and "no data". The first is an error and should throw an exception. The second case usually requires more elaborate handling than "let's just not call this method". When I ask the database/ORM for a UserProfile, it's not there and I get null instead... do I want to silently skip the rest of the method? Or do I really want to (when in "library code") throw an approriate exception (so "the user (code)" knows the user isn't there and can react... or ignore it) or (when I'm coding a specific feature) show a sensible message ("That user doesn't exist, you can't add it to your friend list") to the user?

2 of 8
20

First of all, your options depend on what you'd like the expression to evaluate to if the variable is not dereferencable. I'll assume None is the appropriate result in these examples.

A common idiom for some circumstances in Python uses conditional expressions:

variable.method() if variable is not None else None

While this need may not be widespread, there are circumstances where it would be useful, especially when the references are nested and you would want something like this, where that idiom quickly gets cumbersome.

a?.b?.c?.d

Note that support for a ?. operator for safe deferencing is one of the main topics of PEP 505: None-aware operators \| Python.org. It's current status is "deferred".

Some discussion of it is at:

  • PEP 505: Bringing None-Aware Operators to Python \| Hacker News
🌐
Python
bugs.python.org › issue34408
Issue 34408: possible null pointer dereference in pystate.c - Python tracker
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 · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/78589
🌐
GitHub
github.com › python › cpython › issues › 126108
Potential null pointer dereference in `PySys_AddWarnOptionUnicode` · Issue #126108 · python/cpython
October 29, 2024 - Bug report Bug description: Pointer tstate is compared to a NULL value here, but above there is a dereference. Stack trace: _PySys_AddWarnOptionWithError -> get_warnoptions -> _PySys_GetAttr. Correct code should look like this: void PySy...
Author: python
🌐
Red Hat
bugzilla.redhat.com › show_bug.cgi
1666519 – (CVE-2019-5010) CVE-2019-5010 python: NULL pointer dereference using a specially crafted X509 certificate
Red Hat Bugzilla – Bug 1666519 · This site requires JavaScript to be enabled to function correctly, please enable it · Privacy Contact FAQ Legal
🌐
Tenable
tenable.com › plugins › nessus › 123144
FreeBSD : Python -- NULL pointer dereference vulnerability (d7...<!-- --> | Tenable®
March 27, 2019 - Python Changelog : bpo-35746: [CVE-2019-5010] Fix a NULL pointer deref in ssl module. The cert parser did not handle CRL distribution points with empty DP or URI correctly. A malicious or buggy certificate can result into segfault.
🌐
GitHub
github.com › python › cpython › issues › 126238
Possible null pointer dereference of freevars in _PyCompile_LookupArg · Issue #126238 · python/cpython
November 22, 2024 - Bug report Bug description: freevars is checked for null, but later possible dereference happens. Maybe replacing Py_DECREF by Py_XDECREF should do the trick. CPython versions tested on: 3.11 Operating systems tested on: No response Link...
Author: python
🌐
Openssf
best.openssf.org › Secure-Coding-Guide-for-Python › CWE-703 › CWE-476
CWE-476: NULL Pointer Dereference | OpenSSF Best Practices Working Group
The code example attempts to directly call len() on a non-array value, in this case on None, which will raise a TypeError. Such unchecked dereferencing of a None value could result in an application crash or denial of service, impacting system availability.
🌐
Cybersecurity Help
cybersecurity-help.cz › vulnerabilities › 17805
VU17805 NULL pointer dereference in Python
July 4, 2020 - NULL pointer dereference vulnerability in Python CVE-2019-5010. The vulnerability allows a remote attacker to perform a denial of service (DoS) attack. Public exploit available.
🌐
Python
docs.python.org › 2.5 › lib › ctypes-pointers.html
14.14.1.14 Pointers
December 23, 2008 - >>> null_ptr = POINTER(c_int)() >>> print bool(null_ptr) False >>> ctypes checks for NULL when dereferencing pointers (but dereferencing non-NULL pointers would crash Python):
🌐
Python
bugs.python.org › issue27963
Issue 27963: null poiter dereference in set_conversion_mode due uncheck _ctypes_conversion_errors - Python tracker
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 · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/72150