From ed586689315e1b48feb513b27c7d4a82edf06882 Mon Sep 17 00:00:00 2001 From: Chairn Date: Fri, 30 Dec 2022 00:23:50 +0100 Subject: [PATCH 1/2] Remove possible overflow in str_comp_filenames (fixes #6204) --- src/base/system.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/base/system.cpp b/src/base/system.cpp index ac345d236..40f7b00a1 100644 --- a/src/base/system.cpp +++ b/src/base/system.cpp @@ -2834,8 +2834,7 @@ int str_comp_filenames(const char *a, const char *b) result = 0; do { - if(!result) - result = *a - *b; + result = *a - *b; ++a; ++b; } while(*a >= '0' && *a <= '9' && *b >= '0' && *b <= '9'); @@ -2844,7 +2843,7 @@ int str_comp_filenames(const char *a, const char *b) return 1; else if(*b >= '0' && *b <= '9') return -1; - else if(result) + else return result; } From 2847d0f6d02e3a23947c7a491223bfe37ec99689 Mon Sep 17 00:00:00 2001 From: Chairn Date: Fri, 30 Dec 2022 01:11:57 +0100 Subject: [PATCH 2/2] Added some test cases that were failing under new version --- src/base/system.cpp | 3 +-- src/test/str.cpp | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/base/system.cpp b/src/base/system.cpp index 40f7b00a1..092a781ca 100644 --- a/src/base/system.cpp +++ b/src/base/system.cpp @@ -2831,7 +2831,6 @@ int str_comp_filenames(const char *a, const char *b) { if(*a >= '0' && *a <= '9' && *b >= '0' && *b <= '9') { - result = 0; do { result = *a - *b; @@ -2843,7 +2842,7 @@ int str_comp_filenames(const char *a, const char *b) return 1; else if(*b >= '0' && *b <= '9') return -1; - else + else if(!(!result && *a && *b)) return result; } diff --git a/src/test/str.cpp b/src/test/str.cpp index 8304a0d08..e4635ecd1 100644 --- a/src/test/str.cpp +++ b/src/test/str.cpp @@ -820,6 +820,13 @@ TEST(Str, CompFilename) EXPECT_GT(str_comp_filenames("b", "A"), 0); EXPECT_LT(str_comp_filenames("a", "B"), 0); EXPECT_GT(str_comp_filenames("B", "a"), 0); + EXPECT_EQ(str_comp_filenames("1A", "1a"), 0); + EXPECT_LT(str_comp_filenames("1a", "1B"), 0); + EXPECT_GT(str_comp_filenames("1B", "1a"), 0); + EXPECT_LT(str_comp_filenames("1a", "1b"), 0); + EXPECT_GT(str_comp_filenames("1b", "1a"), 0); + EXPECT_GT(str_comp_filenames("12a", "1B"), 0); + EXPECT_LT(str_comp_filenames("1B", "12a"), 0); EXPECT_LT(str_comp_filenames("abc", "abcd"), 0); EXPECT_GT(str_comp_filenames("abcd", "abc"), 0); EXPECT_LT(str_comp_filenames("abc2", "abcd1"), 0); @@ -829,8 +836,17 @@ TEST(Str, CompFilename) EXPECT_EQ(str_comp_filenames("file0", "file0"), 0); EXPECT_LT(str_comp_filenames("file0", "file1"), 0); EXPECT_GT(str_comp_filenames("file1", "file0"), 0); + EXPECT_LT(str_comp_filenames("file1", "file09"), 0); + EXPECT_GT(str_comp_filenames("file09", "file1"), 0); + EXPECT_LT(str_comp_filenames("file1", "file009"), 0); + EXPECT_GT(str_comp_filenames("file009", "file1"), 0); EXPECT_LT(str_comp_filenames("file13", "file37"), 0); EXPECT_GT(str_comp_filenames("file37", "file13"), 0); + EXPECT_LT(str_comp_filenames("file1.ext", "file09.ext"), 0); + EXPECT_GT(str_comp_filenames("file09.ext", "file1.ext"), 0); + EXPECT_LT(str_comp_filenames("file1.ext", "file009.ext"), 0); + EXPECT_GT(str_comp_filenames("file009.ext", "file1.ext"), 0); + EXPECT_EQ(str_comp_filenames("file0.ext", "file0.ext"), 0); EXPECT_LT(str_comp_filenames("file13.ext", "file37.ext"), 0); EXPECT_GT(str_comp_filenames("file37.ext", "file13.ext"), 0); EXPECT_LT(str_comp_filenames("FILE13.EXT", "file37.ext"), 0);