8178118: Arguments::create_numbered_property allocates wrong buffer in case count > 99

Reviewed-by: dholmes, dcubed, sspitsyn
This commit is contained in:
Ekaterina Pavlova 2017-04-06 14:07:21 -07:00 committed by Igor Ignatyev
parent 3af1da4533
commit 11a8c7a52f

View file

@ -2587,19 +2587,26 @@ bool Arguments::create_property(const char* prop_name, const char* prop_value, P
}
bool Arguments::create_numbered_property(const char* prop_base_name, const char* prop_value, unsigned int count) {
// Make sure count is < 1,000. Otherwise, memory allocation will be too small.
if (count < 1000) {
size_t prop_len = strlen(prop_base_name) + strlen(prop_value) + 5;
const unsigned int props_count_limit = 1000;
const int max_digits = 3;
const int extra_symbols_count = 3; // includes '.', '=', '\0'
// Make sure count is < props_count_limit. Otherwise, memory allocation will be too small.
if (count < props_count_limit) {
size_t prop_len = strlen(prop_base_name) + strlen(prop_value) + max_digits + extra_symbols_count;
char* property = AllocateHeap(prop_len, mtArguments);
int ret = jio_snprintf(property, prop_len, "%s.%d=%s", prop_base_name, count, prop_value);
if (ret < 0 || ret >= (int)prop_len) {
FreeHeap(property);
jio_fprintf(defaultStream::error_stream(), "Failed to create property %s.%d=%s\n", prop_base_name, count, prop_value);
return false;
}
bool added = add_property(property, UnwriteableProperty, InternalProperty);
FreeHeap(property);
return added;
}
jio_fprintf(defaultStream::error_stream(), "Property count limit exceeded: %s, limit=%d\n", prop_base_name, props_count_limit);
return false;
}