Skip to contentSkip to conversation

qapi/error: Check format string argument in error_propagate_prepend()

PATCH-for-5.1? Philippe Mathieu-Daudé 4 participants 6 messages Jul 23, 2020

lore
Share & tools
lore
#0 lore raw

error_propagate_prepend() "behaves like error_prepend()", and error_prepend() uses "formatting @fmt, ... like printf()". error_prepend() checks its format string argument, but error_propagate_prepend() does not. Fix that.

This would have catched the invalid format introduced in commit b98e8d1230f:

CC hw/sd/milkymist-memcard.o
hw/sd/milkymist-memcard.c: In function ‘milkymist_memcard_realize’:
hw/sd/milkymist-memcard.c:284:70: error: format ‘%s’ expects a matching ‘char *’ argument [-Werror=format=]

    284 |         error_propagate_prepend(errp, err, "failed to init SD card: %s");
        |                                                                     ~^
        |                                                                      |
        |                                                                      char *
Fixes 4b5766488f ("Fix use of error_prepend() with &error_fatal, &error_abort")
---
 include/qapi/error.h | 1 +
 1 file changed, 1 insertion(+)
include/qapi/error.h +1 -0
index 7932594dce..eeeef1a34d 100644
@@ -381,6 +381,7 @@ void error_propagate(Error **dst_errp, Error *local_err);
  *     error_propagate(dst_errp, local_err);
  * Please use ERRP_GUARD() and error_prepend() instead when possible.
  */
+GCC_FMT_ATTR(3, 4)
 void error_propagate_prepend(Error **dst_errp, Error *local_err,
                              const char *fmt, ...);
 
Signature · 1 line
2.21.3
Reviewed-by
#1 lore raw

Am 23.07.20 um 11:13 schrieb Philippe Mathieu-Daudé:

Quoted 28 lines

error_propagate_prepend() "behaves like error_prepend()", and error_prepend() uses "formatting @fmt, ... like printf()". error_prepend() checks its format string argument, but error_propagate_prepend() does not. Fix that.

This would have catched the invalid format introduced in commit b98e8d1230f:

CC hw/sd/milkymist-memcard.o
hw/sd/milkymist-memcard.c: In function ‘milkymist_memcard_realize’:
hw/sd/milkymist-memcard.c:284:70: error: format ‘%s’ expects a matching ‘char *’ argument [-Werror=format=]

    284 |         error_propagate_prepend(errp, err, "failed to init SD card: %s");
        |                                                                     ~^
        |                                                                      |
        |                                                                      char *
Fixes 4b5766488f ("Fix use of error_prepend() with &error_fatal, &error_abort")
---
 include/qapi/error.h | 1 +
 1 file changed, 1 insertion(+)
include/qapi/error.h +0 -0
index 7932594dce..eeeef1a34d 100644

include/qapi/error.h void error_propagate(Error **dst_errp, Error *local_err); · line 387

Quoted 7 lines
include/qapi/error.h +1 -0
@@ -381,6 +381,7 @@ void error_propagate(Error **dst_errp, Error *local_err);
  *     error_propagate(dst_errp, local_err);
  * Please use ERRP_GUARD() and error_prepend() instead when possible.
  */
+GCC_FMT_ATTR(3, 4)
 void error_propagate_prepend(Error **dst_errp, Error *local_err,
                              const char *fmt, ...);
 

error_vprepend is one more candidate for GCC_FMT_ATTR. Maybe you can add that, too.

Regards,

Stefan

#2 lore raw
Quoted 41 lines · Stefan Weil

Am 23.07.20 um 11:13 schrieb Philippe Mathieu-Daudé:

Quoted 28 lines

error_propagate_prepend() "behaves like error_prepend()", and error_prepend() uses "formatting @fmt, ... like printf()". error_prepend() checks its format string argument, but error_propagate_prepend() does not. Fix that.

This would have catched the invalid format introduced in commit b98e8d1230f:

CC hw/sd/milkymist-memcard.o
hw/sd/milkymist-memcard.c: In function ‘milkymist_memcard_realize’:
hw/sd/milkymist-memcard.c:284:70: error: format ‘%s’ expects a matching ‘char *’ argument [-Werror=format=]

    284 |         error_propagate_prepend(errp, err, "failed to init SD card: %s");
        |                                                                     ~^
        |                                                                      |
        |                                                                      char *
Fixes 4b5766488f ("Fix use of error_prepend() with &error_fatal, &error_abort")
---
 include/qapi/error.h | 1 +
 1 file changed, 1 insertion(+)
include/qapi/error.h +0 -0
index 7932594dce..eeeef1a34d 100644

include/qapi/error.h void error_propagate(Error **dst_errp, Error *local_err); · line 387

Quoted 7 lines
include/qapi/error.h +1 -0
@@ -381,6 +381,7 @@ void error_propagate(Error **dst_errp, Error *local_err);
  *     error_propagate(dst_errp, local_err);
  * Please use ERRP_GUARD() and error_prepend() instead when possible.
  */
+GCC_FMT_ATTR(3, 4)
 void error_propagate_prepend(Error **dst_errp, Error *local_err,
                              const char *fmt, ...);
 
Quoted 2 lines

error_vprepend is one more candidate for GCC_FMT_ATTR. Maybe you can add that, too.

