| 7 | } |
| 8 | |
| 9 | std::string svsprintf(const char* fmt, va_list ap_orig) { |
| 10 | fmt = convert_fmt_str(fmt); |
| 11 | int size = 100; /* Guess we need no more than 100 bytes */ |
| 12 | char* p; |
| 13 | |
| 14 | if ((p = (char*)malloc(size)) == nullptr) |
| 15 | goto err; |
| 16 | |
| 17 | for (;;) { |
| 18 | va_list ap; |
| 19 | va_copy(ap, ap_orig); |
| 20 | int n = vsnprintf(p, size, fmt, ap); |
| 21 | va_end(ap); |
| 22 | |
| 23 | if (n < 0) |
| 24 | goto err; |
| 25 | |
| 26 | if (n < size) { |
| 27 | std::string rst(p); |
| 28 | free(p); |
| 29 | return rst; |
| 30 | } |
| 31 | |
| 32 | size = n + 1; |
| 33 | |
| 34 | char* np = (char*)realloc(p, size); |
| 35 | if (!np) { |
| 36 | free(p); |
| 37 | goto err; |
| 38 | } else |
| 39 | p = np; |
| 40 | } |
| 41 | |
| 42 | err: |
| 43 | fprintf(stderr, "could not allocate memory for svsprintf; fmt=%s\n", fmt); |
| 44 | __builtin_trap(); |
| 45 | } |
| 46 | |
| 47 | std::string ssprintf(const char* fmt, ...) { |
| 48 | va_list ap; |
no test coverage detected