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

Fix incorrect types passed to Py_BuildValue and PyArg_ParseTupleAndKeywords #193

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/Automaton.c
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ automaton_get(PyObject* self, PyObject* args) {
switch (automaton->store) {
case STORE_INTS:
case STORE_LENGTH:
return F(Py_BuildValue)("i", node->output.integer);
return F(PyLong_FromVoidPtr)((void *)node->output.integer);

case STORE_ANY:
Py_INCREF(node->output.object);
Expand Down Expand Up @@ -890,7 +890,7 @@ automaton_iter(PyObject* self, PyObject* args, PyObject* keywds) {
return NULL;
}

if (!F(PyArg_ParseTupleAndKeywords)(args, keywds, "O|iii", kwlist, &object, &start_tmp, &end_tmp, &ignore_white_space_tmp)) {
if (!F(PyArg_ParseTupleAndKeywords)(args, keywds, "O|nni", kwlist, &object, &start_tmp, &end_tmp, &ignore_white_space_tmp)) {
return NULL;
}

Expand Down Expand Up @@ -1084,7 +1084,7 @@ automaton_get_stats(PyObject* self, PyObject* args) {
get_stats(automaton);

dict = F(Py_BuildValue)(
"{s:k,s:k,s:k,s:k,s:i,s:k}",
"{s:n,s:n,s:n,s:n,s:n,s:n}",
"nodes_count", automaton->stats.nodes_count,
"words_count", automaton->stats.words_count,
"longest_word", automaton->stats.longest_word,
Expand Down Expand Up @@ -1123,19 +1123,19 @@ dump_aux(TrieNode* node, const int depth, void* extra) {


// 1.
tuple = F(Py_BuildValue)("ii", node, (int)(node->eow));
tuple = F(Py_BuildValue)("Ni", PyLong_FromVoidPtr(node), (int)(node->eow));
append_tuple(Dump->nodes)

// 2.
for (i=0; i < node->n; i++) {
child = trienode_get_ith_unsafe(node, i);
tuple = F(Py_BuildValue)("ici", node, trieletter_get_ith_unsafe(node, i), child);
tuple = F(Py_BuildValue)("NcN", PyLong_FromVoidPtr(node), (char)trieletter_get_ith_unsafe(node, i), PyLong_FromVoidPtr(child));
append_tuple(Dump->edges)
}

// 3.
if (node->fail) {
tuple = F(Py_BuildValue)("ii", node, node->fail);
tuple = F(Py_BuildValue)("NN", PyLong_FromVoidPtr(node), PyLong_FromVoidPtr(node->fail));
append_tuple(Dump->fail);
}

Expand Down Expand Up @@ -1193,7 +1193,7 @@ automaton___sizeof__(PyObject* self, PyObject* args) {
size += automaton->stats.total_size;
}

return Py_BuildValue("i", size);
return PyLong_FromSsize_t(size);
#undef automaton
}

Expand Down
2 changes: 1 addition & 1 deletion src/AutomatonItemsIter.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ automaton_items_iter_next(PyObject* self) {

case STORE_LENGTH:
case STORE_INTS:
return F(Py_BuildValue)("i", iter->state->output.integer);
return F(PyLong_FromVoidPtr)((void *)iter->state->output.integer);

default:
PyErr_SetString(PyExc_SystemError, "Incorrect 'store' attribute.");
Expand Down
4 changes: 2 additions & 2 deletions src/AutomatonSearchIter.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,11 @@ automaton_build_output(PyObject* self, PyObject** result) {
switch (iter->automaton->store) {
case STORE_LENGTH:
case STORE_INTS:
*result = F(Py_BuildValue)("ii", idx, node->output.integer);
*result = F(Py_BuildValue)("nN", idx, PyLong_FromVoidPtr((void *)node->output.integer));
return OutputValue;

case STORE_ANY:
*result = F(Py_BuildValue)("iO", idx, node->output.object);
*result = F(Py_BuildValue)("nO", idx, node->output.object);
return OutputValue;

default:
Expand Down
2 changes: 1 addition & 1 deletion src/AutomatonSearchIterLong.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ automaton_build_output_iter_long(PyObject* self) {
switch (iter->automaton->store) {
case STORE_LENGTH:
case STORE_INTS:
return Py_BuildValue("ii", iter->shift + iter->last_index, iter->last_node->output.integer);
return Py_BuildValue("iN", iter->shift + iter->last_index, PyLong_FromVoidPtr((void *)iter->last_node->output.integer));

case STORE_ANY:
return Py_BuildValue("iO", iter->shift + iter->last_index, iter->last_node->output.object);
Expand Down
2 changes: 2 additions & 0 deletions src/pycallfault/pycallfault.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ int check_and_set_error(void);

#define Py_BuildValue_custom(...) (check_and_set_error() ? NULL : Py_BuildValue(__VA_ARGS__))

#define PyLong_FromVoidPtr_custom(arg) (check_and_set_error() ? NULL : PyLong_FromVoidPtr(arg))

#define PyCallable_Check_custom(arg) (check() ? 0 : PyCallable_Check(arg))

#define PyString_Check_custom(arg) (check() ? 0 : PyString_Check(arg))
Expand Down