|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2021, 2021 IBM Corp. and others |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under |
| 5 | + * the terms of the Eclipse Public License 2.0 which accompanies this |
| 6 | + * distribution and is available at http://eclipse.org/legal/epl-2.0 |
| 7 | + * or the Apache License, Version 2.0 which accompanies this distribution |
| 8 | + * and is available at https://www.apache.org/licenses/LICENSE-2.0. |
| 9 | + * |
| 10 | + * This Source Code may also be made available under the following Secondary |
| 11 | + * Licenses when the conditions for such availability set forth in the |
| 12 | + * Eclipse Public License, v. 2.0 are satisfied: GNU General Public License, |
| 13 | + * version 2 with the GNU Classpath Exception [1] and GNU General Public |
| 14 | + * License, version 2 with the OpenJDK Assembly Exception [2]. |
| 15 | + * |
| 16 | + * [1] https://www.gnu.org/software/classpath/license.html |
| 17 | + * [2] http://openjdk.java.net/legal/assembly-exception.html |
| 18 | + * |
| 19 | + * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception |
| 20 | + *******************************************************************************/ |
| 21 | + |
| 22 | +#include "infra/String.hpp" |
| 23 | +#include "infra/Assert.hpp" |
| 24 | +#include "env/FrontEnd.hpp" // defines va_copy for old MSVC and z/OS |
| 25 | +#include <limits.h> |
| 26 | + |
| 27 | +#if defined (_MSC_VER) && (_MSC_VER < 1900) |
| 28 | +#define stdlib_vsnprintf _vsnprintf |
| 29 | +#else |
| 30 | +#define stdlib_vsnprintf vsnprintf |
| 31 | +#endif |
| 32 | + |
| 33 | +namespace TR { |
| 34 | + |
| 35 | +// Returns the length of the formatted string without writing it to memory. |
| 36 | +int vprintfLen(const char *fmt, va_list args) |
| 37 | + { |
| 38 | + // This works with both standard vsnprintf() and _vsnprintf(). |
| 39 | + return stdlib_vsnprintf(NULL, 0, fmt, args); |
| 40 | + } |
| 41 | + |
| 42 | +// Returns the length of the formatted string without writing it to memory. |
| 43 | +int printfLen(const char *fmt, ...) |
| 44 | + { |
| 45 | + va_list args; |
| 46 | + va_start(args, fmt); |
| 47 | + int len = TR::vprintfLen(fmt, args); |
| 48 | + va_end(args); |
| 49 | + return len; |
| 50 | + } |
| 51 | + |
| 52 | +// Returns true if the formatted string was truncated. The length of the |
| 53 | +// formatted string (excluding the NUL terminator) is stored into len. That |
| 54 | +// length is (size - 1) if the output was truncated, because when using |
| 55 | +// _vsnprintf() the would-be length is unknown. |
| 56 | +bool vsnprintfTrunc(char *buf, size_t size, int *len, const char *fmt, va_list args) |
| 57 | + { |
| 58 | + // This assertion guarantees that (size - 1) will not overflow in the |
| 59 | + // truncation case below, and that there is actually a location in which to |
| 60 | + // store the terminating NUL. The only reason to pass size=0 would be to |
| 61 | + // compute the formatted length, which should be done using |
| 62 | + // TR::[v]printfLen() instead. |
| 63 | + TR_ASSERT_FATAL(size > 0, "vsnprintfTrunc: no buffer space provided"); |
| 64 | + |
| 65 | + // This assertion guarantees that it's safe to truncate (size - 1) to int in |
| 66 | + // the string truncation case below. |
| 67 | + TR_ASSERT_FATAL(size - 1 <= (size_t)INT_MAX, "vsnprintfTrunc: buffer too large"); |
| 68 | + |
| 69 | + int n = stdlib_vsnprintf(buf, size, fmt, args); |
| 70 | + |
| 71 | + // The formatted length does not include the NUL, so if (n >= size), the |
| 72 | + // result has been truncated. If we are using _vsnprintf(), it's possible to |
| 73 | + // get (n == size), which indicates that the entire formatted string has |
| 74 | + // been stored *without* a trailing NUL, but not (n > size). In the case |
| 75 | + // where standard vsnprintf() produces (n > size), _vsnprintf() returns a |
| 76 | + // negative value instead. So a negative result also indicates truncation. |
| 77 | + bool truncated = n < 0 || n >= size; |
| 78 | + if (truncated) |
| 79 | + { |
| 80 | + buf[size - 1] = '\0'; // Ensure NUL-termination in the _vsnprintf() case. |
| 81 | + *len = (int)(size - 1); |
| 82 | + } |
| 83 | + else |
| 84 | + { |
| 85 | + *len = n; |
| 86 | + } |
| 87 | + |
| 88 | + return truncated; |
| 89 | + } |
| 90 | + |
| 91 | +// See vsnprintfTrunc() above. |
| 92 | +bool snprintfTrunc(char *buf, size_t size, int *len, const char *fmt, ...) |
| 93 | + { |
| 94 | + va_list args; |
| 95 | + va_start(args, fmt); |
| 96 | + bool truncated = TR::vsnprintfTrunc(buf, size, len, fmt, args); |
| 97 | + va_end(args); |
| 98 | + return truncated; |
| 99 | + } |
| 100 | + |
| 101 | +// Variant of vsnprintfTrunc() without the len pointer, for cases where the |
| 102 | +// caller is uninterested in the length. |
| 103 | +bool vsnprintfTrunc(char *buf, size_t size, const char *fmt, va_list args) |
| 104 | + { |
| 105 | + int dummy; |
| 106 | + return vsnprintfTrunc(buf, size, &dummy, fmt, args); |
| 107 | + } |
| 108 | + |
| 109 | +// Variant of snprintfTrunc() without the len pointer, for cases where the |
| 110 | +// caller is uninterested in the length. |
| 111 | +bool snprintfTrunc(char *buf, size_t size, const char *fmt, ...) |
| 112 | + { |
| 113 | + va_list args; |
| 114 | + va_start(args, fmt); |
| 115 | + bool truncated = TR::vsnprintfTrunc(buf, size, fmt, args); |
| 116 | + va_end(args); |
| 117 | + return truncated; |
| 118 | + } |
| 119 | + |
| 120 | +// Asserts that the formatted string was not truncated and returns its length |
| 121 | +// (excluding the NUL terminator). |
| 122 | +int vsnprintfNoTrunc(char *buf, size_t size, const char *fmt, va_list args) |
| 123 | + { |
| 124 | + int len; |
| 125 | + bool truncated = TR::vsnprintfTrunc(buf, size, &len, fmt, args); |
| 126 | + TR_ASSERT_FATAL(!truncated, "vsnprintfNoTrunc: truncation occurred"); |
| 127 | + return len; |
| 128 | + } |
| 129 | + |
| 130 | +// Asserts that the formatted string was not truncated and returns its length |
| 131 | +// (excluding the NUL terminator). |
| 132 | +int snprintfNoTrunc(char *buf, size_t size, const char *fmt, ...) |
| 133 | + { |
| 134 | + va_list args; |
| 135 | + va_start(args, fmt); |
| 136 | + int len = TR::vsnprintfNoTrunc(buf, size, fmt, args); |
| 137 | + va_end(args); |
| 138 | + return len; |
| 139 | + } |
| 140 | + |
| 141 | +void TR::StringBuf::vappendf(const char *fmt, va_list args) |
| 142 | + { |
| 143 | + va_list argsCopy; |
| 144 | + |
| 145 | + va_copy(argsCopy, args); |
| 146 | + int appendLen = TR::vprintfLen(fmt, argsCopy); |
| 147 | + va_copy_end(argsCopy); |
| 148 | + |
| 149 | + TR_ASSERT_FATAL(appendLen >= 0, "error in format string"); |
| 150 | + |
| 151 | + size_t newLen = _len + appendLen; |
| 152 | + ensureCapacity(newLen); |
| 153 | + |
| 154 | + TR_ASSERT_FATAL(appendLen + 1 <= _cap - _len, "insufficient buffer capacity"); |
| 155 | + int realAppendLen = stdlib_vsnprintf(&_text[_len], appendLen + 1, fmt, args); |
| 156 | + TR_ASSERT_FATAL(realAppendLen == appendLen, "incorrect predicted snprintf length"); |
| 157 | + TR_ASSERT_FATAL(_text[newLen] == '\0', "missing NUL terminator"); |
| 158 | + |
| 159 | + _len = newLen; |
| 160 | + } |
| 161 | + |
| 162 | +void TR::StringBuf::appendf(const char *fmt, ...) |
| 163 | + { |
| 164 | + va_list args; |
| 165 | + va_start(args, fmt); |
| 166 | + vappendf(fmt, args); |
| 167 | + va_end(args); |
| 168 | + } |
| 169 | + |
| 170 | +void TR::StringBuf::ensureCapacity(size_t newLen) |
| 171 | + { |
| 172 | + // Note that the capacity must always be strictly greater than the length so |
| 173 | + // that there is room for the NUL terminator. |
| 174 | + if (newLen < _cap) |
| 175 | + return; // nothing to do |
| 176 | + |
| 177 | + size_t newCap = newLen + 1; // + 1 for NUL terminator |
| 178 | + |
| 179 | + // Grow the buffer geometrically each time it is reallocated to ensure that |
| 180 | + // appends are amortized O(1) per character |
| 181 | + if (newCap < 2 * _cap) |
| 182 | + newCap = 2 * _cap; |
| 183 | + |
| 184 | + char *newText = (char*)_region.allocate(newCap); |
| 185 | + memcpy(newText, _text, _len + 1); // + 1 for NUL terminator |
| 186 | + _text = newText; |
| 187 | + _cap = newCap; |
| 188 | + } |
| 189 | + |
| 190 | +} |
0 commit comments