1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2012 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
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
14 // distribution.
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.
18 //
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.
30
31 // from google3/base/stringprintf.cc
32
33 #include <google/protobuf/stubs/stringprintf.h>
34
35 #include <errno.h>
36 #include <stdarg.h> // For va_list and related operations
37 #include <stdio.h> // MSVC requires this for _vsnprintf
38 #include <vector>
39
40 #include <google/protobuf/stubs/common.h>
41 #include <google/protobuf/stubs/logging.h>
42
43 namespace google {
44 namespace protobuf {
45
46 #ifdef _MSC_VER
47 #ifndef va_copy
48 // Define va_copy for MSVC. This is a hack, assuming va_list is simply a
49 // pointer into the stack and is safe to copy.
50 #define va_copy(dest, src) ((dest) = (src))
51 #endif
52 #endif
53
StringAppendV(std::string * dst,const char * format,va_list ap)54 void StringAppendV(std::string* dst, const char* format, va_list ap) {
55 // First try with a small fixed size buffer
56 static const int kSpaceLength = 1024;
57 char space[kSpaceLength];
58
59 // It's possible for methods that use a va_list to invalidate
60 // the data in it upon use. The fix is to make a copy
61 // of the structure before using it and use that copy instead.
62 va_list backup_ap;
63 va_copy(backup_ap, ap);
64 int result = vsnprintf(space, kSpaceLength, format, backup_ap);
65 va_end(backup_ap);
66
67 if (result < kSpaceLength) {
68 if (result >= 0) {
69 // Normal case -- everything fit.
70 dst->append(space, result);
71 return;
72 }
73
74 #ifdef _MSC_VER
75 {
76 // Error or MSVC running out of space. MSVC 8.0 and higher
77 // can be asked about space needed with the special idiom below:
78 va_copy(backup_ap, ap);
79 result = vsnprintf(nullptr, 0, format, backup_ap);
80 va_end(backup_ap);
81 }
82 #endif
83
84 if (result < 0) {
85 // Just an error.
86 return;
87 }
88 }
89
90 // Increase the buffer size to the size requested by vsnprintf,
91 // plus one for the closing \0.
92 int length = result+1;
93 char* buf = new char[length];
94
95 // Restore the va_list before we use it again
96 va_copy(backup_ap, ap);
97 result = vsnprintf(buf, length, format, backup_ap);
98 va_end(backup_ap);
99
100 if (result >= 0 && result < length) {
101 // It fit
102 dst->append(buf, result);
103 }
104 delete[] buf;
105 }
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
SStringPrintf(std::string * dst,const char * format,...)116 const std::string& SStringPrintf(std::string* dst, const char* format, ...) {
117 va_list ap;
118 va_start(ap, format);
119 dst->clear();
120 StringAppendV(dst, format, ap);
121 va_end(ap);
122 return *dst;
123 }
124
StringAppendF(std::string * dst,const char * format,...)125 void StringAppendF(std::string* dst, const char* format, ...) {
126 va_list ap;
127 va_start(ap, format);
128 StringAppendV(dst, format, ap);
129 va_end(ap);
130 }
131
132 // Max arguments supported by StringPrintVector
133 const int kStringPrintfVectorMaxArgs = 32;
134
135 // An empty block of zero for filler arguments. This is const so that if
136 // printf tries to write to it (via %n) then the program gets a SIGSEGV
137 // and we can fix the problem or protect against an attack.
138 static const char string_printf_empty_block[256] = { '\0' };
139
StringPrintfVector(const char * format,const std::vector<std::string> & v)140 std::string StringPrintfVector(const char* format,
141 const std::vector<std::string>& v) {
142 GOOGLE_CHECK_LE(v.size(), kStringPrintfVectorMaxArgs)
143 << "StringPrintfVector currently only supports up to "
144 << kStringPrintfVectorMaxArgs << " arguments. "
145 << "Feel free to add support for more if you need it.";
146
147 // Add filler arguments so that bogus format+args have a harder time
148 // crashing the program, corrupting the program (%n),
149 // or displaying random chunks of memory to users.
150
151 const char* cstr[kStringPrintfVectorMaxArgs];
152 for (int i = 0; i < v.size(); ++i) {
153 cstr[i] = v[i].c_str();
154 }
155 for (int i = v.size(); i < GOOGLE_ARRAYSIZE(cstr); ++i) {
156 cstr[i] = &string_printf_empty_block[0];
157 }
158
159 // I do not know any way to pass kStringPrintfVectorMaxArgs arguments,
160 // or any way to build a va_list by hand, or any API for printf
161 // that accepts an array of arguments. The best I can do is stick
162 // this COMPILE_ASSERT right next to the actual statement.
163
164 static_assert(kStringPrintfVectorMaxArgs == 32, "arg_count_mismatch");
165 return StringPrintf(format,
166 cstr[0], cstr[1], cstr[2], cstr[3], cstr[4],
167 cstr[5], cstr[6], cstr[7], cstr[8], cstr[9],
168 cstr[10], cstr[11], cstr[12], cstr[13], cstr[14],
169 cstr[15], cstr[16], cstr[17], cstr[18], cstr[19],
170 cstr[20], cstr[21], cstr[22], cstr[23], cstr[24],
171 cstr[25], cstr[26], cstr[27], cstr[28], cstr[29],
172 cstr[30], cstr[31]);
173 }
174 } // namespace protobuf
175 } // namespace google
176