1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2012 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 // from google3/base/stringprintf.cc
33 #include <google/protobuf/stubs/stringprintf.h>
36 #include <stdarg.h> // For va_list and related operations
37 #include <stdio.h> // MSVC requires this for _vsnprintf
40 #include <google/protobuf/stubs/common.h>
41 #include <google/protobuf/stubs/logging.h>
47 enum { IS_COMPILER_MSVC = 1 };
49 // Define va_copy for MSVC. This is a hack, assuming va_list is simply a
50 // pointer into the stack and is safe to copy.
51 #define va_copy(dest, src) ((dest) = (src))
54 enum { IS_COMPILER_MSVC = 0 };
57 void StringAppendV(string* dst, const char* format, va_list ap) {
58 // First try with a small fixed size buffer
59 static const int kSpaceLength = 1024;
60 char space[kSpaceLength];
62 // It's possible for methods that use a va_list to invalidate
63 // the data in it upon use. The fix is to make a copy
64 // of the structure before using it and use that copy instead.
66 va_copy(backup_ap, ap);
67 int result = vsnprintf(space, kSpaceLength, format, backup_ap);
70 if (result < kSpaceLength) {
72 // Normal case -- everything fit.
73 dst->append(space, result);
77 if (IS_COMPILER_MSVC) {
78 // Error or MSVC running out of space. MSVC 8.0 and higher
79 // can be asked about space needed with the special idiom below:
80 va_copy(backup_ap, ap);
81 result = vsnprintf(nullptr, 0, format, backup_ap);
91 // Increase the buffer size to the size requested by vsnprintf,
92 // plus one for the closing \0.
93 int length = result+1;
94 char* buf = new char[length];
96 // Restore the va_list before we use it again
97 va_copy(backup_ap, ap);
98 result = vsnprintf(buf, length, format, backup_ap);
101 if (result >= 0 && result < length) {
103 dst->append(buf, result);
109 string StringPrintf(const char* format, ...) {
111 va_start(ap, format);
113 StringAppendV(&result, format, ap);
118 const string& SStringPrintf(string* dst, const char* format, ...) {
120 va_start(ap, format);
122 StringAppendV(dst, format, ap);
127 void StringAppendF(string* dst, const char* format, ...) {
129 va_start(ap, format);
130 StringAppendV(dst, format, ap);
134 // Max arguments supported by StringPrintVector
135 const int kStringPrintfVectorMaxArgs = 32;
137 // An empty block of zero for filler arguments. This is const so that if
138 // printf tries to write to it (via %n) then the program gets a SIGSEGV
139 // and we can fix the problem or protect against an attack.
140 static const char string_printf_empty_block[256] = { '\0' };
142 string StringPrintfVector(const char* format, const std::vector<string>& v) {
143 GOOGLE_CHECK_LE(v.size(), kStringPrintfVectorMaxArgs)
144 << "StringPrintfVector currently only supports up to "
145 << kStringPrintfVectorMaxArgs << " arguments. "
146 << "Feel free to add support for more if you need it.";
148 // Add filler arguments so that bogus format+args have a harder time
149 // crashing the program, corrupting the program (%n),
150 // or displaying random chunks of memory to users.
152 const char* cstr[kStringPrintfVectorMaxArgs];
153 for (int i = 0; i < v.size(); ++i) {
154 cstr[i] = v[i].c_str();
156 for (int i = v.size(); i < GOOGLE_ARRAYSIZE(cstr); ++i) {
157 cstr[i] = &string_printf_empty_block[0];
160 // I do not know any way to pass kStringPrintfVectorMaxArgs arguments,
161 // or any way to build a va_list by hand, or any API for printf
162 // that accepts an array of arguments. The best I can do is stick
163 // this COMPILE_ASSERT right next to the actual statement.
165 GOOGLE_COMPILE_ASSERT(kStringPrintfVectorMaxArgs == 32, arg_count_mismatch);
166 return StringPrintf(format,
167 cstr[0], cstr[1], cstr[2], cstr[3], cstr[4],
168 cstr[5], cstr[6], cstr[7], cstr[8], cstr[9],
169 cstr[10], cstr[11], cstr[12], cstr[13], cstr[14],
170 cstr[15], cstr[16], cstr[17], cstr[18], cstr[19],
171 cstr[20], cstr[21], cstr[22], cstr[23], cstr[24],
172 cstr[25], cstr[26], cstr[27], cstr[28], cstr[29],
175 } // namespace protobuf
176 } // namespace google