Skip to content

Commit a532835

Browse files
committed
cp duplicateStringValue()
1 parent ef21fbc commit a532835

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

include/json/value.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ Json::Value obj_value(Json::objectValue); // {}
518518
LargestUInt uint_;
519519
double real_;
520520
bool bool_;
521-
char* string_;
521+
char* string_; // actually ptr to unsigned, followed by str
522522
ObjectValues* map_;
523523
} value_;
524524
ValueType type_ : 8;

src/lib_json/json_value.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,28 @@ static inline char* duplicateStringValue(const char* value,
100100
return newString;
101101
}
102102

103+
/* Record the length as a prefix.
104+
*/
105+
static inline char* duplicatePrefixedStringValue(
106+
const char* value,
107+
unsigned int length = unknown)
108+
{
109+
if (length == unknown)
110+
length = (unsigned int)strlen(value);
111+
112+
// Avoid an integer overflow in the call to malloc below by limiting length
113+
// to a sane value.
114+
if (length >= (unsigned)Value::maxInt)
115+
length = Value::maxInt - 1;
116+
117+
char* newString = static_cast<char*>(malloc(length + 1));
118+
JSON_ASSERT_MESSAGE(newString != 0,
119+
"in Json::Value::duplicateStringValue(): "
120+
"Failed to allocate string value buffer");
121+
memcpy(newString, value, length);
122+
newString[length] = 0;
123+
return newString;
124+
}
103125
/** Free the string duplicated by duplicateStringValue().
104126
*/
105127
static inline void releaseStringValue(char* value) { free(value); }

0 commit comments

Comments
 (0)