stringprintf.cc 6.0 KB

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