realloc printbuffer if necessary to have at least "needed" bytes more */
| 483 | |
| 484 | /* realloc printbuffer if necessary to have at least "needed" bytes more */ |
| 485 | static 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; |
no outgoing calls
searching dependent graphs…