blob: 83fdfe454e9034e36d946dabbddca4f19be62edf [file] [log] [blame]
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +00001// Protocol Buffers - Google's data interchange format
2// Copyright 2012 Google Inc. All rights reserved.
Feng Xiaoe4288622014-10-01 16:26:23 -07003// https://developers.google.com/protocol-buffers/
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +00004//
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#include <google/protobuf/stubs/common.h>
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +000040
41namespace google {
42namespace protobuf {
43
44#ifdef _MSC_VER
45enum { IS_COMPILER_MSVC = 1 };
xiaofeng@google.comfcb8a502012-09-24 06:48:20 +000046#ifndef va_copy
47// Define va_copy for MSVC. This is a hack, assuming va_list is simply a
48// pointer into the stack and is safe to copy.
49#define va_copy(dest, src) ((dest) = (src))
50#endif
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +000051#else
52enum { IS_COMPILER_MSVC = 0 };
53#endif
54
55void StringAppendV(string* dst, const char* format, va_list ap) {
56 // First try with a small fixed size buffer
57 static const int kSpaceLength = 1024;
58 char space[kSpaceLength];
59
60 // It's possible for methods that use a va_list to invalidate
61 // the data in it upon use. The fix is to make a copy
62 // of the structure before using it and use that copy instead.
63 va_list backup_ap;
64 va_copy(backup_ap, ap);
65 int result = vsnprintf(space, kSpaceLength, format, backup_ap);
66 va_end(backup_ap);
67
68 if (result < kSpaceLength) {
69 if (result >= 0) {
70 // Normal case -- everything fit.
71 dst->append(space, result);
72 return;
73 }
74
75 if (IS_COMPILER_MSVC) {
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(NULL, 0, format, backup_ap);
80 va_end(backup_ap);
81 }
82
83 if (result < 0) {
84 // Just an error.
85 return;
86 }
87 }
88
89 // Increase the buffer size to the size requested by vsnprintf,
90 // plus one for the closing \0.
91 int length = result+1;
92 char* buf = new char[length];
93
94 // Restore the va_list before we use it again
95 va_copy(backup_ap, ap);
96 result = vsnprintf(buf, length, format, backup_ap);
97 va_end(backup_ap);
98
99 if (result >= 0 && result < length) {
100 // It fit
101 dst->append(buf, result);
102 }
103 delete[] buf;
104}
105
106
107string StringPrintf(const char* format, ...) {
108 va_list ap;
109 va_start(ap, format);
110 string result;
111 StringAppendV(&result, format, ap);
112 va_end(ap);
113 return result;
114}
115
116const string& SStringPrintf(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
125void StringAppendF(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
133const 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.
138static const char string_printf_empty_block[256] = { '\0' };
139
140string StringPrintfVector(const char* format, const vector<string>& v) {
141 GOOGLE_CHECK_LE(v.size(), kStringPrintfVectorMaxArgs)
142 << "StringPrintfVector currently only supports up to "
143 << kStringPrintfVectorMaxArgs << " arguments. "
144 << "Feel free to add support for more if you need it.";
145
146 // Add filler arguments so that bogus format+args have a harder time
147 // crashing the program, corrupting the program (%n),
148 // or displaying random chunks of memory to users.
149
150 const char* cstr[kStringPrintfVectorMaxArgs];
151 for (int i = 0; i < v.size(); ++i) {
152 cstr[i] = v[i].c_str();
153 }
154 for (int i = v.size(); i < GOOGLE_ARRAYSIZE(cstr); ++i) {
155 cstr[i] = &string_printf_empty_block[0];
156 }
157
158 // I do not know any way to pass kStringPrintfVectorMaxArgs arguments,
159 // or any way to build a va_list by hand, or any API for printf
160 // that accepts an array of arguments. The best I can do is stick
161 // this COMPILE_ASSERT right next to the actual statement.
162
163 GOOGLE_COMPILE_ASSERT(kStringPrintfVectorMaxArgs == 32, arg_count_mismatch);
164 return StringPrintf(format,
165 cstr[0], cstr[1], cstr[2], cstr[3], cstr[4],
166 cstr[5], cstr[6], cstr[7], cstr[8], cstr[9],
167 cstr[10], cstr[11], cstr[12], cstr[13], cstr[14],
168 cstr[15], cstr[16], cstr[17], cstr[18], cstr[19],
169 cstr[20], cstr[21], cstr[22], cstr[23], cstr[24],
170 cstr[25], cstr[26], cstr[27], cstr[28], cstr[29],
171 cstr[30], cstr[31]);
172}
173} // namespace protobuf
174} // namespace google