[Concept,03/23] expo: Guard against a crash in scene_textline_calc_dims()

Message ID 20250915122905.1217249-4-sjg@u-boot.org
State New
Headers
Series expo: Support interactions with a mouse or touchpad |

Commit Message

Simon Glass Sept. 15, 2025, 12:28 p.m. UTC
  From: Simon Glass <sjg@chromium.org>

If there is no console (e.g. in a test) this function segfaults. Add
the console as a parameter so it is clear that it is needed. Check for
it in the caller.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 boot/scene.c          | 6 +++++-
 boot/scene_internal.h | 5 ++++-
 boot/scene_textline.c | 7 ++++---
 3 files changed, 13 insertions(+), 5 deletions(-)
  

Patch

diff --git a/boot/scene.c b/boot/scene.c
index 564037b0e1a..493f2bbab97 100644
--- a/boot/scene.c
+++ b/boot/scene.c
@@ -1114,7 +1114,11 @@  int scene_calc_dims(struct scene *scn)
 				struct scene_obj_textline *tline;
 
 				tline = (struct scene_obj_textline *)obj;
-				ret = scene_textline_calc_dims(tline);
+				if (!scn->expo->cons)
+					continue;
+
+				ret = scene_textline_calc_dims(tline,
+							       scn->expo->cons);
 				if (ret)
 					return log_msg_ret("men", ret);
 
diff --git a/boot/scene_internal.h b/boot/scene_internal.h
index ba05e05d99a..eada390275c 100644
--- a/boot/scene_internal.h
+++ b/boot/scene_internal.h
@@ -21,6 +21,7 @@  struct scene_obj_menu;
 struct scene_obj_textline;
 struct scene_obj_txtedit;
 struct scene_txt_generic;
+struct udevice;
 struct vidconsole_bbox;
 
 enum scene_obj_t;
@@ -380,9 +381,11 @@  int scene_dims_union(struct scene *scn, uint id, struct scene_obj_dims *dims);
  * Updates the width and height of the textline based on its contents
  *
  * @tline: Textline to update
+ * @cons: UCLASS_VIDEO_CONSOLE device (cannot be NULL)
  * Returns 0 if OK, -ENOTSUPP if there is no graphical console
  */
-int scene_textline_calc_dims(struct scene_obj_textline *tline);
+int scene_textline_calc_dims(struct scene_obj_textline *tline,
+			     struct udevice *cons);
 
 /**
  * scene_menu_calc_bbox() - Calculate bounding boxes for the menu
diff --git a/boot/scene_textline.c b/boot/scene_textline.c
index acaf3d91bbc..4a77cc652a8 100644
--- a/boot/scene_textline.c
+++ b/boot/scene_textline.c
@@ -60,7 +60,8 @@  void scene_textline_calc_bbox(struct scene_obj_textline *tline,
 			 edit_bbox);
 }
 
-int scene_textline_calc_dims(struct scene_obj_textline *tline)
+int scene_textline_calc_dims(struct scene_obj_textline *tline,
+			     struct udevice *cons)
 {
 	struct scene *scn = tline->obj.scene;
 	struct vidconsole_bbox bbox;
@@ -71,8 +72,8 @@  int scene_textline_calc_dims(struct scene_obj_textline *tline)
 	if (!txt)
 		return log_msg_ret("dim", -ENOENT);
 
-	ret = vidconsole_nominal(scn->expo->cons, txt->gen.font_name,
-				 txt->gen.font_size, tline->max_chars, &bbox);
+	ret = vidconsole_nominal(cons, txt->gen.font_name, txt->gen.font_size,
+				 tline->max_chars, &bbox);
 	if (ret)
 		return log_msg_ret("nom", ret);