Skip to content

Commit

Permalink
cp duplicateStringValue()
Browse files Browse the repository at this point in the history
  • Loading branch information
cdunn2001 committed Mar 3, 2015
1 parent ef21fbc commit a532835
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion include/json/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ Json::Value obj_value(Json::objectValue); // {}
LargestUInt uint_;
double real_;
bool bool_;
char* string_;
char* string_; // actually ptr to unsigned, followed by str
ObjectValues* map_;
} value_;
ValueType type_ : 8;
Expand Down
22 changes: 22 additions & 0 deletions src/lib_json/json_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
Expand Down

0 comments on commit a532835

Please sign in to comment.