This one is different as it uses a va_list. Now I realize it is only called in util/error.c, and all its callers are guarded with GCC_FMT_ATTR. Maybe we can make it static to simplify... Markus?

Quoted 6 lines

Regards,

Stefan

Reviewed-by
#5 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 6 lines · Philippe Mathieu-Daudé

error_propagate_prepend() "behaves like error_prepend()", and error_prepend() uses "formatting @fmt, ... like printf()". error_prepend() checks its format string argument, but error_propagate_prepend() does not. Fix that.

This would have catched the invalid format introduced in commit

s/catched/caught/

Quoted 9 lines

b98e8d1230f:

CC hw/sd/milkymist-memcard.o
hw/sd/milkymist-memcard.c: In function ‘milkymist_memcard_realize’:
hw/sd/milkymist-memcard.c:284:70: error: format ‘%s’ expects a matching ‘char *’ argument [-Werror=format=]

    284 |         error_propagate_prepend(errp, err, "failed to init SD card: %s");
        |                                                                     ~^
        |                                                                      |
        |                                                                      char *

Suggest to shorten to

This would have caught the bug fixed in the previous commit.

and make sure Stefan's fix actually becomes the previous commit.

Quoted 1 line
Fixes 4b5766488f ("Fix use of error_prepend() with &error_fatal, &error_abort")

Kind of. Omitting the attritbute isn't wrong, just foolish / careless.

I'd write

Missed in commit 4b5766488f "error: Fix use of error_prepend() with &error_fatal, &error_abort".

Quoted 2 lines

If you like my tweaks, I can apply them in my tree.

Quoted 9 lines
---
 include/qapi/error.h | 1 +
 1 file changed, 1 insertion(+)
include/qapi/error.h +0 -0
index 7932594dce..eeeef1a34d 100644

include/qapi/error.h void error_propagate(Error **dst_errp, Error *local_err); · line 386

Quoted 6 lines
include/qapi/error.h +1 -0
@@ -381,6 +381,7 @@ void error_propagate(Error **dst_errp, Error *local_err);
  *     error_propagate(dst_errp, local_err);
  * Please use ERRP_GUARD() and error_prepend() instead when possible.
  */
+GCC_FMT_ATTR(3, 4)
 void error_propagate_prepend(Error **dst_errp, Error *local_err,
                              const char *fmt, ...);
#3 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 26 lines · Philippe Mathieu-Daudé
Quoted 25 lines · Stefan Weil

Am 23.07.20 um 11:13 schrieb Philippe Mathieu-Daudé:

Quoted 23 lines

error_propagate_prepend() "behaves like error_prepend()", and error_prepend() uses "formatting @fmt, ... like printf()". error_prepend() checks its format string argument, but error_propagate_prepend() does not. Fix that.

This would have catched the invalid format introduced in commit b98e8d1230f:

CC hw/sd/milkymist-memcard.o
hw/sd/milkymist-memcard.c: In function ‘milkymist_memcard_realize’:
hw/sd/milkymist-memcard.c:284:70: error: format ‘%s’ expects a matching ‘char *’ argument [-Werror=format=]

    284 |         error_propagate_prepend(errp, err, "failed to init SD card: %s");
        |                                                                     ~^
        |                                                                      |
        |                                                                      char *
Fixes 4b5766488f ("Fix use of error_prepend() with &error_fatal, &error_abort")
---
 include/qapi/error.h | 1 +
 1 file changed, 1 insertion(+)
Quoted 11 lines
Quoted 11 lines

include/qapi/error.h void error_propagate(Error **dst_errp, Error *local_err); · line 386

Quoted 11 lines
include/qapi/error.h +1 -0
index 7932594dce..eeeef1a34d 100644
@@ -381,6 +381,7 @@ void error_propagate(Error **dst_errp, Error *local_err);
  *     error_propagate(dst_errp, local_err);
  * Please use ERRP_GUARD() and error_prepend() instead when possible.
  */
+GCC_FMT_ATTR(3, 4)
 void error_propagate_prepend(Error **dst_errp, Error *local_err,
                              const char *fmt, ...);

Wait! You put the attribute in an unusual place. We have it at the end of the declaration elsewhere in this file. Let's remain locally consistent.

Quoted 8 lines
Quoted 4 lines

error_vprepend is one more candidate for GCC_FMT_ATTR. Maybe you can add that, too.

This one is different as it uses a va_list. Now I realize it is only called in util/error.c, and all its callers are guarded with GCC_FMT_ATTR. Maybe we can make it static to simplify... Markus?

Could use GCC_FMT_ATTR(2, 0). Much less useful, but why not.

Perhaps a respin would be cleaner than me applying multiple tweaks.

#4 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 7 lines · Philippe Mathieu-Daudé
Quoted 3 lines

error_vprepend is one more candidate for GCC_FMT_ATTR. Maybe you can add that, too.

This one is different as it uses a va_list. Now I realize it is only called in util/error.c, and all its callers are guarded with GCC_FMT_ATTR. Maybe we can make it static to simplify... Markus?

Using GCC_FMT_ATTR on va_list functions is just fine; the difference is that you spell its parameters (n, 0) instead of (n, n + 1). As for marking the function static, that was just discussed and rejected:

https://lists.gnu.org/archive/html/qemu-devel/2020-07/msg06730.html

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org