Skip to content

Commit

Permalink
Fix internal error on bad parse tree output. #243
Browse files Browse the repository at this point in the history
  • Loading branch information
amykyta3 committed Dec 29, 2024
1 parent 3c6e494 commit 8c3c0c5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/systemrdl/parser/ext/sa_systemrdl_cpp_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ antlr4::tree::ParseTree* get_parse_tree(SystemRDLParser *parser, const char *ent
*/
PyObject* do_parse(PyObject *self, PyObject *args) {
PyObject *strdata = NULL;
PyObject *strdata_as_bytes = NULL;
PyObject *result = NULL;
PyObject *token_module = NULL;

Expand All @@ -70,7 +71,6 @@ PyObject* do_parse(PyObject *self, PyObject *args) {
// PyUnicode_AsUTF8AndSize is not part of the stable ABI until python3.10
// To maximize backwards compatibility, Working around by converting to
// bytes, then to char instead
PyObject *strdata_as_bytes;
strdata_as_bytes = PyCodec_Encode(strdata, "utf-8", NULL);
if(!strdata_as_bytes) throw speedy_antlr::PythonException();
PyBytes_AsStringAndSize(strdata_as_bytes, &cstrdata, &bufsize);
Expand Down Expand Up @@ -109,21 +109,23 @@ PyObject* do_parse(PyObject *self, PyObject *args) {

// Clean up data
Py_XDECREF(token_module);
Py_XDECREF(strdata_as_bytes);
Py_XDECREF(strdata);
Py_XDECREF(strdata_as_bytes);

return result;

} catch(speedy_antlr::PythonException &e) {
Py_XDECREF(token_module);
Py_XDECREF(strdata);
Py_XDECREF(strdata_as_bytes);
Py_XDECREF(result);

// Python exception already has error indicator set
return NULL;
} catch(...) {
Py_XDECREF(token_module);
Py_XDECREF(strdata);
Py_XDECREF(strdata_as_bytes);
Py_XDECREF(result);

// An internal C++ exception was thrown.
Expand Down
10 changes: 9 additions & 1 deletion src/systemrdl/parser/ext/speedy_antlr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,22 @@ PyObject* Translator::convert_ctx(
}
Py_DECREF(py_token);
} else if (antlrcpp::is<antlr4::ParserRuleContext *>(ctx->children[i])) {
std::any result;
child_ref = static_cast<void*>(ctx->children[i]);
try {
py_child = std::any_cast<PyObject *>(visitor->visit(ctx->children[i]));
result = visitor->visit(ctx->children[i]);
} catch(PythonException &e) {
Py_XDECREF(py_ctx);
Py_XDECREF(py_children);
throw;
}

if(!result.has_value()) {
py_child = Py_None;
} else {
py_child = std::any_cast<PyObject *>(result);
}

PyObject_SetAttrString(py_child, "parentCtx", py_ctx);
py_label_candidate = py_child;
Py_INCREF(py_label_candidate);
Expand Down

0 comments on commit 8c3c0c5

Please sign in to comment.