Record the length as a prefix. */
| 2569 | /* Record the length as a prefix. |
| 2570 | */ |
| 2571 | static 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 | } |
| 2591 | inline static void decodePrefixedString(bool isPrefixed, char const* prefixed, |
| 2592 | unsigned* length, char const** value) { |
| 2593 | if (!isPrefixed) { |
no test coverage detected