Skip to contentSkip to conversation

kern/misc: Move grub_printf_fmt_check to gfxmenu

PATCH In review Colin Watson 2 participants 2 messages Mar 20, 2021 – Mar 22, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kern/misc: Move grub_printf_fmt_check to gfxmenu

This function is only used by gfxmenu. Moving it there is unfortunately a bit messy, but it frees up a fairly substantial amount (in relative terms) of precious core image space on i386-pc.

DOWN: obj/i386-pc/grub-core/kernel.img (31740 > 31528) - change: -212
UP: obj/i386-pc/grub-core/gfxmenu.mod (39816 < 40216) - change: 400
DOWN: i386-pc core image (biosdisk ext2 part_msdos) (27453 > 27351) - change: -102
DOWN: i386-pc core image (biosdisk ext2 part_msdos diskfilter mdraid09) (32367 > 32259) - change: -108

---
 grub-core/gfxmenu/gui_string_util.c | 36 +++++++++++
 grub-core/kern/misc.c               | 99 +++++++----------------------
 include/grub/gui_string_util.h      | 16 +++++
 include/grub/misc.h                 | 41 +++++++-----
 4 files changed, 100 insertions(+), 92 deletions(-)
grub-core/gfxmenu/gui_string_util.c +36 -0
index ba1e1eab3..119802326 100644
@@ -204,3 +204,39 @@ grub_get_dirname (const char *file_path)
 
   return grub_new_substring (file_path, 0, last_slash + 1);
 }
+
+grub_err_t
+grub_printf_fmt_check (const char *fmt, const char *fmt_expected)
+{
+  struct grub_printf_args args_expected, args_fmt;
+  grub_err_t ret;
+  grub_size_t n;
+
+  if (fmt == NULL || fmt_expected == NULL)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid format");
+
+  ret = grub_parse_printf_arg_fmt (fmt_expected, &args_expected, 1,
+				   GRUB_SIZE_MAX);
+  if (ret != GRUB_ERR_NONE)
+    return ret;
+
+  /* Limit parsing to the number of expected arguments. */
+  ret = grub_parse_printf_arg_fmt (fmt, &args_fmt, 1, args_expected.count);
+  if (ret != GRUB_ERR_NONE)
+    {
+      grub_free_printf_args (&args_expected);
+      return ret;
+    }
+
+  for (n = 0; n < args_fmt.count; n++)
+    if (args_fmt.ptr[n].type != args_expected.ptr[n].type)
+     {
+	ret = grub_error (GRUB_ERR_BAD_ARGUMENT, "arguments types do not match");
+	break;
+     }
+
+  grub_free_printf_args (&args_expected);
+  grub_free_printf_args (&args_fmt);
+
+  return ret;
+}
grub-core/kern/misc.c +22 -77

180 lines not on this page · raw message

include/grub/gui_string_util.h +16 -0
index 34f9a090d..5cb444f18 100644
@@ -30,4 +30,20 @@ char *grub_resolve_relative_path (const char *base, const char *path);
 
 char *grub_get_dirname (const char *file_path);
 
+/*
+ * grub_printf_fmt_check() checks a fmt string for printf() against an
+ * expected format. It is intended for cases where the fmt string could come
+ * from an outside source and cannot be trusted.
+ *
+ * While expected fmt accepts a printf() format string it should be kept
+ * as simple as possible. The printf() format strings with positional
+ * parameters are NOT accepted, neither for fmt nor for fmt_expected.
+ *
+ * The fmt is accepted if it has equal or less arguments than fmt_expected
+ * and if the type of all arguments match.
+ *
+ * Returns GRUB_ERR_NONE if fmt is acceptable.
+ */
+grub_err_t grub_printf_fmt_check (const char *fmt, const char *fmt_expected);
+
 #endif /* GRUB_GUI_STRING_UTIL_HEADER */
include/grub/misc.h +26 -15

47 lines not on this page · raw message

Signature · 1 line
2.25.1
#1 lore raw
Quoted 9 lines · Colin Watson

This function is only used by gfxmenu. Moving it there is unfortunately a bit messy, but it frees up a fairly substantial amount (in relative terms) of precious core image space on i386-pc.

DOWN: obj/i386-pc/grub-core/kernel.img (31740 > 31528) - change: -212
UP: obj/i386-pc/grub-core/gfxmenu.mod (39816 < 40216) - change: 400
DOWN: i386-pc core image (biosdisk ext2 part_msdos) (27453 > 27351) - change: -102
DOWN: i386-pc core image (biosdisk ext2 part_msdos diskfilter mdraid09) (32367 > 32259) - change: -108

Thank you for the size analysis. Size constraints on i386-pc was something I had not considered when I wrote the original patch.