diff options
author | Nicolas Pauss <nicolas.pauss@gmail.com> | 2021-09-06 18:12:18 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-06 18:12:18 +0200 |
commit | 16a82475f45c163d9a3ca74e177ad4eb097289eb (patch) | |
tree | 4fd132d994949a0ca412c0b19c14b5b93d4a72fd | |
parent | 2b52df4d75d185f7ac8b4230529e7fc2428a3605 (diff) | |
download | cython-16a82475f45c163d9a3ca74e177ad4eb097289eb.tar.gz |
Fix maybe uninitialized `value` in get_value and get_value_no_default. (GH-4361)
ff16389c0da5acc2be1 introduced convenient functions get_value() and get_value_no_default().
Unfortunately, the variable `value` in these functions was not set before usage with PyContextVar_Get().
This triggered some warnings:
Error compiling Cython file:
------------------------------------------------------------
...
"""Return a new reference to the value of the context variable,
or the default value of the context variable,
or None if no such value or default was found.
"""
cdef PyObject *value
PyContextVar_Get(var, NULL, &value)
^
------------------------------------------------------------
Cython/Includes/cpython/contextvars.pxd:118:33: local variable 'value' might be referenced before assignment
Error compiling Cython file:
------------------------------------------------------------
...
or the provided default value if no such value was found.
Ignores the default value of the context variable, if any.
"""
cdef PyObject *value
PyContextVar_Get(var, <PyObject*>default_value, &value)
^
------------------------------------------------------------
Cython/Includes/cpython/contextvars.pxd:136:53: local variable 'value' might be referenced before assignment
It can be replicated by simply importing `cpython`:
echo "cimport cpython" >/tmp/mod.pyx && ./cython.py -Werror -Wextra /tmp/mod.pyx
The solution is simply to assign NULL to `value` on declaration.
-rw-r--r-- | Cython/Includes/cpython/contextvars.pxd | 4 |
1 files changed, 2 insertions, 2 deletions
diff --git a/Cython/Includes/cpython/contextvars.pxd b/Cython/Includes/cpython/contextvars.pxd index cc73cb365..d4065cd8e 100644 --- a/Cython/Includes/cpython/contextvars.pxd +++ b/Cython/Includes/cpython/contextvars.pxd @@ -114,7 +114,7 @@ cdef inline object get_value(var, default_value=None): or the default value of the context variable, or None if no such value or default was found. """ - cdef PyObject *value + cdef PyObject *value = NULL PyContextVar_Get(var, NULL, &value) if value is NULL: # context variable does not have a default @@ -132,7 +132,7 @@ cdef inline object get_value_no_default(var, default_value=None): Ignores the default value of the context variable, if any. """ - cdef PyObject *value + cdef PyObject *value = NULL PyContextVar_Get(var, <PyObject*>default_value, &value) # value of context variable or 'default_value' pyvalue = <object>value |