MCPcopy Create free account
hub / github.com/Tencent/Hardcoder / ensure

Function ensure

libapp2sys/src/main/cpp/cjson/cJSON.c:374–459  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

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

deallocateMethod · 0.80
allocateMethod · 0.80

Tested by

no test coverage detected