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