From 85e8724e69cf54946c7b40628e88f2ec12368d7c Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Wed, 20 Sep 2017 08:36:18 -0700 Subject: [PATCH] SORT VARIABLES: Improve stability of sort. The sort was being made "stable" by comparing variables' addresses in memory. There's nothing particularly predictable about that, especially from one malloc implementation to another. This commit fixes the stability by comparing the variables' dictionary indexes instead. Bug #52072. --- src/language/dictionary/sort-variables.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/language/dictionary/sort-variables.c b/src/language/dictionary/sort-variables.c index 8c653af724..aee341bda6 100644 --- a/src/language/dictionary/sort-variables.c +++ b/src/language/dictionary/sort-variables.c @@ -190,7 +190,11 @@ compare_vars (const void *a_, const void *b_, const void *c_) /* Make this a stable sort. */ if (!retval) - retval = a < b ? -1 : a > b; + { + size_t a_index = var_get_dict_index (a); + size_t b_index = var_get_dict_index (b); + retval = a_index < b_index ? -1 : a_index > b_index; + } if (c->descending) retval = -retval; -- 2.30.2