Record the length as a prefix. */
| 2538 | /* Record the length as a prefix. |
| 2539 | */ |
| 2540 | static inline char* duplicateAndPrefixStringValue( |
| 2541 | const char* value, |
| 2542 | unsigned int length) |
| 2543 | { |
| 2544 | // Avoid an integer overflow in the call to malloc below by limiting length |
| 2545 | // to a sane value. |
| 2546 | JSON_ASSERT_MESSAGE(length <= (unsigned)Value::maxInt - sizeof(unsigned) - 1U, |
| 2547 | "in Json::Value::duplicateAndPrefixStringValue(): " |
| 2548 | "length too big for prefixing"); |
| 2549 | unsigned actualLength = length + static_cast<unsigned>(sizeof(unsigned)) + 1U; |
| 2550 | char* newString = static_cast<char*>(malloc(actualLength)); |
| 2551 | if (newString == 0) { |
| 2552 | throwRuntimeError( |
| 2553 | "in Json::Value::duplicateAndPrefixStringValue(): " |
| 2554 | "Failed to allocate string value buffer"); |
| 2555 | } |
| 2556 | *reinterpret_cast<unsigned*>(newString) = length; |
| 2557 | memcpy(newString + sizeof(unsigned), value, length); |
| 2558 | newString[actualLength - 1U] = 0; // to avoid buffer over-run accidents by users later |
| 2559 | return newString; |
| 2560 | } |
| 2561 | inline static void decodePrefixedString( |
| 2562 | bool isPrefixed, char const* prefixed, |
| 2563 | unsigned* length, char const** value) |
no test coverage detected