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