From ea03451d1e9c53bdbb2f0c246e8652d9a82e808f Mon Sep 17 00:00:00 2001 From: nanahi <130121847+na-na-hi@users.noreply.github.com> Date: Sun, 9 Jun 2024 14:01:43 -0400 Subject: [PATCH] misc/natural_sort: avoid implementation-defined behavior in comparison Before a7158ceec00f14e680a885722cfe23761b4662d3, string comparision was done with strcmp, which does unsigned comparison. The natural sort implementation instead compares on char values. This causes implementation-defined behavior in comparison, depending on the signedness of char type. Fix this by using unsigned comparison instead. --- misc/natural_sort.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/misc/natural_sort.c b/misc/natural_sort.c index 3e0bab0e3c..f7b567d491 100644 --- a/misc/natural_sort.c +++ b/misc/natural_sort.c @@ -51,9 +51,9 @@ int mp_natural_sort_cmp(const char *name1, const char *name2) name2++; } } else { - if (mp_tolower(name1[0]) < mp_tolower(name2[0])) + if ((unsigned char)mp_tolower(name1[0]) < (unsigned char)mp_tolower(name2[0])) return -1; - if (mp_tolower(name1[0]) > mp_tolower(name2[0])) + if ((unsigned char)mp_tolower(name1[0]) > (unsigned char)mp_tolower(name2[0])) return 1; name1++; name2++;