blob: 5f1ad9bd48af5ebc993a60064e5629ea38a5299a [file] [log] [blame]
Vitaly Bukacbed2062015-08-17 12:54:05 -07001// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/strings/stringprintf.h"
6
7#include <errno.h>
8
9#include <vector>
10
Vitaly Buka8750b272015-08-18 18:39:08 -070011#include "base/logging.h"
Vitaly Bukacbed2062015-08-17 12:54:05 -070012#include "base/scoped_clear_errno.h"
13#include "base/strings/string_util.h"
Vitaly Bukacbed2062015-08-17 12:54:05 -070014
15namespace base {
16
17namespace {
18
19// Overloaded wrappers around vsnprintf and vswprintf. The buf_size parameter
20// is the size of the buffer. These return the number of characters in the
21// formatted string excluding the NUL terminator. If the buffer is not
22// large enough to accommodate the formatted string without truncation, they
23// return the number of characters that would be in the fully-formatted string
24// (vsnprintf, and vswprintf on Windows), or -1 (vswprintf on POSIX platforms).
25inline int vsnprintfT(char* buffer,
26 size_t buf_size,
27 const char* format,
28 va_list argptr) {
29 return base::vsnprintf(buffer, buf_size, format, argptr);
30}
31
Vitaly Bukacbed2062015-08-17 12:54:05 -070032// Templatized backend for StringPrintF/StringAppendF. This does not finalize
33// the va_list, the caller is expected to do that.
34template <class StringType>
35static void StringAppendVT(StringType* dst,
36 const typename StringType::value_type* format,
37 va_list ap) {
38 // First try with a small fixed size buffer.
39 // This buffer size should be kept in sync with StringUtilTest.GrowBoundary
40 // and StringUtilTest.StringPrintfBounds.
41 typename StringType::value_type stack_buf[1024];
42
43 va_list ap_copy;
44 va_copy(ap_copy, ap);
45
46#if !defined(OS_WIN)
47 ScopedClearErrno clear_errno;
48#endif
49 int result = vsnprintfT(stack_buf, arraysize(stack_buf), format, ap_copy);
50 va_end(ap_copy);
51
52 if (result >= 0 && result < static_cast<int>(arraysize(stack_buf))) {
53 // It fit.
54 dst->append(stack_buf, result);
55 return;
56 }
57
58 // Repeatedly increase buffer size until it fits.
59 int mem_length = arraysize(stack_buf);
60 while (true) {
61 if (result < 0) {
62#if defined(OS_WIN)
63 // On Windows, vsnprintfT always returns the number of characters in a
64 // fully-formatted string, so if we reach this point, something else is
65 // wrong and no amount of buffer-doubling is going to fix it.
66 return;
67#else
68 if (errno != 0 && errno != EOVERFLOW)
69 return;
70 // Try doubling the buffer size.
71 mem_length *= 2;
72#endif
73 } else {
74 // We need exactly "result + 1" characters.
75 mem_length = result + 1;
76 }
77
78 if (mem_length > 32 * 1024 * 1024) {
79 // That should be plenty, don't try anything larger. This protects
80 // against huge allocations when using vsnprintfT implementations that
81 // return -1 for reasons other than overflow without setting errno.
82 DLOG(WARNING) << "Unable to printf the requested string due to size.";
83 return;
84 }
85
86 std::vector<typename StringType::value_type> mem_buf(mem_length);
87
88 // NOTE: You can only use a va_list once. Since we're in a while loop, we
89 // need to make a new copy each time so we don't use up the original.
90 va_copy(ap_copy, ap);
91 result = vsnprintfT(&mem_buf[0], mem_length, format, ap_copy);
92 va_end(ap_copy);
93
94 if ((result >= 0) && (result < mem_length)) {
95 // It fit.
96 dst->append(&mem_buf[0], result);
97 return;
98 }
99 }
100}
101
102} // namespace
103
104std::string StringPrintf(const char* format, ...) {
105 va_list ap;
106 va_start(ap, format);
107 std::string result;
108 StringAppendV(&result, format, ap);
109 va_end(ap);
110 return result;
111}
112
Vitaly Bukacbed2062015-08-17 12:54:05 -0700113std::string StringPrintV(const char* format, va_list ap) {
114 std::string result;
115 StringAppendV(&result, format, ap);
116 return result;
117}
118
119const std::string& SStringPrintf(std::string* dst, const char* format, ...) {
120 va_list ap;
121 va_start(ap, format);
122 dst->clear();
123 StringAppendV(dst, format, ap);
124 va_end(ap);
125 return *dst;
126}
127
Vitaly Bukacbed2062015-08-17 12:54:05 -0700128void StringAppendF(std::string* dst, const char* format, ...) {
129 va_list ap;
130 va_start(ap, format);
131 StringAppendV(dst, format, ap);
132 va_end(ap);
133}
134
Vitaly Bukacbed2062015-08-17 12:54:05 -0700135void StringAppendV(std::string* dst, const char* format, va_list ap) {
136 StringAppendVT(dst, format, ap);
137}
138
Vitaly Bukacbed2062015-08-17 12:54:05 -0700139} // namespace base