diff mbox

[kvm-unit-tests,21/32] lib: printf-style report prefixes

Message ID 20170421005004.137260-22-dmatlack@google.com (mailing list archive)
State New, archived
Headers show

Commit Message

David Matlack April 21, 2017, 12:49 a.m. UTC
From: Peter Feiner <pfeiner@google.com>

Signed-off-by: Peter Feiner <pfeiner@google.com>
Signed-off-by: David Matlack <dmatlack@google.com>
---
 lib/libcflat.h |  1 +
 lib/report.c   | 38 +++++++++++++++++++++++++++++++++-----
 2 files changed, 34 insertions(+), 5 deletions(-)

Comments

Andrew Jones April 21, 2017, 7:42 a.m. UTC | #1
On Thu, Apr 20, 2017 at 05:49:53PM -0700, David Matlack wrote:
> From: Peter Feiner <pfeiner@google.com>
> 
> Signed-off-by: Peter Feiner <pfeiner@google.com>
> Signed-off-by: David Matlack <dmatlack@google.com>
> ---
>  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));

I don't think this last assert is necessary.

> +
>  	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
>

Otherwise

Reviewed-by: Andrew Jones <drjones@redhat.com>
Thomas Huth May 12, 2017, 10:51 a.m. UTC | #2
On 21.04.2017 02:49, David Matlack wrote:
> From: Peter Feiner <pfeiner@google.com>
> 
> Signed-off-by: Peter Feiner <pfeiner@google.com>
> Signed-off-by: David Matlack <dmatlack@google.com>
> ---
>  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);
>  }

I can not compile the current master of kvm-unit-tests anymore... I
think it is due to the above patch. I get now these error messages:

powerpc64-linux-gnu-gcc  -std=gnu99 -ffreestanding -Wextra -O2 -I lib -I lib/libfdt -Wa,-mregnames -g -MMD -MF lib/.report.d -Wall -Werror  -fomit-frame-pointer    -Wno-frame-address   -fno-pic   -mbig-endian   -c -o lib/report.o lib/report.c
In file included from lib/report.c:13:0:
lib/report.c: In function ‘report_prefix_pushf’:
lib/report.c:38:17: error: comparison between signed and unsigned integer expressions [-Werror=sign-compare]
  assert_msg(len < sizeof(prefixes), "%d >= %lu", len, sizeof(prefixes));
                 ^
lib/libcflat.h:133:8: note: in definition of macro ‘assert_msg’
  if (!(cond)) {       \
        ^
lib/report.c:45:17: error: comparison between signed and unsigned integer expressions [-Werror=sign-compare]
  assert_msg(len < sizeof(prefixes), "%d >= %lu", len, sizeof(prefixes));
                 ^
lib/libcflat.h:133:8: note: in definition of macro ‘assert_msg’
  if (!(cond)) {       \
        ^
lib/report.c:53:17: error: comparison between signed and unsigned integer expressions [-Werror=sign-compare]
  assert_msg(len < sizeof(prefixes), "%d >= %lu", len, sizeof(prefixes));
                 ^
lib/libcflat.h:133:8: note: in definition of macro ‘assert_msg’
  if (!(cond)) {       \
        ^
 Thomas
David Hildenbrand May 12, 2017, 11:22 a.m. UTC | #3
On 12.05.2017 12:51, Thomas Huth wrote:
> On 21.04.2017 02:49, David Matlack wrote:
>> From: Peter Feiner <pfeiner@google.com>
>>
>> Signed-off-by: Peter Feiner <pfeiner@google.com>
>> Signed-off-by: David Matlack <dmatlack@google.com>
>> ---
>>  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);
>>  }
> 
> I can not compile the current master of kvm-unit-tests anymore... I
> think it is due to the above patch. I get now these error messages:
> 
> powerpc64-linux-gnu-gcc  -std=gnu99 -ffreestanding -Wextra -O2 -I lib -I lib/libfdt -Wa,-mregnames -g -MMD -MF lib/.report.d -Wall -Werror  -fomit-frame-pointer    -Wno-frame-address   -fno-pic   -mbig-endian   -c -o lib/report.o lib/report.c
> In file included from lib/report.c:13:0:
> lib/report.c: In function ‘report_prefix_pushf’:
> lib/report.c:38:17: error: comparison between signed and unsigned integer expressions [-Werror=sign-compare]
>   assert_msg(len < sizeof(prefixes), "%d >= %lu", len, sizeof(prefixes));
>                  ^
> lib/libcflat.h:133:8: note: in definition of macro ‘assert_msg’
>   if (!(cond)) {       \
>         ^
> lib/report.c:45:17: error: comparison between signed and unsigned integer expressions [-Werror=sign-compare]
>   assert_msg(len < sizeof(prefixes), "%d >= %lu", len, sizeof(prefixes));
>                  ^
> lib/libcflat.h:133:8: note: in definition of macro ‘assert_msg’
>   if (!(cond)) {       \
>         ^
> lib/report.c:53:17: error: comparison between signed and unsigned integer expressions [-Werror=sign-compare]
>   assert_msg(len < sizeof(prefixes), "%d >= %lu", len, sizeof(prefixes));
>                  ^
> lib/libcflat.h:133:8: note: in definition of macro ‘assert_msg’
>   if (!(cond)) {       \
>         ^
>  Thomas
> 

Also found that while preparing the s390x patch series. Already sent

[kvm-unit-tests PATCH v1] lib: fix compilation warning
David Matlack May 12, 2017, 4:53 p.m. UTC | #4
On Fri, May 12, 2017 at 4:22 AM, David Hildenbrand <david@redhat.com> wrote:
>
> Also found that while preparing the s390x patch series. Already sent
>
> [kvm-unit-tests PATCH v1] lib: fix compilation warning

Sorry about that. Thanks for the fix!

>
> --
>
> Thanks,
>
> David
diff mbox

Patch

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';