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