Record the length as a prefix. */
| 2585 | /* Record the length as a prefix. |
| 2586 | */ |
| 2587 | static inline char* duplicateAndPrefixStringValue( |
| 2588 | const char* value, |
| 2589 | unsigned int length) |
| 2590 | { |
| 2591 | // Avoid an integer overflow in the call to malloc below by limiting length |
| 2592 | // to a sane value. |
| 2593 | JSON_ASSERT_MESSAGE(length <= static_cast<unsigned>(Value::maxInt) - sizeof(unsigned) - 1U, |
| 2594 | "in Json::Value::duplicateAndPrefixStringValue(): " |
| 2595 | "length too big for prefixing"); |
| 2596 | unsigned actualLength = length + static_cast<unsigned>(sizeof(unsigned)) + 1U; |
| 2597 | char* newString = static_cast<char*>(malloc(actualLength)); |
| 2598 | if (newString == 0) { |
| 2599 | throwRuntimeError( |
| 2600 | "in Json::Value::duplicateAndPrefixStringValue(): " |
| 2601 | "Failed to allocate string value buffer"); |
| 2602 | } |
| 2603 | *reinterpret_cast<unsigned*>(newString) = length; |
| 2604 | memcpy(newString + sizeof(unsigned), value, length); |
| 2605 | newString[actualLength - 1U] = 0; // to avoid buffer over-run accidents by users later |
| 2606 | return newString; |
| 2607 | } |
| 2608 | inline static void decodePrefixedString( |
| 2609 | bool isPrefixed, char const* prefixed, |
| 2610 | unsigned* length, char const** value) |
no test coverage detected