From f856979d85ace61bfeb2d31146485ec668202ad8 Mon Sep 17 00:00:00 2001
From: "Arnold D. Robbins" <arnold@skeeve.com>
Date: Tue, 31 Jul 2018 09:07:29 +0300
Subject: [PATCH 090/289] Fix coredump from IGNORECASE array sorting.

---
 ChangeLog           |  6 ++++++
 array.c             |  9 ++++++---
 test/ChangeLog      |  5 +++++
 test/Makefile.am    |  4 +++-
 test/Makefile.in    |  9 ++++++++-
 test/Maketests      |  5 +++++
 test/arraysort2.awk | 34 ++++++++++++++++++++++++++++++++++
 test/arraysort2.ok  | 26 ++++++++++++++++++++++++++
 8 files changed, 93 insertions(+), 5 deletions(-)
 create mode 100644 test/arraysort2.awk
 create mode 100644 test/arraysort2.ok

diff --git a/array.c b/array.c
index 5d953c04..aa52f3a1 100644
--- a/array.c
+++ b/array.c
@@ -979,7 +979,6 @@ cmp_strings(const NODE *n1, const NODE *n2)
 	char *s1, *s2;
 	size_t len1, len2;
 	int ret;
-	size_t lmin;
 
 	s1 = n1->stptr;
 	len1 = n1->stlen;
@@ -992,7 +991,9 @@ cmp_strings(const NODE *n1, const NODE *n2)
 		return 1;
 
 	/* len1 > 0 && len2 > 0 */
-	lmin = len1 < len2 ? len1 : len2;
+	// make const to ensure it doesn't change if we
+	// need to call memcmp(), below
+	const size_t lmin = len1 < len2 ? len1 : len2;
 
 	if (IGNORECASE) {
 		const unsigned char *cp1 = (const unsigned char *) s1;
@@ -1002,7 +1003,9 @@ cmp_strings(const NODE *n1, const NODE *n2)
 			ret = strncasecmpmbs((const unsigned char *) cp1,
 					     (const unsigned char *) cp2, lmin);
 		} else {
-			for (ret = 0; lmin-- > 0 && ret == 0; cp1++, cp2++)
+			size_t count = lmin;
+
+			for (ret = 0; count-- > 0 && ret == 0; cp1++, cp2++)
 				ret = casetable[*cp1] - casetable[*cp2];
 		}
 		if (ret != 0)
-- 
2.19.1

