MCPcopy Create free account
hub / github.com/WolfireGames/overgrowth / duplicateAndPrefixStringValue

Function duplicateAndPrefixStringValue

Source/JSON/jsoncpp.cpp:2275–2294  ·  view source on GitHub ↗

Record the length as a prefix. */

Source from the content-addressed store, hash-verified

2273/* Record the length as a prefix.
2274 */
2275static inline char* duplicateAndPrefixStringValue(
2276 const char* value,
2277 unsigned int length) {
2278 // Avoid an integer overflow in the call to malloc below by limiting length
2279 // to a sane value.
2280 JSON_ASSERT_MESSAGE(length <= (unsigned)Value::maxInt - sizeof(unsigned) - 1U,
2281 "in Json::Value::duplicateAndPrefixStringValue(): "
2282 "length too big for prefixing");
2283 unsigned actualLength = length + static_cast<unsigned>(sizeof(unsigned)) + 1U;
2284 char* newString = static_cast<char*>(OG_MALLOC(actualLength));
2285 if (newString == 0) {
2286 throwRuntimeError(
2287 "in Json::Value::duplicateAndPrefixStringValue(): "
2288 "Failed to allocate string value buffer");
2289 }
2290 *reinterpret_cast<unsigned*>(newString) = length;
2291 memcpy(newString + sizeof(unsigned), value, length);
2292 newString[actualLength - 1U] = 0; // to avoid buffer over-run accidents by users later
2293 return newString;
2294}
2295inline static void decodePrefixedString(
2296 bool isPrefixed, char const* prefixed,
2297 unsigned* length, char const** value) {

Callers 1

ValueMethod · 0.70

Calls 1

throwRuntimeErrorFunction · 0.70

Tested by

no test coverage detected