Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.

Commit 9dca969

Browse files
committed
Fixes exception when passing empty bytes to decode
An uncaught exception was thrown and python interpreter was killed because of that Signed-off-by: ncordon <nacho.cordon.castillo@gmail.com>
1 parent d1b0798 commit 9dca969

1 file changed

Lines changed: 17 additions & 9 deletions

File tree

bblfsh/pyuast.cc

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include <cstdlib>
33
#include <cstring>
44
#include <unordered_map>
5-
5+
#include <iostream>
66
#include <Python.h>
77
#include <structmember.h>
88

@@ -1048,17 +1048,25 @@ static PyObject *PythonContextExt_decode(PyObject *self, PyObject *args, PyObjec
10481048
int res = PyObject_GetBuffer(obj, &buf, PyBUF_C_CONTIGUOUS);
10491049
if (res != 0) return nullptr;
10501050

1051-
uast::Buffer ubuf(buf.buf, (size_t)(buf.len));
1051+
PythonContextExt *pyU = nullptr;
10521052

1053-
uast::Context<NodeHandle>* ctx = uast::Decode(ubuf, format);
1054-
PyBuffer_Release(&buf);
1053+
try {
1054+
uast::Buffer ubuf(buf.buf, (size_t)(buf.len));
1055+
uast::Context<NodeHandle>* ctx = uast::Decode(ubuf, format);
1056+
pyU = PyObject_New(PythonContextExt, &PythonContextExtType);
10551057

1056-
PythonContextExt *pyU = PyObject_New(PythonContextExt, &PythonContextExtType);
1057-
if (!pyU) {
1058-
delete(ctx);
1059-
return nullptr;
1058+
if (!pyU) {
1059+
delete(ctx);
1060+
return nullptr;
1061+
} else {
1062+
pyU->p = new ContextExt(ctx);
1063+
}
1064+
} catch (const std::exception& e) {
1065+
PyErr_SetString(PyExc_RuntimeError, e.what());
1066+
pyU = nullptr;
10601067
}
1061-
pyU->p = new ContextExt(ctx);
1068+
1069+
PyBuffer_Release(&buf);
10621070
return (PyObject*)pyU;
10631071
}
10641072

0 commit comments

Comments
 (0)