MCPcopy Create free account
hub / github.com/Kitware/VTK / duplicateAndPrefixStringValue

Function duplicateAndPrefixStringValue

ThirdParty/jsoncpp/vtkjsoncpp/jsoncpp.cpp:2571–2590  ·  view source on GitHub ↗

Record the length as a prefix. */

Source from the content-addressed store, hash-verified

2569/* Record the length as a prefix.
2570 */
2571static inline char* duplicateAndPrefixStringValue(const char* value,
2572 unsigned int length) {
2573 // Avoid an integer overflow in the call to malloc below by limiting length
2574 // to a sane value.
2575 JSON_ASSERT_MESSAGE(length <= static_cast<unsigned>(Value::maxInt) -
2576 sizeof(unsigned) - 1U,
2577 "in Json::Value::duplicateAndPrefixStringValue(): "
2578 "length too big for prefixing");
2579 size_t actualLength = sizeof(length) + length + 1;
2580 auto newString = static_cast<char*>(malloc(actualLength));
2581 if (newString == nullptr) {
2582 throwRuntimeError("in Json::Value::duplicateAndPrefixStringValue(): "
2583 "Failed to allocate string value buffer");
2584 }
2585 *reinterpret_cast<unsigned*>(newString) = length;
2586 memcpy(newString + sizeof(unsigned), value, length);
2587 newString[actualLength - 1U] =
2588 0; // to avoid buffer over-run accidents by users later
2589 return newString;
2590}
2591inline static void decodePrefixedString(bool isPrefixed, char const* prefixed,
2592 unsigned* length, char const** value) {
2593 if (!isPrefixed) {

Callers 2

ValueMethod · 0.85
dupPayloadMethod · 0.85

Calls 1

throwRuntimeErrorFunction · 0.85

Tested by

no test coverage detected