Duplicates the specified string value. * @param value Pointer to the string to duplicate. Must be zero-terminated if * length is "unknown". * @param length Length of the value. if equals to unknown, then it will be * computed using strlen(value). * @return Pointer on the duplicate instance of string. */
| 2753 | * @return Pointer on the duplicate instance of string. |
| 2754 | */ |
| 2755 | static inline char *duplicateStringValue(const char *value, size_t length) |
| 2756 | { |
| 2757 | // Avoid an integer overflow in the call to malloc below by limiting length |
| 2758 | // to a sane value. |
| 2759 | if (length >= static_cast<size_t>(Value::maxInt)) |
| 2760 | length = Value::maxInt - 1; |
| 2761 | |
| 2762 | auto newString = static_cast<char *>(malloc(length + 1)); |
| 2763 | if (newString == nullptr) |
| 2764 | { |
| 2765 | throwRuntimeError("in Json::Value::duplicateStringValue(): " |
| 2766 | "Failed to allocate string value buffer"); |
| 2767 | } |
| 2768 | memcpy(newString, value, length); |
| 2769 | newString[length] = 0; |
| 2770 | return newString; |
| 2771 | } |
| 2772 | |
| 2773 | /* Record the length as a prefix. |
| 2774 | */ |
no test coverage detected