MCPcopy Create free account
hub / github.com/DaveGamble/cJSON / ensure

Function ensure

cJSON.c:485–568  ·  view source on GitHub ↗

realloc printbuffer if necessary to have at least "needed" bytes more */

Source from the content-addressed store, hash-verified

483
484/* realloc printbuffer if necessary to have at least "needed" bytes more */
485static unsigned char* ensure(printbuffer * const p, size_t needed)
486{
487 unsigned char *newbuffer = NULL;
488 size_t newsize = 0;
489
490 if ((p == NULL) || (p->buffer == NULL))
491 {
492 return NULL;
493 }
494
495 if ((p->length > 0) && (p->offset >= p->length))
496 {
497 /* make sure that offset is valid */
498 return NULL;
499 }
500
501 if (needed > INT_MAX)
502 {
503 /* sizes bigger than INT_MAX are currently not supported */
504 return NULL;
505 }
506
507 needed += p->offset + 1;
508 if (needed <= p->length)
509 {
510 return p->buffer + p->offset;
511 }
512
513 if (p->noalloc) {
514 return NULL;
515 }
516
517 /* calculate new buffer size */
518 if (needed > (INT_MAX / 2))
519 {
520 /* overflow of int, use INT_MAX if possible */
521 if (needed <= INT_MAX)
522 {
523 newsize = INT_MAX;
524 }
525 else
526 {
527 return NULL;
528 }
529 }
530 else
531 {
532 newsize = needed * 2;
533 }
534
535 if (p->hooks.reallocate != NULL)
536 {
537 /* reallocate with realloc if available */
538 newbuffer = (unsigned char*)p->hooks.reallocate(p->buffer, newsize);
539 if (newbuffer == NULL)
540 {
541 p->hooks.deallocate(p->buffer);
542 p->length = 0;

Callers 6

print_numberFunction · 0.85
print_string_ptrFunction · 0.85
print_valueFunction · 0.85
print_arrayFunction · 0.85
print_objectFunction · 0.85

Calls

no outgoing calls

Tested by 1

Used in the wild real call sites across dependent graphs

searching dependent graphs…