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