From 62a272930969a5443fa80436dcc05ebef856bef2 Mon Sep 17 00:00:00 2001 From: vng Date: Sat, 19 Jan 2013 14:38:29 +0300 Subject: [PATCH] Fix possible crash in name generation. --- map/bookmark.cpp | 15 +++++++++++---- map/map_tests/bookmarks_test.cpp | 31 ++++++++++++++++++++++++------- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/map/bookmark.cpp b/map/bookmark.cpp index b5feeb3c49..cc6d253e44 100644 --- a/map/bookmark.cpp +++ b/map/bookmark.cpp @@ -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() diff --git a/map/map_tests/bookmarks_test.cpp b/map/map_tests/bookmarks_test.cpp index a8fd89187e..2c911ba1a6 100644 --- a/map/map_tests/bookmarks_test.cpp +++ b/map/map_tests/bookmarks_test.cpp @@ -200,15 +200,19 @@ UNIT_TEST(Bookmarks_ExportKML) namespace { - // Call this function to delete test category files. - void DeleteCategoryFiles() + template 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); +}