MCPcopy Create free account
hub / github.com/devsisters/libquic / StringAppendVT

Function StringAppendVT

src/base/strings/stringprintf.cc:47–112  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

45// the va_list, the caller is expected to do that.
46template <class StringType>
47static void StringAppendVT(StringType* dst,
48 const typename StringType::value_type* format,
49 va_list ap) {
50 // First try with a small fixed size buffer.
51 // This buffer size should be kept in sync with StringUtilTest.GrowBoundary
52 // and StringUtilTest.StringPrintfBounds.
53 typename StringType::value_type stack_buf[1024];
54
55 va_list ap_copy;
56 va_copy(ap_copy, ap);
57
58#if !defined(OS_WIN)
59 ScopedClearErrno clear_errno;
60#endif
61 int result = vsnprintfT(stack_buf, arraysize(stack_buf), format, ap_copy);
62 va_end(ap_copy);
63
64 if (result >= 0 && result < static_cast<int>(arraysize(stack_buf))) {
65 // It fit.
66 dst->append(stack_buf, result);
67 return;
68 }
69
70 // Repeatedly increase buffer size until it fits.
71 int mem_length = arraysize(stack_buf);
72 while (true) {
73 if (result < 0) {
74#if defined(OS_WIN)
75 // On Windows, vsnprintfT always returns the number of characters in a
76 // fully-formatted string, so if we reach this point, something else is
77 // wrong and no amount of buffer-doubling is going to fix it.
78 return;
79#else
80 if (errno != 0 && errno != EOVERFLOW)
81 return;
82 // Try doubling the buffer size.
83 mem_length *= 2;
84#endif
85 } else {
86 // We need exactly "result + 1" characters.
87 mem_length = result + 1;
88 }
89
90 if (mem_length > 32 * 1024 * 1024) {
91 // That should be plenty, don't try anything larger. This protects
92 // against huge allocations when using vsnprintfT implementations that
93 // return -1 for reasons other than overflow without setting errno.
94 DLOG(WARNING) << "Unable to printf the requested string due to size.";
95 return;
96 }
97
98 std::vector<typename StringType::value_type> mem_buf(mem_length);
99
100 // NOTE: You can only use a va_list once. Since we're in a while loop, we
101 // need to make a new copy each time so we don't use up the original.
102 va_copy(ap_copy, ap);
103 result = vsnprintfT(&mem_buf[0], mem_length, format, ap_copy);
104 va_end(ap_copy);

Callers 1

StringAppendVFunction · 0.85

Calls 1

vsnprintfTFunction · 0.85

Tested by

no test coverage detected