cp duplicateStringValue()

This commit is contained in:
Christopher Dunn 2015-02-21 10:54:38 -06:00
parent ef21fbc785
commit a53283568f
2 changed files with 23 additions and 1 deletions

View File

@ -518,7 +518,7 @@ private:
LargestUInt uint_;
double real_;
bool bool_;
char* string_;
char* string_; // actually ptr to unsigned, followed by str
ObjectValues* map_;
} value_;
ValueType type_ : 8;

View File

@ -100,6 +100,28 @@ static inline char* duplicateStringValue(const char* value,
return newString;
}
/* Record the length as a prefix.
*/
static inline char* duplicatePrefixedStringValue(
const char* value,
unsigned int length = unknown)
{
if (length == unknown)
length = (unsigned int)strlen(value);
// Avoid an integer overflow in the call to malloc below by limiting length
// to a sane value.
if (length >= (unsigned)Value::maxInt)
length = Value::maxInt - 1;
char* newString = static_cast<char*>(malloc(length + 1));
JSON_ASSERT_MESSAGE(newString != 0,
"in Json::Value::duplicateStringValue(): "
"Failed to allocate string value buffer");
memcpy(newString, value, length);
newString[length] = 0;
return newString;
}
/** Free the string duplicated by duplicateStringValue().
*/
static inline void releaseStringValue(char* value) { free(value); }