Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segmentation fault when instantiating decimal.SignalDictMixin type #106263

Closed
CharlieZhao95 opened this issue Jun 30, 2023 · 2 comments
Closed

Segmentation fault when instantiating decimal.SignalDictMixin type #106263

CharlieZhao95 opened this issue Jun 30, 2023 · 2 comments
Labels
type-crash A hard crash of the interpreter, possibly with a core dump

Comments

@CharlieZhao95
Copy link
Contributor

CharlieZhao95 commented Jun 30, 2023

Crash report

The following code will causes a segmentation fault:

>>> import decimal
>>> tp = type(decimal.Context().flags)  # SignalDict type
>>> tp()  # Segmentation fault

This code instantiates an object of SignalDict type (inherited from the base class SignalDictMixin) and tries to print the contents of the object (use repr).

The problem is caused by the following C code, where the signaldict_repr function accesses a null pointer.

static int
signaldict_init(PyObject *self, PyObject *args UNUSED, PyObject *kwds UNUSED)
{
    SdFlagAddr(self) = NULL;
    return 0;
}
...
static PyObject *
signaldict_repr(PyObject *self)
{
    ...
    for (cm=signal_map, i=0; cm->name != NULL; cm++, i++) {
        n[i] = cm->fqname;
        // Access NULL pointer here
        b[i] = SdFlags(self)&cm->flag ? "True" : "False";
    }
    ...
}

Your environment

  • CPython versions tested on: 3.13.0.0a0, 3.10.2
  • Operating system and architecture: Ubuntu 22.04.1 LTS, Windows 11

Linked PRs

@CharlieZhao95 CharlieZhao95 added the type-crash A hard crash of the interpreter, possibly with a core dump label Jun 30, 2023
@CharlieZhao95
Copy link
Contributor Author

Note: This problem will not be triggered normally, because we will assign a value to SignalDict in the constructor of decimal.Context(). We usually don't manually construct a SignalDict object.

>>> import decimal
>>> flags = decimal.Context().flags
>>> tp = type(flags)
>>> tp
<class 'abc.SignalDict'>
>>> flags
{<class 'decimal.InvalidOperation'>:False, <class 'decimal.FloatOperation'>:False, <class 'decimal.DivisionByZero'>:False, <class 'decimal.Overflow'>:False, <class 'decimal.Underflow'>:False, <class 'decimal.Subnormal'>:False, <class 'decimal.Inexact'>:False, <class 'decimal.Rounded'>:False, <class 'decimal.Clamped'>:False}

@CharlieZhao95
Copy link
Contributor Author

CharlieZhao95 commented Jun 30, 2023

The simplest solution is to assign default values to signaldict object in the signaldict_init function. I will propose a fix. :)

kumaraditya303 pushed a commit that referenced this issue Jul 30, 2023
…06270)

Co-authored-by: sunmy2019 <59365878+sunmy2019@users.noreply.github.com>
CharlieZhao95 added a commit to CharlieZhao95/cpython that referenced this issue Jul 31, 2023
…le (python#106270)

Co-authored-by: sunmy2019 <59365878+sunmy2019@users.noreply.github.com>
(cherry picked from commit 3979150)
CharlieZhao95 added a commit to CharlieZhao95/cpython that referenced this issue Jul 31, 2023
…le (python#106270)

Co-authored-by: sunmy2019 <59365878+sunmy2019@users.noreply.github.com>
(cherry picked from commit 3979150)
ambv pushed a commit that referenced this issue Jul 31, 2023
…ule (#… (#107491)

Co-authored-by: sunmy2019 <59365878+sunmy2019@users.noreply.github.com>

(cherry picked from commit 3979150)
ambv pushed a commit that referenced this issue Jul 31, 2023
…ule (#… (#107490)

Co-authored-by: sunmy2019 <59365878+sunmy2019@users.noreply.github.com>
(cherry picked from commit 3979150)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-crash A hard crash of the interpreter, possibly with a core dump
Projects
None yet
Development

No branches or pull requests

2 participants