6206: Remove possible overflow in str_comp_filenames (fixes #6204) r=def- a=Chairn


## Checklist

- [ ] Tested the change ingame
- [ ] Provided screenshots if it is a visual change
- [ ] Tested in combination with possibly related configuration options
- [x] Written a unit test (especially base/) or added coverage to integration test
- [x] Considered possible null pointers and out of bounds array indexing
- [x] Changed no physics that affect existing maps
- [x] Tested the change with [ASan+UBSan or valgrind's memcheck](https://github.com/ddnet/ddnet/#using-addresssanitizer--undefinedbehavioursanitizer-or-valgrinds-memcheck) (optional)


Co-authored-by: Chairn <chairn.nq@hotmail.fr>
This commit is contained in:
bors[bot] 2022-12-30 21:25:03 +00:00 committed by GitHub
commit beae6b8a6b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 4 deletions

View file

@ -2831,11 +2831,9 @@ int str_comp_filenames(const char *a, const char *b)
{
if(*a >= '0' && *a <= '9' && *b >= '0' && *b <= '9')
{
result = 0;
do
{
if(!result)
result = *a - *b;
result = *a - *b;
++a;
++b;
} while(*a >= '0' && *a <= '9' && *b >= '0' && *b <= '9');
@ -2844,7 +2842,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 if(!(!result && *a && *b))
return result;
}

View file

@ -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);