From: Peter Feiner <pfeiner@xxxxxxxxxx> Signed-off-by: Peter Feiner <pfeiner@xxxxxxxxxx> Signed-off-by: David Matlack <dmatlack@xxxxxxxxxx> --- lib/libcflat.h | 1 + lib/report.c | 38 +++++++++++++++++++++++++++++++++----- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/lib/libcflat.h b/lib/libcflat.h index 5d356df75f1f..05c18543dd72 100644 --- a/lib/libcflat.h +++ b/lib/libcflat.h @@ -96,6 +96,7 @@ extern int vsnprintf(char *buf, int size, const char *fmt, va_list va) extern int vprintf(const char *fmt, va_list va) __attribute__((format(printf, 1, 0))); +void report_prefix_pushf(const char *prefix_fmt, ...); extern void report_prefix_push(const char *prefix); extern void report_prefix_pop(void); extern void report(const char *msg_fmt, bool pass, ...); diff --git a/lib/report.c b/lib/report.c index e24e81382f9e..1033f1e44e99 100644 --- a/lib/report.c +++ b/lib/report.c @@ -17,14 +17,42 @@ static unsigned int tests, failures, xfailures, skipped; static char prefixes[256]; static struct spinlock lock; -void report_prefix_push(const char *prefix) +#define PREFIX_DELIMITER ": " + +void report_prefix_pushf(const char *prefix_fmt, ...) { + va_list va; + int len; + int start; + spin_lock(&lock); - strcat(prefixes, prefix); - strcat(prefixes, ": "); + + len = strlen(prefixes); + assert_msg(len < sizeof(prefixes), "%d >= %lu", len, sizeof(prefixes)); + start = len; + + va_start(va, prefix_fmt); + len += vsnprintf(&prefixes[len], sizeof(prefixes) - len, prefix_fmt, + va); + va_end(va); + assert_msg(len < sizeof(prefixes), "%d >= %lu", len, sizeof(prefixes)); + + assert_msg(!strstr(&prefixes[start], PREFIX_DELIMITER), + "Prefix \"%s\" contains delimiter \"" PREFIX_DELIMITER "\"", + &prefixes[start]); + + len += snprintf(&prefixes[len], sizeof(prefixes) - len, + PREFIX_DELIMITER); + assert_msg(len < sizeof(prefixes), "%d >= %lu", len, sizeof(prefixes)); + spin_unlock(&lock); } +void report_prefix_push(const char *prefix) +{ + report_prefix_pushf("%s", prefix); +} + void report_prefix_pop(void) { char *p, *q; @@ -34,9 +62,9 @@ void report_prefix_pop(void) if (!*prefixes) return; - for (p = prefixes, q = strstr(p, ": ") + 2; + for (p = prefixes, q = strstr(p, PREFIX_DELIMITER) + 2; *q; - p = q, q = strstr(p, ": ") + 2) + p = q, q = strstr(p, PREFIX_DELIMITER) + 2) ; *p = '\0'; -- 2.12.2.816.g2cccc81164-goog