* xmlStrVASPrintf: * @out: pointer to the resulting string * @maxSize: maximum size of the output buffer * @msg: printf format string * @ap: arguments for format string * * Creates a newly allocated string according to format. * * Returns 0 on success, 1 if the result was truncated or on other * errors, -1 if a memory allocation failed. */
| 606 | * errors, -1 if a memory allocation failed. |
| 607 | */ |
| 608 | int |
| 609 | xmlStrVASPrintf(xmlChar **out, int maxSize, const char *msg, va_list ap) { |
| 610 | char empty[1]; |
| 611 | va_list copy; |
| 612 | xmlChar *buf; |
| 613 | int res, size; |
| 614 | int truncated = 0; |
| 615 | |
| 616 | if (out == NULL) |
| 617 | return(1); |
| 618 | *out = NULL; |
| 619 | if (msg == NULL) |
| 620 | return(1); |
| 621 | if (maxSize < 32) |
| 622 | maxSize = 32; |
| 623 | |
| 624 | va_copy(copy, ap); |
| 625 | res = vsnprintf(empty, 1, msg, copy); |
| 626 | va_end(copy); |
| 627 | |
| 628 | if (res > 0) { |
| 629 | /* snprintf seems to work according to C99. */ |
| 630 | |
| 631 | if (res < maxSize) { |
| 632 | size = res + 1; |
| 633 | } else { |
| 634 | size = maxSize; |
| 635 | truncated = 1; |
| 636 | } |
| 637 | buf = xmlMalloc(size); |
| 638 | if (buf == NULL) |
| 639 | return(-1); |
| 640 | if (vsnprintf((char *) buf, size, msg, ap) < 0) { |
| 641 | xmlFree(buf); |
| 642 | return(1); |
| 643 | } |
| 644 | } else { |
| 645 | /* |
| 646 | * Unfortunately, older snprintf implementations don't follow the |
| 647 | * C99 spec. If the output exceeds the size of the buffer, they can |
| 648 | * return -1, 0 or the number of characters written instead of the |
| 649 | * needed size. Older MSCVRT also won't write a terminating null |
| 650 | * byte if the buffer is too small. |
| 651 | * |
| 652 | * If the value returned is non-negative and strictly less than |
| 653 | * the buffer size (without terminating null), the result should |
| 654 | * have been written completely, so we double the buffer size |
| 655 | * until this condition is true. This assumes that snprintf will |
| 656 | * eventually return a non-negative value. Otherwise, we will |
| 657 | * allocate more and more memory until we run out. |
| 658 | * |
| 659 | * Note that this code path is also executed on conforming |
| 660 | * platforms if the output is the empty string. |
| 661 | */ |
| 662 | |
| 663 | buf = NULL; |
| 664 | size = 32; |
| 665 | while (1) { |
no outgoing calls
no test coverage detected