Commit 4b324243 authored by Marius Wachtler's avatar Marius Wachtler

additional error handling

parent 7b270cf9
......@@ -2246,11 +2246,11 @@ _PyExc_Init(void)
POST_INIT(UnicodeWarning)
POST_INIT(BytesWarning)
PyExc_MemoryErrorInst = BaseException_new(&_PyExc_MemoryError, NULL, NULL);
PyExc_MemoryErrorInst = PyGC_AddRoot(BaseException_new(&_PyExc_MemoryError, NULL, NULL));
if (!PyExc_MemoryErrorInst)
Py_FatalError("Cannot pre-allocate MemoryError instance");
PyExc_RecursionErrorInst = BaseException_new(&_PyExc_RuntimeError, NULL, NULL);
PyExc_RecursionErrorInst = PyGC_AddRoot(BaseException_new(&_PyExc_RuntimeError, NULL, NULL));
if (!PyExc_RecursionErrorInst)
Py_FatalError("Cannot pre-allocate RuntimeError instance for "
"recursion errors");
......
......@@ -51,6 +51,10 @@ extern "C" inline void* gc_alloc(size_t bytes, GCKind kind_id) {
// inline it, which is especially useful for this function.
ScopedStatTimer gc_alloc_stattimer(gc_alloc_stattimer_counter, 15);
#endif
if ((size_t)(bytes) > (size_t)PY_SSIZE_T_MAX)
return NULL;
size_t alloc_bytes = bytes + sizeof(GCAllocation);
#ifndef NVALGRIND
......@@ -124,6 +128,9 @@ extern "C" inline void* gc_realloc(void* ptr, size_t bytes) {
// Normal realloc() supports receiving a NULL pointer, but we need to know what the GCKind is:
assert(ptr);
if ((size_t)(bytes) > (size_t)PY_SSIZE_T_MAX)
return NULL;
size_t alloc_bytes = bytes + sizeof(GCAllocation);
GCAllocation* alloc;
......
......@@ -892,8 +892,20 @@ extern "C" int PyErr_BadArgument() noexcept {
}
extern "C" PyObject* PyErr_NoMemory() noexcept {
fatalOrError(PyExc_NotImplementedError, "unimplemented");
return nullptr;
if (PyErr_ExceptionMatches(PyExc_MemoryError))
/* already current */
return NULL;
/* raise the pre-allocated instance if it still exists */
if (PyExc_MemoryErrorInst)
PyErr_SetObject(PyExc_MemoryError, PyExc_MemoryErrorInst);
else
/* this will probably fail since there's no memory and hee,
hee, we have to instantiate this class
*/
PyErr_SetNone(PyExc_MemoryError);
return NULL;
}
extern "C" const char* PyExceptionClass_Name(PyObject* o) noexcept {
......
......@@ -929,11 +929,17 @@ Box* fileNew(BoxedClass* cls, Box* s, Box* m, Box** args) {
assert(cls == file_cls);
if (s->cls == unicode_cls)
if (s->cls == unicode_cls) {
s = _PyUnicode_AsDefaultEncodedString(s, NULL);
if (!s)
throwCAPIException();
}
if (m->cls == unicode_cls)
if (m->cls == unicode_cls) {
m = _PyUnicode_AsDefaultEncodedString(m, NULL);
if (!m)
throwCAPIException();
}
if (s->cls != str_cls) {
raiseExcHelper(TypeError, "coercing to Unicode: need string of buffer, %s found", getTypeName(s));
......
......@@ -2477,6 +2477,10 @@ extern "C" int PyString_AsStringAndSize(register PyObject* obj, register char**
}
extern "C" PyObject* PyString_FromStringAndSize(const char* s, ssize_t n) noexcept {
if (n < 0) {
PyErr_SetString(PyExc_SystemError, "Negative size passed to PyString_FromStringAndSize");
return NULL;
}
if (s == NULL)
return BoxedString::createUninitializedString(n);
return boxString(llvm::StringRef(s, n));
......
......@@ -3274,7 +3274,9 @@ extern "C" PyVarObject* PyObject_InitVar(PyVarObject* op, PyTypeObject* tp, Py_s
}
extern "C" PyObject* PyObject_Init(PyObject* op, PyTypeObject* tp) noexcept {
assert(op);
if (op == NULL)
return PyErr_NoMemory();
assert(tp);
assert(gc::isValidGCMemory(op));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment