diff options
author | thestig@chromium.org <thestig@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-07 19:55:55 +0000 |
---|---|---|
committer | thestig@chromium.org <thestig@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-07 19:55:55 +0000 |
commit | 9274ead1c74ef0025072923f14aeaedd2598c0d6 (patch) | |
tree | baf28919c2f144b9009b6ba8d4470707ad2da7b8 | |
parent | 5c311356f75f014502b63f1afa79c5452a0cfa59 (diff) | |
download | chromium_src-9274ead1c74ef0025072923f14aeaedd2598c0d6.zip chromium_src-9274ead1c74ef0025072923f14aeaedd2598c0d6.tar.gz chromium_src-9274ead1c74ef0025072923f14aeaedd2598c0d6.tar.bz2 |
gtk: Fix gtk_dnd_util::ExtractNamedURL to not crash on invalid data.
BUG=43347
TEST=included
Review URL: http://codereview.chromium.org/2003006
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@46731 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | app/app.gyp | 9 | ||||
-rw-r--r-- | app/gtk_dnd_util.cc | 7 | ||||
-rw-r--r-- | app/gtk_dnd_util_unittest.cc | 77 |
3 files changed, 89 insertions, 4 deletions
diff --git a/app/app.gyp b/app/app.gyp index 6b7b706..a7c328f 100644 --- a/app/app.gyp +++ b/app/app.gyp @@ -47,12 +47,12 @@ 'os_exchange_data_win_unittest.cc', 'run_all_unittests.cc', 'slide_animation_unittest.cc', - 'system_monitor_unittest.cc', - 'test_suite.h', - 'text_elider_unittest.cc', 'sql/connection_unittest.cc', 'sql/statement_unittest.cc', 'sql/transaction_unittest.cc', + 'system_monitor_unittest.cc', + 'test_suite.h', + 'text_elider_unittest.cc', 'tree_node_iterator_unittest.cc', 'win_util_unittest.cc', ], @@ -61,6 +61,9 @@ ], 'conditions': [ ['OS=="linux" or OS=="freebsd" or OS=="openbsd" or OS=="solaris"', { + 'sources': [ + 'gtk_dnd_util_unittest.cc', + ], 'dependencies': [ 'app_unittest_strings', '../build/linux/system.gyp:gtk', diff --git a/app/gtk_dnd_util.cc b/app/gtk_dnd_util.cc index 343e3df..38d171b 100644 --- a/app/gtk_dnd_util.cc +++ b/app/gtk_dnd_util.cc @@ -1,9 +1,11 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Copyright (c) 2010 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "app/gtk_dnd_util.h" +#include <string> + #include "base/logging.h" #include "base/pickle.h" #include "base/utf_string_conversions.h" @@ -200,6 +202,9 @@ void WriteURLWithName(GtkSelectionData* selection_data, bool ExtractNamedURL(GtkSelectionData* selection_data, GURL* url, string16* title) { + if (!selection_data || selection_data->length <= 0) + return false; + Pickle data(reinterpret_cast<char*>(selection_data->data), selection_data->length); void* iter = NULL; diff --git a/app/gtk_dnd_util_unittest.cc b/app/gtk_dnd_util_unittest.cc new file mode 100644 index 0000000..1de8991 --- /dev/null +++ b/app/gtk_dnd_util_unittest.cc @@ -0,0 +1,77 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include <string> + +#include "app/gtk_dnd_util.h" +#include "base/pickle.h" +#include "base/scoped_ptr.h" +#include "base/utf_string_conversions.h" +#include "googleurl/src/gurl.h" +#include "testing/gtest/include/gtest/gtest.h" + +TEST(GtkDndUtilTest, ExtractNamedURLValid) { + const std::string kTitle = "title"; + const std::string kUrl = "http://www.foobar.com/"; + Pickle pickle; + pickle.WriteString(kTitle); + pickle.WriteString(kUrl); + + GtkSelectionData data; + scoped_array<guchar> test_data(new guchar[pickle.size()]); + memcpy(test_data.get(), pickle.data(), pickle.size()); + data.data = test_data.get(); + data.length = pickle.size(); + + GURL url; + string16 title; + ASSERT_EQ(true, gtk_dnd_util::ExtractNamedURL(&data, &url, &title)); + EXPECT_EQ(UTF8ToUTF16(kTitle), title); + EXPECT_EQ(GURL(kUrl), url); +} + +TEST(GtkDndUtilTest, ExtractNamedURLInvalidURL) { + const std::string kTitle = "title"; + const std::string kBadUrl = "foobar"; + Pickle pickle; + pickle.WriteString(kTitle); + pickle.WriteString(kBadUrl); + + GtkSelectionData data; + scoped_array<guchar> test_data(new guchar[pickle.size()]); + memcpy(test_data.get(), pickle.data(), pickle.size()); + data.data = test_data.get(); + data.length = pickle.size(); + + GURL url; + string16 title; + EXPECT_EQ(false, gtk_dnd_util::ExtractNamedURL(&data, &url, &title)); +} + +TEST(GtkDndUtilTest, ExtractNamedURLInvalidInput) { + GURL url; + string16 title; + GtkSelectionData data; + data.data = NULL; + data.length = 0; + + EXPECT_EQ(false, gtk_dnd_util::ExtractNamedURL(&data, &url, &title)); + + guchar empty_data[] = ""; + data.data = empty_data; + data.length = 0; + + EXPECT_EQ(false, gtk_dnd_util::ExtractNamedURL(&data, &url, &title)); + + const std::string kTitle = "title"; + Pickle pickle; + pickle.WriteString(kTitle); + + scoped_array<guchar> test_data(new guchar[pickle.size()]); + memcpy(test_data.get(), pickle.data(), pickle.size()); + data.data = test_data.get(); + data.length = pickle.size(); + + EXPECT_EQ(false, gtk_dnd_util::ExtractNamedURL(&data, &url, &title)); +} |