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. */
| 2564 | * @return Pointer on the duplicate instance of string. |
| 2565 | */ |
| 2566 | static inline char* duplicateStringValue(const char* value, |
| 2567 | size_t length) |
| 2568 | { |
| 2569 | // Avoid an integer overflow in the call to malloc below by limiting length |
| 2570 | // to a sane value. |
| 2571 | if (length >= static_cast<size_t>(Value::maxInt)) |
| 2572 | length = Value::maxInt - 1; |
| 2573 | |
| 2574 | char* newString = static_cast<char*>(malloc(length + 1)); |
| 2575 | if (newString == NULL) { |
| 2576 | throwRuntimeError( |
| 2577 | "in Json::Value::duplicateStringValue(): " |
| 2578 | "Failed to allocate string value buffer"); |
| 2579 | } |
| 2580 | memcpy(newString, value, length); |
| 2581 | newString[length] = 0; |
| 2582 | return newString; |
| 2583 | } |
| 2584 | |
| 2585 | /* Record the length as a prefix. |
| 2586 | */ |
no test coverage detected