Record the length as a prefix. */
| 2773 | /* Record the length as a prefix. |
| 2774 | */ |
| 2775 | static inline char *duplicateAndPrefixStringValue(const char *value, unsigned int length) |
| 2776 | { |
| 2777 | // Avoid an integer overflow in the call to malloc below by limiting length |
| 2778 | // to a sane value. |
| 2779 | JSON_ASSERT_MESSAGE(length <= static_cast<unsigned>(Value::maxInt) - sizeof(unsigned) - 1U, |
| 2780 | "in Json::Value::duplicateAndPrefixStringValue(): " |
| 2781 | "length too big for prefixing"); |
| 2782 | size_t actualLength = sizeof(length) + length + 1; |
| 2783 | auto newString = static_cast<char *>(malloc(actualLength)); |
| 2784 | if (newString == nullptr) |
| 2785 | { |
| 2786 | throwRuntimeError("in Json::Value::duplicateAndPrefixStringValue(): " |
| 2787 | "Failed to allocate string value buffer"); |
| 2788 | } |
| 2789 | *reinterpret_cast<unsigned *>(newString) = length; |
| 2790 | memcpy(newString + sizeof(unsigned), value, length); |
| 2791 | newString[actualLength - 1U] = 0; // to avoid buffer over-run accidents by users later |
| 2792 | return newString; |
| 2793 | } |
| 2794 | inline static void decodePrefixedString(bool isPrefixed, char const *prefixed, unsigned *length, |
| 2795 | char const **value) |
| 2796 | { |
no test coverage detected