Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(609)

Unified Diff: base/string_tokenizer.h

Issue 174490: Fix cases that initialized StringTokenizer with a temporary. (Closed)
Patch Set: check for null path Created 11 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | chrome/browser/gtk/options/advanced_contents_gtk.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/string_tokenizer.h
diff --git a/base/string_tokenizer.h b/base/string_tokenizer.h
index 654e7c96146c8c6bb6d9e8a6d3d6461077bc8f05..eb9ac2d2081a0de282545e9adcdac4973fcd4d8e 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);
« no previous file with comments | « no previous file | chrome/browser/gtk/options/advanced_contents_gtk.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698