From 8333efe8d2b533ccd1642f3ceb279b725fadf026 Mon Sep 17 00:00:00 2001 From: Tobias Frejo Date: Fri, 27 Sep 2024 10:51:58 +0200 Subject: [PATCH 1/4] Work on copy of command to avoid side effect from tokenization --- src/wrapper/slash_py.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/wrapper/slash_py.c b/src/wrapper/slash_py.c index da8c79d..b877bc7 100644 --- a/src/wrapper/slash_py.c +++ b/src/wrapper/slash_py.c @@ -1,4 +1,5 @@ #include "slash_py.h" +#include #include #define PY_SSIZE_T_CLEAN @@ -26,5 +27,13 @@ PyObject * pycsh_slash_execute(PyObject * self, PyObject * args, PyObject * kwds char hist_buf[HISTORY_SIZE]; slash_create_static(&slas, line_buf, LINE_SIZE, hist_buf, HISTORY_SIZE); - return Py_BuildValue("i", slash_execute(&slas, command)); + size_t cmd_len = strlen(command); + char * cmd_cpy = malloc(cmd_len); + strncpy(cmd_cpy, command, cmd_len); + + PyObject * ret = Py_BuildValue("i", slash_execute(&slas, cmd_cpy)); + + free(cmd_cpy); + + return ret; } From e714934807e6d90bf1d9ea58bbadb56ad96069bc Mon Sep 17 00:00:00 2001 From: Tobias Frejo Date: Fri, 27 Sep 2024 12:24:00 +0200 Subject: [PATCH 2/4] Use strdup instead of manual malloc --- src/wrapper/slash_py.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/wrapper/slash_py.c b/src/wrapper/slash_py.c index b877bc7..d547c06 100644 --- a/src/wrapper/slash_py.c +++ b/src/wrapper/slash_py.c @@ -1,6 +1,7 @@ #include "slash_py.h" -#include #include +#include +#include #define PY_SSIZE_T_CLEAN #include @@ -27,9 +28,8 @@ PyObject * pycsh_slash_execute(PyObject * self, PyObject * args, PyObject * kwds char hist_buf[HISTORY_SIZE]; slash_create_static(&slas, line_buf, LINE_SIZE, hist_buf, HISTORY_SIZE); - size_t cmd_len = strlen(command); - char * cmd_cpy = malloc(cmd_len); - strncpy(cmd_cpy, command, cmd_len); + char * cmd_cpy = strdup(command); + slash_printf(&slas, "executing: '%s'\n", cmd_cpy); PyObject * ret = Py_BuildValue("i", slash_execute(&slas, cmd_cpy)); From b430afec6adb99439c39d638b2cb462faec77ad9 Mon Sep 17 00:00:00 2001 From: Tobias Frejo Date: Wed, 20 Nov 2024 15:29:13 +0100 Subject: [PATCH 3/4] Remove debug print --- src/wrapper/slash_py.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/wrapper/slash_py.c b/src/wrapper/slash_py.c index d547c06..8c268ee 100644 --- a/src/wrapper/slash_py.c +++ b/src/wrapper/slash_py.c @@ -29,7 +29,6 @@ PyObject * pycsh_slash_execute(PyObject * self, PyObject * args, PyObject * kwds slash_create_static(&slas, line_buf, LINE_SIZE, hist_buf, HISTORY_SIZE); char * cmd_cpy = strdup(command); - slash_printf(&slas, "executing: '%s'\n", cmd_cpy); PyObject * ret = Py_BuildValue("i", slash_execute(&slas, cmd_cpy)); From 29848944e0f297df65b2ba60d8eb96f403a29f74 Mon Sep 17 00:00:00 2001 From: Tobias Frejo Date: Wed, 20 Nov 2024 15:29:51 +0100 Subject: [PATCH 4/4] Use CLEANUP_FREE to ensure memory is freed --- src/wrapper/slash_py.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/wrapper/slash_py.c b/src/wrapper/slash_py.c index 8c268ee..be6b66c 100644 --- a/src/wrapper/slash_py.c +++ b/src/wrapper/slash_py.c @@ -2,6 +2,7 @@ #include #include #include +#include "../utils.h" #define PY_SSIZE_T_CLEAN #include @@ -28,11 +29,9 @@ PyObject * pycsh_slash_execute(PyObject * self, PyObject * args, PyObject * kwds char hist_buf[HISTORY_SIZE]; slash_create_static(&slas, line_buf, LINE_SIZE, hist_buf, HISTORY_SIZE); - char * cmd_cpy = strdup(command); + char * cmd_cpy CLEANUP_FREE = strdup(command); PyObject * ret = Py_BuildValue("i", slash_execute(&slas, cmd_cpy)); - free(cmd_cpy); - return ret; }