Skip to contentSkip to conversation

hw/arm/aspeed: avoid sign mismatch on sscanf for uart property

PATCH In review Carlo Marcelo Arenas Belón 2 participants 2 messages Aug 2 – Aug 6 Arm/Aspeed machine support

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/arm/aspeed: avoid sign mismatch on sscanf for uart property

using "%u" with sscanf() was likely meant to indicate that a negative value was unexpected, but with a signed variable it could result in undefined behaviour.

use "%d" and check for a negative input explicitly.

---
 hw/arm/aspeed.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/arm/aspeed.c +1 -1
index a48c442058..a9238e6217 100644
@@ -327,7 +327,7 @@ static void aspeed_set_bmc_console(Object *obj, const char *value, Error **errp)
     int uart_first = aspeed_uart_first(sc->uarts_base);
     int uart_last = aspeed_uart_last(sc->uarts_base, sc->uarts_num);
 
-    if (sscanf(value, "uart%u", &val) != 1) {
+    if (sscanf(value, "uart%d", &val) != 1 || val < 0) {
         error_setg(errp, "Bad value for \"uart\" property");
         return;
     }
Signature · 1 line
2.50.1 (Apple Git-155)
Reviewed-by on v1 1/1
#1 lore raw
Quoted 16 lines · Carlo Marcelo Arenas Belón

using "%u" with sscanf() was likely meant to indicate that a negative value was unexpected, but with a signed variable it could result in undefined behaviour.

use "%d" and check for a negative input explicitly.

---
  hw/arm/aspeed.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
hw/arm/aspeed.c +0 -0
index a48c442058..a9238e6217 100644

hw/arm/aspeed.c static void aspeed_set_bmc_console(Object *obj, const char *value, Error **errp) · line 333

Quoted 8 lines
hw/arm/aspeed.c +1 -1
@@ -327,7 +327,7 @@ static void aspeed_set_bmc_console(Object *obj, const char *value, Error **errp)
      int uart_first = aspeed_uart_first(sc->uarts_base);
      int uart_last = aspeed_uart_last(sc->uarts_base, sc->uarts_num);
 
-    if (sscanf(value, "uart%u", &val) != 1) {
+    if (sscanf(value, "uart%d", &val) != 1 || val < 0) {
          error_setg(errp, "Bad value for \"uart\" property");
          return;
      }

The following test :

    if (val < uart_first || val > uart_last) {

would have caught the bad value but it's better to return early.

Thanks,

C.