From e0476cde80f672268e2adc3a4fd2affe4b4bbf94 Mon Sep 17 00:00:00 2001 From: Ritvik Date: Fri, 24 Nov 2023 20:31:15 -0500 Subject: [PATCH] Check return value of sscanf to handle potential undefined behaviours in write_keymaps --- util/grub-mklayout.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/util/grub-mklayout.c b/util/grub-mklayout.c index d171c2700f..2eacf80445 100644 --- a/util/grub-mklayout.c +++ b/util/grub-mklayout.c @@ -380,9 +380,16 @@ write_keymaps (FILE *in, FILE *out, const char *out_filename) char shift[64]; char normalalt[64]; char shiftalt[64]; - - sscanf (line, "keycode %u = %60s %60s %60s %60s", &keycode_linux, - normal, shift, normalalt, shiftalt); + + if (sscanf (line, "keycode %u = %60s %60s %60s %60s", &keycode_linux, + normal, shift, normalalt, shiftalt) != 5) { + /* Bail out since keycodes could not be read, this can happen + * when the in FILE is coming from stdin and user fails to specify the keycode in + * proper format + */ + fprintf (stderr, "%s", _("ERROR: no valid keyboard layout found. Check the input.\n")); + exit (1); + } if (keycode_linux >= ARRAY_SIZE (linux_to_usb_map)) {