string vstringf(char const *format, va_list args) { // estimate string length va_list args2; va_copy(args2, args); int est = vnprintf(format, args2); va_end(args2); // allocate space Array<char> buf(est+1); // render the string int len = vsprintf(buf, format, args); // check the estimate, and fail *hard* if it was low, to avoid any // possibility that this might become exploitable in some context // (do *not* turn this check off in an NDEGUG build) if (len > est) { // don't go through fprintf, etc., because the state of memory // makes that risky static char const msg[] = "fatal error: vnprintf failed to provide a conservative estimate,\n" "memory is most likely corrupted\n"; write(2 /*stderr*/, msg, strlen(msg)); abort(); } // happy return string(buf); }
void printk(const char *format, ...) { va_list ap; va_start(ap, format); vnprintf(_putc, (char*)format, ap); };
int nprintf(char const *format, ...) { va_list args; va_start(args, format); int ret = vnprintf(format, args); va_end(args); return ret; }
void nprintfVector(char const *format, ...) { va_list args; // run vnprintf to obtain estimate va_start(args, format); int est = vnprintf(format, args); va_end(args); // make that much space char *buf = new char[est+1 + 50 /*safety margin*/]; // run the real vsprintf va_start(args, format); int len = vsprintf(buf, format, args); va_end(args); if (len > est) { printf("nprintf failed to conservatively estimate!\n"); printf(" format: %s\n", format); printf(" estimate: %d\n", est); printf(" actual: %d\n", len); exit(2); } if (len != est) { // print the overestimates; they shouldn't be much noise in the // common case, but might hint at a problem earlier than I'd // otherwise notice printf("nprintf overestimate:\n"); printf(" format: %s\n", format); printf(" estimate: %d\n", est); printf(" actual: %d\n", len); } delete[] buf; }