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

Unified Diff: base/string_util.cc

Issue 174342: Fix a bug where we reused a va_list across multiple calls. (Closed)
Patch Set: comment 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 | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/string_util.cc
diff --git a/base/string_util.cc b/base/string_util.cc
index f58f105ee1653b6a9ec83948d9243b3073c300bf..1edc0d98ac74dcbb6e2724bea3f18d3f28a1f0cd 100644
--- a/base/string_util.cc
+++ b/base/string_util.cc
@@ -943,14 +943,14 @@ static void StringAppendVT(StringType* dst,
// and StringUtilTest.StringPrintfBounds.
typename StringType::value_type stack_buf[1024];
- va_list backup_ap;
- GG_VA_COPY(backup_ap, ap);
+ va_list ap_copy;
+ GG_VA_COPY(ap_copy, ap);
#if !defined(OS_WIN)
errno = 0;
#endif
- int result = vsnprintfT(stack_buf, arraysize(stack_buf), format, backup_ap);
- va_end(backup_ap);
+ int result = vsnprintfT(stack_buf, arraysize(stack_buf), format, ap_copy);
+ va_end(ap_copy);
if (result >= 0 && result < static_cast<int>(arraysize(stack_buf))) {
// It fit.
@@ -990,11 +990,11 @@ static void StringAppendVT(StringType* dst,
std::vector<typename StringType::value_type> mem_buf(mem_length);
- // Restore the va_list before we use it again.
- GG_VA_COPY(backup_ap, ap);
-
- result = vsnprintfT(&mem_buf[0], mem_length, format, ap);
- va_end(backup_ap);
+ // NOTE: You can only use a va_list once. Since we're in a while loop, we
+ // need to make a new copy each time so we don't use up the original.
+ GG_VA_COPY(ap_copy, ap);
+ result = vsnprintfT(&mem_buf[0], mem_length, format, ap_copy);
Evan Martin 2009/08/24 20:28:50 (note for brett: aside from the variable rename, t
+ va_end(ap_copy);
if ((result >= 0) && (result < mem_length)) {
// It fit.
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698