MCPcopy Create free account
hub / github.com/esnet/iperf / ensure

Function ensure

src/cjson.c:450–533  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

448
449/* realloc printbuffer if necessary to have at least "needed" bytes more */
450static unsigned char* ensure(printbuffer * const p, size_t needed)
451{
452 unsigned char *newbuffer = NULL;
453 size_t newsize = 0;
454
455 if ((p == NULL) || (p->buffer == NULL))
456 {
457 return NULL;
458 }
459
460 if ((p->length > 0) && (p->offset >= p->length))
461 {
462 /* make sure that offset is valid */
463 return NULL;
464 }
465
466 if (needed > SIZE_MAX)
467 {
468 /* sizes bigger than SIZE_MAX are currently not supported */
469 return NULL;
470 }
471
472 needed += p->offset + 1;
473 if (needed <= p->length)
474 {
475 return p->buffer + p->offset;
476 }
477
478 if (p->noalloc) {
479 return NULL;
480 }
481
482 /* calculate new buffer size */
483 if (needed > (SIZE_MAX / 2))
484 {
485 /* overflow of int, use SIZE_MAX if possible */
486 if (needed <= SIZE_MAX)
487 {
488 newsize = SIZE_MAX;
489 }
490 else
491 {
492 return NULL;
493 }
494 }
495 else
496 {
497 newsize = needed * 2;
498 }
499
500 if (p->hooks.reallocate != NULL)
501 {
502 /* reallocate with realloc if available */
503 newbuffer = (unsigned char*)p->hooks.reallocate(p->buffer, newsize);
504 if (newbuffer == NULL)
505 {
506 p->hooks.deallocate(p->buffer);
507 p->length = 0;

Callers 5

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

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…