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. */
| 109 | * @return Pointer on the duplicate instance of string. |
| 110 | */ |
| 111 | static inline char* duplicateStringValue(const char* value, size_t length) { |
| 112 | // Avoid an integer overflow in the call to malloc below by limiting length |
| 113 | // to a sane value. |
| 114 | if (length >= static_cast<size_t>(Value::maxInt)) |
| 115 | length = Value::maxInt - 1; |
| 116 | |
| 117 | auto newString = static_cast<char*>(malloc(length + 1)); |
| 118 | if (newString == nullptr) { |
| 119 | throwRuntimeError("in Json::Value::duplicateStringValue(): Failed to allocate string value buffer"); |
| 120 | } |
| 121 | memcpy(newString, value, length); |
| 122 | newString[length] = 0; |
| 123 | return newString; |
| 124 | } |
| 125 | |
| 126 | /* Record the length as a prefix. |
| 127 | */ |
no test coverage detected