From 0148e0ea47fe4f01a731e14092b4fb12b1a76c63 Mon Sep 17 00:00:00 2001
From: Andy Vawer <andy@vawer.com>
Date: Fri, 10 Sep 2021 10:14:47 +0000
Subject: [PATCH] Abandon set caret position if the window or menu isn't there

During destruction of the menu tree, if set caret position fails when resetting the caret to the old position (ie. illegal window handle as the window has been deleted) then destroying the menu tree also fails with catastrophic results.
Therefore
* Do not fault SetCaretPosition to invalid window handle, just change it to -1
* Do not try to redraw areas of an invalid window when moving caret

Version 5.82. Not tagged
---
 VersionASM | 6 +++---
 VersionNum | 8 ++++----
 s/CnPCaret | 9 ++++++++-
 3 files changed, 15 insertions(+), 8 deletions(-)

diff --git a/VersionASM b/VersionASM
index f2826be..06c4e63 100644
--- a/VersionASM
+++ b/VersionASM
@@ -12,9 +12,9 @@
 Module_MajorVersion     SETS    "5.82"
 Module_Version          SETA    582
 Module_MinorVersion     SETS    ""
-Module_Date             SETS    "14 Jul 2021"
-Module_ApplicationDate  SETS    "14-Jul-21"
+Module_Date             SETS    "13 Sep 2021"
+Module_ApplicationDate  SETS    "13-Sep-21"
 Module_ComponentName    SETS    "Wimp"
 Module_FullVersion      SETS    "5.82"
-Module_HelpVersion      SETS    "5.82 (14 Jul 2021)"
+Module_HelpVersion      SETS    "5.82 (13 Sep 2021)"
                         END
diff --git a/VersionNum b/VersionNum
index 8663b72..47364b8 100644
--- a/VersionNum
+++ b/VersionNum
@@ -5,17 +5,17 @@
  */
 #define Module_MajorVersion_CMHG        5.82
 #define Module_MinorVersion_CMHG
-#define Module_Date_CMHG                14 Jul 2021
+#define Module_Date_CMHG                13 Sep 2021
 
 #define Module_MajorVersion             "5.82"
 #define Module_Version                  582
 #define Module_MinorVersion             ""
-#define Module_Date                     "14 Jul 2021"
+#define Module_Date                     "13 Sep 2021"
 
-#define Module_ApplicationDate          "14-Jul-21"
+#define Module_ApplicationDate          "13-Sep-21"
 
 #define Module_ComponentName            "Wimp"
 
 #define Module_FullVersion              "5.82"
-#define Module_HelpVersion              "5.82 (14 Jul 2021)"
+#define Module_HelpVersion              "5.82 (13 Sep 2021)"
 #define Module_LibraryVersionInfo       "5:82"
diff --git a/s/CnPCaret b/s/CnPCaret
index 5ff869e..8f423b6 100644
--- a/s/CnPCaret
+++ b/s/CnPCaret
@@ -112,7 +112,8 @@ int_set_caret_position ROUT
         MOVEQ   R2,#0                   ; if we were called with R2='TASK' then
         STREQ   R2,[sp,#(2+1+8)*4]      ; ensure R2=0 on return - taskhandle stacked below r2
 08
-        SavePSR R6                      ; keep V flag for exiting later
+        CLRV                            ; clear V flag as we're not going to report this as an error
+        SavePSR R6
         Push    "R6"
         STR     R0,[sp,#8]              ; preserve error message
         MOV     R0,#-1                  ; carry on as an invalid window handle
@@ -541,6 +542,12 @@ wscp_remove_current_caret ROUT
 ;           r4=flags
 
         Push    "R0-R5,R10,R11,R14"
+        ; ensure we're removing from a valid window
+        Push    "handle"
+        MOV     handle,R0
+        BL      checkhandle
+        Pull    "handle"
+        MOVVS   R0,#nullptr             ; handle was invalid so change it to 'no window'
 
         ; clear redraw block
         MOV     R14,#nullptr
-- 
GitLab