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. */
| 1572 | * @return Pointer on the duplicate instance of string. |
| 1573 | */ |
| 1574 | static inline char* duplicateStringValue(const char* value, |
| 1575 | unsigned int length = unknown) { |
| 1576 | if (length == unknown) |
| 1577 | length = (unsigned int)strlen(value); |
| 1578 | |
| 1579 | // Avoid an integer overflow in the call to malloc below by limiting length |
| 1580 | // to a sane value. |
| 1581 | if (length >= (unsigned)Value::maxInt) |
| 1582 | length = Value::maxInt - 1; |
| 1583 | |
| 1584 | char* newString = static_cast<char*>(malloc(length + 1)); |
| 1585 | JSON_ASSERT_MESSAGE(newString != 0, |
| 1586 | "in Json::Value::duplicateStringValue(): " |
| 1587 | "Failed to allocate string value buffer"); |
| 1588 | memcpy(newString, value, length); |
| 1589 | newString[length] = 0; |
| 1590 | return newString; |
| 1591 | } |
| 1592 | |
| 1593 | /** Free the string duplicated by duplicateStringValue(). |
| 1594 | */ |
no outgoing calls
no test coverage detected