mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@barebox.org>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@barebox.org>
Subject: [PATCH 5/5] fbconsole: reset cursor to origin when changing fonts
Date: Sun,  1 Jun 2025 23:00:02 +0200	[thread overview]
Message-ID: <20250601210002.562733-5-a.fatoum@barebox.org> (raw)
In-Reply-To: <20250601210002.562733-1-a.fatoum@barebox.org>

If we switch from one font to another, we update priv->rows and
priv->cols, but priv->x and priv->y remain at their old values.

This becomes problematic, when a bigger font is chosen, because the
coordinates may now exceed the maximum number of rows and lines,
triggering an out-of-bounds memory access, when they are scaled
according to the new font.

Fix this by resetting to (0, 0) whenever the number of columns and rows
changes. The end result looks neater anyway.

Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
 drivers/video/fbconsole.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/drivers/video/fbconsole.c b/drivers/video/fbconsole.c
index 167569abd5c1..fd220a6c1e05 100644
--- a/drivers/video/fbconsole.c
+++ b/drivers/video/fbconsole.c
@@ -679,6 +679,7 @@ static int setup_font(struct fbc_priv *priv)
 	const struct font_desc *font;
 	unsigned int height = priv->fb->yres - priv->margin.top - priv->margin.bottom;
 	unsigned int width = priv->fb->xres - priv->margin.left - priv->margin.right;
+	unsigned int newrows, newcols;
 
 	font = find_font_enum(priv->par_font_val);
 	if (!font) {
@@ -690,14 +691,22 @@ static int setup_font(struct fbc_priv *priv)
 	switch (priv->rotation) {
 	case FBCONSOLE_ROTATE_0:
 	case FBCONSOLE_ROTATE_180:
-		priv->rows = height / priv->font->height;
-		priv->cols = width / priv->font->width;
+		newrows = height / priv->font->height;
+		newcols = width / priv->font->width;
 		break;
 	case FBCONSOLE_ROTATE_90:
 	case FBCONSOLE_ROTATE_270:
-		priv->rows = width / priv->font->height;
-		priv->cols = height / priv->font->width;
+		newrows = width / priv->font->height;
+		newcols = height / priv->font->width;
 		break;
+	default:
+		return -EINVAL;
+	}
+
+	if (priv->rows != newrows || priv->cols != newcols) {
+		priv->rows = newrows;
+		priv->cols = newcols;
+		priv->x = priv->y = 0;
 	}
 
 	return 0;
-- 
2.39.5




  parent reply	other threads:[~2025-06-01 21:00 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-01 20:59 [PATCH 1/5] fbconsole: use symbolic names for ANSI colors Ahmad Fatoum
2025-06-01 20:59 ` [PATCH 2/5] fbconsole: darken shade of non-bright white Ahmad Fatoum
2025-06-01 21:00 ` [PATCH 3/5] fbconsole: add support for aixterm bright colors Ahmad Fatoum
2025-06-01 21:00 ` [PATCH 4/5] fbconsole: use only one default color Ahmad Fatoum
2025-06-01 21:00 ` Ahmad Fatoum [this message]
2025-06-01 21:02   ` [PATCH 5/5] fbconsole: reset cursor to origin when changing fonts Ahmad Fatoum
2025-06-02  7:42   ` (subset) " Sascha Hauer
2025-06-02  7:42 ` (subset) [PATCH 1/5] fbconsole: use symbolic names for ANSI colors Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250601210002.562733-5-a.fatoum@barebox.org \
    --to=a.fatoum@barebox.org \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox