MCPcopy Create free account
hub / github.com/ByConity/ByConity / ensure

Function ensure

contrib/cJSON/cJSON.cpp:384–469  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

382
383/* realloc printbuffer if necessary to have at least "needed" bytes more */
384static unsigned char* ensure(printbuffer * const p, size_t needed)
385{
386 unsigned char *newbuffer = NULL;
387 size_t newsize = 0;
388
389 if ((p == NULL) || (p->buffer == NULL))
390 {
391 return NULL;
392 }
393
394 if ((p->length > 0) && (p->offset >= p->length))
395 {
396 /* make sure that offset is valid */
397 return NULL;
398 }
399
400 if (needed > INT_MAX)
401 {
402 /* sizes bigger than INT_MAX are currently not supported */
403 return NULL;
404 }
405
406 needed += p->offset + 1;
407 if (needed <= p->length)
408 {
409 return p->buffer + p->offset;
410 }
411
412 if (p->noalloc) {
413 return NULL;
414 }
415
416 /* calculate new buffer size */
417 if (needed > (INT_MAX / 2))
418 {
419 /* overflow of int, use INT_MAX if possible */
420 if (needed <= INT_MAX)
421 {
422 newsize = INT_MAX;
423 }
424 else
425 {
426 return NULL;
427 }
428 }
429 else
430 {
431 newsize = needed * 2;
432 }
433
434 if (p->hooks.reallocate != NULL)
435 {
436 /* reallocate with realloc if available */
437 newbuffer = (unsigned char*)p->hooks.reallocate(p->buffer, newsize);
438 if (newbuffer == NULL)
439 {
440 p->hooks.deallocate(p->buffer);
441 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 4

memcpyFunction · 0.85
reallocateMethod · 0.80
deallocateMethod · 0.45
allocateMethod · 0.45

Tested by

no test coverage detected