mirror of
https://github.com/DaveGamble/cJSON.git
synced 2023-08-10 21:13:26 +03:00
Replace strcpy with memcpy and remove magic numbers for string sizes
This commit is contained in:
parent
054b4d146d
commit
0b20df9ecf
26
cJSON.c
26
cJSON.c
@ -836,12 +836,13 @@ static cJSON_bool print_string_ptr(const unsigned char * const input, printbuffe
|
||||
/* empty string */
|
||||
if (input == NULL)
|
||||
{
|
||||
output = ensure(output_buffer, sizeof("\"\""));
|
||||
const char quotes[] = "\"\"";
|
||||
output = ensure(output_buffer, sizeof(quotes));
|
||||
if (output == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
strcpy((char*)output, "\"\"");
|
||||
memcpy(output, quotes, sizeof(quotes));
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -1297,31 +1298,40 @@ static cJSON_bool print_value(const cJSON * const item, printbuffer * const outp
|
||||
switch ((item->type) & 0xFF)
|
||||
{
|
||||
case cJSON_NULL:
|
||||
output = ensure(output_buffer, 5);
|
||||
{
|
||||
const char null_string[] = "null";
|
||||
output = ensure(output_buffer, sizeof(null_string));
|
||||
if (output == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
strcpy((char*)output, "null");
|
||||
memcpy(output, null_string, sizeof(null_string));
|
||||
return true;
|
||||
}
|
||||
|
||||
case cJSON_False:
|
||||
output = ensure(output_buffer, 6);
|
||||
{
|
||||
const char false_string[] = "false";
|
||||
output = ensure(output_buffer, sizeof(false_string));
|
||||
if (output == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
strcpy((char*)output, "false");
|
||||
memcpy(output, false_string, sizeof(false_string));
|
||||
return true;
|
||||
}
|
||||
|
||||
case cJSON_True:
|
||||
output = ensure(output_buffer, 5);
|
||||
{
|
||||
const char true_string[] = "true";
|
||||
output = ensure(output_buffer, sizeof(true_string));
|
||||
if (output == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
strcpy((char*)output, "true");
|
||||
memcpy(output, true_string, sizeof(true_string));
|
||||
return true;
|
||||
}
|
||||
|
||||
case cJSON_Number:
|
||||
return print_number(item, output_buffer);
|
||||
|
Loading…
Reference in New Issue
Block a user