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. */
| 2518 | * @return Pointer on the duplicate instance of string. |
| 2519 | */ |
| 2520 | static inline char* duplicateStringValue(const char* value, |
| 2521 | size_t length) { |
| 2522 | // Avoid an integer overflow in the call to malloc below by limiting length |
| 2523 | // to a sane value. |
| 2524 | if (length >= (size_t)Value::maxInt) |
| 2525 | length = Value::maxInt - 1; |
| 2526 | |
| 2527 | char* newString = static_cast<char*>(malloc(length + 1)); |
| 2528 | if (newString == NULL) { |
| 2529 | throwRuntimeError( |
| 2530 | "in Json::Value::duplicateStringValue(): " |
| 2531 | "Failed to allocate string value buffer"); |
| 2532 | } |
| 2533 | memcpy(newString, value, length); |
| 2534 | newString[length] = 0; |
| 2535 | return newString; |
| 2536 | } |
| 2537 | |
| 2538 | /* Record the length as a prefix. |
| 2539 | */ |
no test coverage detected