summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormattm@chromium.org <mattm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-08-26 01:19:43 +0000
committermattm@chromium.org <mattm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-08-26 01:19:43 +0000
commit96690a420b73c1b5a4ae350a1a13963b90dabbec (patch)
tree7870d1347381cb41be4be98fa3a29ddd67629859
parent6ee15630603b46b24c02dcf4d4cf8b8412af52aa (diff)
downloadchromium_src-96690a420b73c1b5a4ae350a1a13963b90dabbec.zip
chromium_src-96690a420b73c1b5a4ae350a1a13963b90dabbec.tar.gz
chromium_src-96690a420b73c1b5a4ae350a1a13963b90dabbec.tar.bz2
Fix cases that initialized StringTokenizer with a temporary.
Fix examples in StringTokenizer header that recommended doing that. BUG=none TEST=on linux, open options, click proxy configuration button a bunch. It should not fail intermittently. Review URL: http://codereview.chromium.org/174490 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@24398 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--base/string_tokenizer.h15
-rw-r--r--chrome/browser/gtk/options/advanced_contents_gtk.cc4
-rw-r--r--chrome/browser/shell_integration_linux.cc3
3 files changed, 18 insertions, 4 deletions
diff --git a/base/string_tokenizer.h b/base/string_tokenizer.h
index 654e7c9..eb9ac2d 100644
--- a/base/string_tokenizer.h
+++ b/base/string_tokenizer.h
@@ -12,10 +12,18 @@
// refer to the next token in the input string. The user may optionally
// configure the tokenizer to return delimiters.
//
+// Warning: be careful not to pass a C string into the 2-arg constructor:
+// StringTokenizer t("this is a test", " "); // WRONG
+// This will create a temporary std::string, save the begin() and end()
+// iterators, and then the string will be freed before we actually start
+// tokenizing it.
+// Instead, use a std::string or use the 3 arg constructor of CStringTokenizer.
+//
//
// EXAMPLE 1:
//
-// StringTokenizer t("this is a test", " ");
+// char input[] = "this is a test";
+// CStringTokenizer t(input, input + strlen(input), " ");
// while (t.GetNext()) {
// printf("%s\n", t.token().c_str());
// }
@@ -30,7 +38,8 @@
//
// EXAMPLE 2:
//
-// StringTokenizer t("no-cache=\"foo, bar\", private", ", ");
+// std::string input = "no-cache=\"foo, bar\", private";
+// StringTokenizer t(input, ", ");
// t.set_quote_chars("\"");
// while (t.GetNext()) {
// printf("%s\n", t.token().c_str());
@@ -85,6 +94,8 @@ class StringTokenizerT {
RETURN_DELIMS = 1 << 0,
};
+ // The string object must live longer than the tokenizer. (In particular this
+ // should not be constructed with a temporary.)
StringTokenizerT(const str& string,
const str& delims) {
Init(string.begin(), string.end(), delims);
diff --git a/chrome/browser/gtk/options/advanced_contents_gtk.cc b/chrome/browser/gtk/options/advanced_contents_gtk.cc
index c50e77c..891fd71 100644
--- a/chrome/browser/gtk/options/advanced_contents_gtk.cc
+++ b/chrome/browser/gtk/options/advanced_contents_gtk.cc
@@ -411,8 +411,10 @@ void NetworkSection::OnChangeProxiesButtonClicked(GtkButton *button,
bool NetworkSection::SearchPATH(ProxyConfigCommand* commands, size_t ncommands,
size_t* index) {
const char* path = getenv("PATH");
+ if (!path)
+ return false;
FilePath bin_path;
- StringTokenizer tk(path, ":");
+ CStringTokenizer tk(path, path + strlen(path), ":");
// Search $PATH looking for the commands in order.
while (tk.GetNext()) {
for (size_t i = 0; i < ncommands; i++) {
diff --git a/chrome/browser/shell_integration_linux.cc b/chrome/browser/shell_integration_linux.cc
index 8ecc0da..ea38c57 100644
--- a/chrome/browser/shell_integration_linux.cc
+++ b/chrome/browser/shell_integration_linux.cc
@@ -54,7 +54,8 @@ bool GetDesktopShortcutTemplate(std::string* output) {
const char* xdg_data_dirs = getenv("XDG_DATA_DIRS");
if (xdg_data_dirs) {
- StringTokenizer tokenizer(xdg_data_dirs, ":");
+ CStringTokenizer tokenizer(xdg_data_dirs,
+ xdg_data_dirs + strlen(xdg_data_dirs), ":");
while (tokenizer.GetNext()) {
search_paths.push_back(tokenizer.token());
}