| 6 |
| 7 | #include <errno.h> |
| 8 | #include <stddef.h> |
| 9 |
| 10 | #include <vector> |
| 11 |
| 12 | #include "base/scoped_clear_last_error.h" |
| 13 | #include "base/stl_util.h" |
| 14 | #include "base/strings/string_util.h" |
| 15 | #include "base/strings/utf_string_conversions.h" |
| 16 | #include "build/build_config.h" |
| 17 |
| 18 | namespace base { |
| 19 |
| 20 | namespace { |
| 21 |
| 22 | // Overloaded wrappers around vsnprintf and vswprintf. The buf_size parameter |
| 23 | // is the size of the buffer. These return the number of characters in the |
| 24 | // formatted string excluding the NUL terminator. If the buffer is not |
| 25 | // large enough to accommodate the formatted string without truncation, they |
| 26 | // return the number of characters that would be in the fully-formatted string |
| 27 | // (vsnprintf, and vswprintf on Windows), or -1 (vswprintf on POSIX platforms). |
| 28 | inline int vsnprintfT(char* buffer, |
| 29 | size_t buf_size, |
| 30 | const char* format, |
| 31 | va_list argptr) { |
| 32 | return base::vsnprintf(buffer, buf_size, format, argptr); |
| 33 | } |
| 34 |
| 35 | #if defined(OS_WIN) |
| 36 | inline int vsnprintfT(wchar_t* buffer, |
| 37 | size_t buf_size, |
| 38 | const wchar_t* format, |
| 39 | va_list argptr) { |
| 40 | return base::vswprintf(buffer, buf_size, format, argptr); |
| 41 | } |
| 42 | inline int vsnprintfT(char16_t* buffer, |
| 43 | size_t buf_size, |
| 44 | const char16_t* format, |
| 45 | va_list argptr) { |
| 46 | return base::vswprintf(reinterpret_cast<wchar_t*>(buffer), buf_size, |
| 47 | reinterpret_cast<const wchar_t*>(format), argptr); |
| 48 | } |
| 49 | #endif |
| 50 |
| 51 | // Templatized backend for StringPrintF/StringAppendF. This does not finalize |
| 52 | // the va_list, the caller is expected to do that. |
| 53 | template <class CharT> |
| 54 | static void StringAppendVT(std::basic_string<CharT>* dst, |
| 55 | const CharT* format, |
| 56 | va_list ap) { |
| 57 | // First try with a small fixed size buffer. |
| 58 | // This buffer size should be kept in sync with StringUtilTest.GrowBoundary |
| 59 | // and StringUtilTest.StringPrintfBounds. |
| 60 | CharT stack_buf[1024]; |
| 61 |
| 62 | va_list ap_copy; |
| 63 | va_copy(ap_copy, ap); |
| 64 |
| 65 | base::internal::ScopedClearLastError last_error; |
| 66 | int result = vsnprintfT(stack_buf, base::size(stack_buf), format, ap_copy); |
| 67 | va_end(ap_copy); |
| 68 |
| 69 | if (result >= 0 && result < static_cast<int>(base::size(stack_buf))) { |
| 70 | // It fit. |
| 71 | dst->append(stack_buf, result); |
| 72 | return; |
| 73 | } |
| 74 |
| 75 | // Repeatedly increase buffer size until it fits. |
| 76 | int mem_length = base::size(stack_buf); |
| 77 | while (true) { |
| 78 | if (result < 0) { |
| 79 | #if defined(OS_WIN) |
| 80 | // On Windows, vsnprintfT always returns the number of characters in a |
| 81 | // fully-formatted string, so if we reach this point, something else is |
| 82 | // wrong and no amount of buffer-doubling is going to fix it. |
| 83 | return; |
| 84 | #else |
| 85 | if (errno != 0 && errno != EOVERFLOW) |
| 86 | return; |
| 87 | // Try doubling the buffer size. |
| 88 | mem_length *= 2; |
| 89 | #endif |
| 90 | } else { |
| 91 | // We need exactly "result + 1" characters. |
| 92 | mem_length = result + 1; |
| 93 | } |
| 94 |
| 95 | if (mem_length > 32 * 1024 * 1024) { |
| 96 | // That should be plenty, don't try anything larger. This protects |
| 97 | // against huge allocations when using vsnprintfT implementations that |
| 98 | // return -1 for reasons other than overflow without setting errno. |
| 99 | DLOG(WARNING) << "Unable to printf the requested string due to size."; |
| 100 | return; |
| 101 | } |
| 102 |
| 103 | std::vector<CharT> mem_buf(mem_length); |
| 104 |
| 105 | // NOTE: You can only use a va_list once. Since we're in a while loop, we |
| 106 | // need to make a new copy each time so we don't use up the original. |
| 107 | va_copy(ap_copy, ap); |
| 108 | result = vsnprintfT(&mem_buf[0], mem_length, format, ap_copy); |
| 109 | va_end(ap_copy); |
| 110 |
| 111 | if ((result >= 0) && (result < mem_length)) { |
| 112 | // It fit. |
| 113 | dst->append(&mem_buf[0], result); |
| 114 | return; |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 |
| 119 | } // namespace |
| 120 |
| 121 | std::string StringPrintf(const char* format, ...) { |
| 122 | va_list ap; |
| 123 | va_start(ap, format); |
| 124 | std::string result; |
| 125 | StringAppendV(&result, format, ap); |
| 126 | va_end(ap); |
| 127 | return result; |
| 128 | } |
| 129 |
| 130 | #if defined(OS_WIN) |
| 131 | std::wstring StringPrintf(const wchar_t* format, ...) { |
| 132 | va_list ap; |
| 133 | va_start(ap, format); |
| 134 | std::wstring result; |
| 135 | StringAppendV(&result, format, ap); |
| 136 | va_end(ap); |
| 137 | return result; |
| 138 | } |
| 139 |
| 140 | std::u16string StringPrintf(const char16_t* format, ...) { |
| 141 | va_list ap; |
| 142 | va_start(ap, format); |
| 143 | std::u16string result; |
| 144 | StringAppendV(&result, format, ap); |
| 145 | va_end(ap); |
| 146 | return result; |
| 147 | } |
| 148 | #endif |
| 149 |
| 150 | std::string StringPrintV(const char* format, va_list ap) { |
| 151 | std::string result; |
| 152 | StringAppendV(&result, format, ap); |
| 153 | return result; |
| 154 | } |
| 155 |
| 156 | const std::string& SStringPrintf(std::string* dst, const char* format, ...) { |
| 157 | va_list ap; |
| 158 | va_start(ap, format); |
| 159 | dst->clear(); |
| 160 | StringAppendV(dst, format, ap); |
| 161 | va_end(ap); |
| 162 | return *dst; |
| 163 | } |
| 164 |
| 165 | #if defined(OS_WIN) |
| 166 | const std::wstring& SStringPrintf(std::wstring* dst, |
| 167 | const wchar_t* format, ...) { |
| 168 | va_list ap; |
| 169 | va_start(ap, format); |
| 170 | dst->clear(); |
| 171 | StringAppendV(dst, format, ap); |
| 172 | va_end(ap); |
| 173 | return *dst; |
| 174 | } |
| 175 |
| 176 | const std::u16string& SStringPrintf(std::u16string* dst, |
| 177 | const char16_t* format, |
| 178 | ...) { |
| 179 | va_list ap; |
| 180 | va_start(ap, format); |
| 181 | dst->clear(); |
| 182 | StringAppendV(dst, format, ap); |
| 183 | va_end(ap); |
| 184 | return *dst; |
| 185 | } |
| 186 | #endif |
| 187 |
| 188 | void StringAppendF(std::string* dst, const char* format, ...) { |
| 189 | va_list ap; |
| 190 | va_start(ap, format); |
| 191 | StringAppendV(dst, format, ap); |
| 192 | va_end(ap); |
| 193 | } |
| 194 |
| 195 | #if defined(OS_WIN) |
| 196 | void StringAppendF(std::wstring* dst, const wchar_t* format, ...) { |
| 197 | va_list ap; |
| 198 | va_start(ap, format); |
| 199 | StringAppendV(dst, format, ap); |
| 200 | va_end(ap); |
| 201 | } |
| 202 |
| 203 | void StringAppendF(std::u16string* dst, const char16_t* format, ...) { |
| 204 | va_list ap; |
| 205 | va_start(ap, format); |
| 206 | StringAppendV(dst, format, ap); |
| 207 | va_end(ap); |
| 208 | } |
| 209 | #endif |
| 210 |
| 211 | void StringAppendV(std::string* dst, const char* format, va_list ap) { |
| 212 | StringAppendVT(dst, format, ap); |
| 213 | } |
| 214 |
| 215 | #if defined(OS_WIN) |
| 216 | void StringAppendV(std::wstring* dst, const wchar_t* format, va_list ap) { |
| 217 | StringAppendVT(dst, format, ap); |
| 218 | } |
| 219 |
| 220 | void StringAppendV(std::u16string* dst, const char16_t* format, va_list ap) { |
| 221 | StringAppendVT(dst, format, ap); |
| 222 | } |
| 223 | #endif |
| 224 |
| 225 | } // namespace base |
| 226 |