aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRobin Haberkorn <robin.haberkorn@googlemail.com>2024-10-15 23:33:43 +0300
committerRobin Haberkorn <robin.haberkorn@googlemail.com>2024-10-15 23:33:43 +0300
commitd556aee67e615b48c25861741d28d103109235e3 (patch)
tree46c30b4c420c1f50cdc8b4e10a1e13e440eb1c01
parent7413b9cab2690f7bed6d82e903b0fc08b1590360 (diff)
downloadsciteco-d556aee67e615b48c25861741d28d103109235e3.tar.gz
fixed memory corruptions due to undoing the teco_machine_stringbuilding_t::codepage
* It's contained in teco_machine_main_t which is created per macro call frame. So after macro calls, the machine no longer exists. It is therefore unsafe to undo its members indiscriminately. * On the other hand, we must undo the codepage setting when run interactively, so it is now only undone when belonging to the commandline macro frame. * This was actually causing memory corruptions on every fnkeys cursor movement, but never caused crashes - probably because the invalid pointers are always pointing to unused parts of the C call stack. * Initially broken in b31b8871.
-rw-r--r--src/core-commands.c6
-rw-r--r--src/parser.c12
-rw-r--r--src/qreg-commands.c6
-rw-r--r--src/search.c19
-rw-r--r--src/spawn.c7
5 files changed, 42 insertions, 8 deletions
diff --git a/src/core-commands.c b/src/core-commands.c
index a390fa4..befb6e8 100644
--- a/src/core-commands.c
+++ b/src/core-commands.c
@@ -2759,8 +2759,12 @@ teco_state_insert_initial(teco_machine_main_t *ctx, GError **error)
/*
* Current document's encoding determines the behaviour of
* string building constructs.
+ *
+ * NOTE: This is not safe to undo in macro calls.
*/
- teco_undo_guint(ctx->expectstring.machine.codepage) = teco_interface_get_codepage();
+ if (ctx == &teco_cmdline.machine)
+ teco_undo_guint(ctx->expectstring.machine.codepage);
+ ctx->expectstring.machine.codepage = teco_interface_get_codepage();
if (!teco_expressions_eval(FALSE, error))
return FALSE;
diff --git a/src/parser.c b/src/parser.c
index 8cb26e7..4e42833 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -35,6 +35,7 @@
#include "ring.h"
#include "glob.h"
#include "error.h"
+#include "cmdline.h"
#include "core-commands.h"
#include "goto-commands.h"
#include "parser.h"
@@ -906,8 +907,15 @@ teco_machine_stringbuilding_clear(teco_machine_stringbuilding_t *ctx)
gboolean
teco_state_expectstring_initial(teco_machine_main_t *ctx, GError **error)
{
- if (ctx->mode == TECO_MODE_NORMAL)
- teco_undo_guint(ctx->expectstring.machine.codepage) = teco_default_codepage();
+ if (ctx->mode > TECO_MODE_NORMAL)
+ return TRUE;
+
+ /*
+ * NOTE: This is not safe to undo in macro calls.
+ */
+ if (ctx == &teco_cmdline.machine)
+ teco_undo_guint(ctx->expectstring.machine.codepage);
+ ctx->expectstring.machine.codepage = teco_default_codepage();
return TRUE;
}
diff --git a/src/qreg-commands.c b/src/qreg-commands.c
index 41e4465..f8bb4cb 100644
--- a/src/qreg-commands.c
+++ b/src/qreg-commands.c
@@ -29,6 +29,7 @@
#include "interface.h"
#include "ring.h"
#include "parser.h"
+#include "cmdline.h"
#include "core-commands.h"
#include "qreg.h"
#include "qreg-commands.h"
@@ -492,8 +493,11 @@ teco_state_setqregstring_building_initial(teco_machine_main_t *ctx, GError **err
/*
* The expected codepage of string building constructs is determined
* by the Q-Register.
+ *
+ * NOTE: This is not safe to undo in macro calls.
*/
- teco_undo_guint(ctx->expectstring.machine.codepage);
+ if (ctx == &teco_cmdline.machine)
+ teco_undo_guint(ctx->expectstring.machine.codepage);
return qreg->vtable->get_string(qreg, NULL, NULL, &ctx->expectstring.machine.codepage, error);
}
diff --git a/src/search.c b/src/search.c
index db85283..22dc726 100644
--- a/src/search.c
+++ b/src/search.c
@@ -35,6 +35,7 @@
#include "parser.h"
#include "core-commands.h"
#include "error.h"
+#include "cmdline.h"
#include "search.h"
typedef struct {
@@ -60,7 +61,12 @@ teco_state_search_initial(teco_machine_main_t *ctx, GError **error)
if (ctx->mode > TECO_MODE_NORMAL)
return TRUE;
- teco_undo_guint(ctx->expectstring.machine.codepage) = teco_interface_get_codepage();
+ /*
+ * NOTE: This is not safe to undo in macro calls.
+ */
+ if (ctx == &teco_cmdline.machine)
+ teco_undo_guint(ctx->expectstring.machine.codepage);
+ ctx->expectstring.machine.codepage = teco_interface_get_codepage();
if (G_UNLIKELY(!teco_search_qreg_machine))
teco_search_qreg_machine = teco_machine_qregspec_new(TECO_QREG_REQUIRED, ctx->qreg_table_locals,
@@ -1046,8 +1052,15 @@ TECO_DEFINE_STATE_SEARCH(teco_state_search_delete);
static gboolean
teco_state_replace_insert_initial(teco_machine_main_t *ctx, GError **error)
{
- if (ctx->mode == TECO_MODE_NORMAL)
- teco_undo_guint(ctx->expectstring.machine.codepage) = teco_interface_get_codepage();
+ if (ctx->mode > TECO_MODE_NORMAL)
+ return TRUE;
+
+ /*
+ * NOTE: This is not safe to undo in macro calls.
+ */
+ if (ctx == &teco_cmdline.machine)
+ teco_undo_guint(ctx->expectstring.machine.codepage);
+ ctx->expectstring.machine.codepage = teco_interface_get_codepage();
return TRUE;
}
diff --git a/src/spawn.c b/src/spawn.c
index 445acc5..cb1ef75 100644
--- a/src/spawn.c
+++ b/src/spawn.c
@@ -43,6 +43,7 @@
#include "core-commands.h"
#include "qreg-commands.h"
#include "error.h"
+#include "cmdline.h"
#include "spawn.h"
/*
@@ -167,8 +168,12 @@ teco_state_execute_initial(teco_machine_main_t *ctx, GError **error)
/*
* Command-lines and file names are always assumed to be UTF-8,
* unless we set TECO_ED_DEFAULT_ANSI.
+ *
+ * NOTE: This is not safe to undo in macro calls.
*/
- teco_undo_guint(ctx->expectstring.machine.codepage) = teco_default_codepage();
+ if (ctx == &teco_cmdline.machine)
+ teco_undo_guint(ctx->expectstring.machine.codepage);
+ ctx->expectstring.machine.codepage = teco_default_codepage();
if (!teco_expressions_eval(FALSE, error))
return FALSE;