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