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