Fix possible crash in name generation.

This commit is contained in:
vng 2013-01-19 14:38:29 +03:00 committed by Alex Zolotarev
parent 52d4124b3c
commit 62a2729309
2 changed files with 35 additions and 11 deletions

View file

@ -450,16 +450,23 @@ string BookmarkCategory::RemoveInvalidSymbols(string const & name)
string BookmarkCategory::GenerateUniqueFileName(const string & path, string name)
{
string const kmlExt(".kml");
// check if file name already contains .kml extension
size_t const extPos = name.rfind(kmlExt);
if (extPos == name.size() - kmlExt.size())
name.resize(name.size() - kmlExt.size());
if (extPos != string::npos)
{
// remove extension
ASSERT_GREATER_OR_EQUAL(name.size(), kmlExt.size(), ());
size_t const expectedPos = name.size() - kmlExt.size();
if (extPos == expectedPos)
name.resize(expectedPos);
}
size_t counter = 1;
string suffix;
while (Platform::IsFileExistsByFullPath(path + name + suffix + ".kml"))
while (Platform::IsFileExistsByFullPath(path + name + suffix + kmlExt))
suffix = strings::to_string(counter++);
return (path + name + suffix + ".kml");
return (path + name + suffix + kmlExt);
}
bool BookmarkCategory::SaveToKMLFile()

View file

@ -200,15 +200,19 @@ UNIT_TEST(Bookmarks_ExportKML)
namespace
{
// Call this function to delete test category files.
void DeleteCategoryFiles()
template <size_t N> void DeleteCategoryFiles(char const * (&arrFiles)[N])
{
string const path = GetPlatform().WritableDir();
char const * arrFiles[] = { "cat1", "cat2", "cat3" };
for (size_t i = 0; i < ARRAY_SIZE(arrFiles); ++i)
for (size_t i = 0; i < N; ++i)
FileWriter::DeleteFileX(path + arrFiles[i] + ".kml");
}
// Call this function to delete test category files.
void DeleteDefCategoryFiles()
{
char const * arrFiles[] = { "cat1", "cat2", "cat3" };
DeleteCategoryFiles(arrFiles);
}
}
UNIT_TEST(Bookmarks_Timestamp)
@ -297,7 +301,7 @@ UNIT_TEST(Bookmarks_Getting)
cat->DeleteBookmark(0);
TEST_EQUAL(cat->GetBookmarksCount(), 0, ());
DeleteCategoryFiles();
DeleteDefCategoryFiles();
}
UNIT_TEST(Bookmarks_AddressInfo)
@ -413,7 +417,7 @@ UNIT_TEST(Bookmarks_AddingMoving)
TEST_EQUAL(pBm->GetName(), "name3", ());
TEST_EQUAL(pBm->GetType(), "placemark-green", ());
DeleteCategoryFiles();
DeleteDefCategoryFiles();
}
namespace
@ -452,3 +456,16 @@ UNIT_TEST(Bookmarks_InnerFolder)
TEST_EQUAL(cat.GetBookmarksCount(), 1, ());
}
UNIT_TEST(BookmarkCategory_EmptyName)
{
BookmarkCategory * pCat = new BookmarkCategory("");
pCat->AddBookmark(Bookmark(m2::PointD(0, 0), "", "placemark-red"), 17);
pCat->SaveToKMLFile();
pCat->SetName("xxx");
pCat->SaveToKMLFile();
char const * arrFiles[] = { "Bookmarks", "xxx" };
DeleteCategoryFiles(arrFiles);
}