[BUG] Missing error checking for overflowing integer casts
Open
@YannickJadoul is already working on this.
Since Jan 12, 2021.
bug
- Dominant language
- C++
- Stars
- 18k
- Forks
- 2.3k
- Avg merge
- 5d 17h
- Merged PRs (30d)
- 10
Description
Issue description
There appears to be some missing error checking around Python int to C++ long casts that may overflow. In the example below, since the Python -> C++ cast fails, I expect a C++ exception to be thrown. However, it appears the Python interpreter's error state ends up set but pybind11 fails to notice.
I suspect that this method needs to check the interpreter's error state (since PyLong_AsLong may fail if the integer is larger than a C long)
https://github.com/pybind/pybind11/blob/98f1bbb8004f654ba9e26717bdf5912fb899b05a/include/pybind11/pytypes.h#L1143
Reproducible example code
#include <pybind11/pybind11.h>
#include <iostream>
int docast(pybind11::object a) {
int b = pybind11::cast<pybind11::int_>(a);
std::cerr << "Should not reach here" << std::endl;
return b;
}
PYBIND11_MODULE(t, m) {
m.def("docast", &docast, "Perform a cast");
}
In [1]: import t
In [2]: t.docast(3123412423423423234234234)
Should not reach here
---------------------------------------------------------------------------
OverflowError Traceback (most recent call last)
OverflowError: Python int too large to convert to C long
The above exception was the direct cause of the following exception:
SystemError Traceback (most recent call last)
<ipython-input-3-688cef4b7a48> in <module>
----> 1 t.docast(3123412423423423234234234)
SystemError: <built-in method docast of PyCapsule object at 0x102917720> returned a result with an error set
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.