COMMON: Fix bug #14892

When both paths were equal, suffix was incremented one char too much and
went off the string.
Add test cases to check for this.
This commit is contained in:
Le Philousophe 2024-02-02 08:06:59 +01:00
parent de585f5799
commit aceaf99816
2 changed files with 19 additions and 4 deletions

View file

@ -601,13 +601,20 @@ const char *Path::getSuffix(const Common::Path &other) const {
if (_str.hasPrefix(other._str)) {
const char *suffix = _str.c_str() + other._str.size();
if (!other.isSeparatorTerminated()) {
// Make sure we didn't end up in the middle of some path component
if (*suffix != SEPARATOR && *suffix != '\x00') {
if (*suffix == SEPARATOR) {
// Skip the separator
return suffix + 1;
} else if (*suffix == '\x00') {
// Both paths are equal: return end of string
return suffix;
} else {
// We are in the middle of some path component: this is not relative
return nullptr;
}
suffix++;
} else {
// Other already had a separator: this is relative and starts with next component
return suffix;
}
return suffix;
} else {
return nullptr;
}

View file

@ -301,6 +301,10 @@ class PathTestSuite : public CxxTest::TestSuite
// Everything is relative to empty
TS_ASSERT_EQUALS(p1.isRelativeTo(p), true);
TS_ASSERT_EQUALS(p2.isRelativeTo(p), true);
// Everything is relative to itself
TS_ASSERT_EQUALS(p1.isRelativeTo(p1), true);
TS_ASSERT_EQUALS(p2.isRelativeTo(p2), true);
// A path is not relative to empty one
TS_ASSERT_EQUALS(p.isRelativeTo(p1), false);
@ -326,6 +330,10 @@ class PathTestSuite : public CxxTest::TestSuite
// Everything is relative to empty
TS_ASSERT_EQUALS(p1.relativeTo(p).toString(), TEST_PATH);
TS_ASSERT_EQUALS(p2.relativeTo(p).toString(), TEST_ESCAPED1_PATH);
// Everything is relative to itself
TS_ASSERT_EQUALS(p1.relativeTo(p1), p);
TS_ASSERT_EQUALS(p2.relativeTo(p2), p);
// A path is not relative to empty one
TS_ASSERT_EQUALS(p.relativeTo(p1), p);