• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <vector>
11 
12 #include "base/scoped_clear_errno.h"
13 #include "base/strings/string_util.h"
14 #include "build/build_config.h"
15 
16 namespace base {
17 
18 namespace {
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).
vsnprintfT(char * buffer,size_t buf_size,const char * format,va_list argptr)26 inline int vsnprintfT(char* buffer,
27                       size_t buf_size,
28                       const char* format,
29                       va_list argptr) {
30 #pragma GCC diagnostic push
31 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
32   return base::vsnprintf(buffer, buf_size, format, argptr);
33 #pragma GCC diagnostic pop
34 }
35 
36 // Templatized backend for StringPrintF/StringAppendF. This does not finalize
37 // the va_list, the caller is expected to do that.
38 template <class StringType>
StringAppendVT(StringType * dst,const typename StringType::value_type * format,va_list ap)39 static 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 
StringPrintf(const char * format,...)108 std::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 
StringPrintV(const char * format,va_list ap)117 std::string StringPrintV(const char* format, va_list ap) {
118   std::string result;
119   StringAppendV(&result, format, ap);
120   return result;
121 }
122 
SStringPrintf(std::string * dst,const char * format,...)123 const 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 
StringAppendF(std::string * dst,const char * format,...)132 void 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 
StringAppendV(std::string * dst,const char * format,va_list ap)139 void StringAppendV(std::string* dst, const char* format, va_list ap) {
140   StringAppendVT(dst, format, ap);
141 }
142 
143 }  // namespace base
144