Skip to contentSkip to conversation

video/logo: protect against divide by zero when reading image

PATCH Superseded Yiyuan GUO 2 participants 3 messages May 12, 2021 – May 13, 2021 Framebuffer layer

lore
Share & tools
lore

Yiyuan GUO

author
#0 lore raw

PATCH 1/1 video/logo: protect against divide by zero when reading image

In video/logo/pnmtologo.c, the function read_image can read from the image file an integer 0 and pass it to function get_number255, leading to a divide by zero problem.

---
 drivers/video/logo/pnmtologo.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/video/logo/pnmtologo.c +6 -1
index 4718d7895..cc8dca5ef 100644
@@ -118,7 +118,12 @@ static unsigned int get_number(FILE *fp)
 
 static unsigned int get_number255(FILE *fp, unsigned int maxval)
 {
-    unsigned int val = get_number(fp);
+    unsigned int val;
+
+    if (!maxval)
+	die("Corrupted maxval\n");
+
+    val = get_number(fp);
     return (255*val+maxval/2)/maxval;
 }
 
Signature · 1 line
2.25.1
#1 lore raw

Hi Yiyuan,

CC dri-devel, linux-fbdev

Quoted 5 lines · Yiyuan GUO

In video/logo/pnmtologo.c, the function read_image can read from the image file an integer 0 and pass it to function get_number255, leading to a divide by zero problem.

Thanks for your patch!

drivers/video/logo/pnmtologo.c static unsigned int get_number(FILE *fp) · line 124

Quoted 11 lines
drivers/video/logo/pnmtologo.c +4 -1
@@ -118,7 +118,12 @@ static unsigned int get_number(FILE *fp)
 
 static unsigned int get_number255(FILE *fp, unsigned int maxval)
 {
-    unsigned int val = get_number(fp);
+    unsigned int val;
+
+    if (!maxval)
+	die("Corrupted maxval\n");

Please be consistent with other places reporting errors, e.g.

    die("%s: invalid maxval zero\n", filename);

This looks like a strange place to check the validity of maxval. What about checking if right after its assignment?
To avoid duplicating code, you can create a helper:

    static unsigned int get_maxval(FILE *fp)
    {
	unsigned int maxval = get_number(fp);

	if (!maxval)
	    die("%s: invalid maxval zero\n", filename);

	return maxval;
    }

and:

    /* Plain PGM */

- maxval = get_number(fp);
+ maxval = get_maxval(fp);

and:

    /* Plain PPM */

- maxval = get_number(fp);
+ maxval = get_maxval(fp);

drivers/video/logo/pnmtologo.c static unsigned int get_number(FILE *fp) · line 128

Quoted 4 lines
drivers/video/logo/pnmtologo.c +2 -0
+
+    val = get_number(fp);
    return (255*val+maxval/2)/maxval;
 }

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

author
#2 lore raw

PATCH v2 1/1 video/logo: protect against divide by zero when reading image

In video/logo/pnmtologo.c, the function read_image can read from the image file an integer 0 and pass it to function get_number255, leading to a divide by zero problem.

---
 drivers/video/logo/pnmtologo.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
drivers/video/logo/pnmtologo.c +12 -2
index 4718d7895..d9e8d1d5b 100644
@@ -116,6 +116,16 @@ static unsigned int get_number(FILE *fp)
     return val;
 }
 
+static unsigned int get_maxval(FILE *fp)
+{
+    unsigned int maxval = get_number(fp);
+
+    if (!maxval)
+	die("%s: invalid maxval zero\n", filename);
+
+    return maxval;
+}
+
 static unsigned int get_number255(FILE *fp, unsigned int maxval)
 {
     unsigned int val = get_number(fp);
@@ -182,7 +192,7 @@ static void read_image(void)
 
 	case '2':
 	    /* Plain PGM */
-	    maxval = get_number(fp);
+	    maxval = get_maxval(fp);
 	    for (i = 0; i < logo_height; i++)
 		for (j = 0; j < logo_width; j++)
 		    logo_data[i][j].red = logo_data[i][j].green =
@@ -191,7 +201,7 @@ static void read_image(void)
 
 	case '3':
 	    /* Plain PPM */
-	    maxval = get_number(fp);
+	    maxval = get_maxval(fp);
 	    for (i = 0; i < logo_height; i++)
 		for (j = 0; j < logo_width; j++) {
 		    logo_data[i][j].red = get_number255(fp, maxval);
Signature · 1 line
2.25.1