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