Skip to contentSkip to conversation

tgafb: potential NULL dereference in init

PATCH Superseded Dan Carpenter 2 participants 3 messages Aug 26, 2013 – Aug 27, 2013

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tgafb: potential NULL dereference in init

Static checkers complain that there are paths where "tga_type_name" can be NULL. I've re-arranged the code slightly so that's impossible.

drivers/video/tgafb.c +3 -4
index c9c8e5a..2dcaf2e 100644
@@ -1475,7 +1475,7 @@ tgafb_init_fix(struct fb_info *info)
 	int tga_bus_pci = TGA_BUS_PCI(par->dev);
 	int tga_bus_tc = TGA_BUS_TC(par->dev);
 	u8 tga_type = par->tga_type;
-	const char *tga_type_name = NULL;
+	const char *tga_type_name;
 
 	switch (tga_type) {
 	case TGA_TYPE_8PLANE:
@@ -1496,10 +1496,9 @@ tgafb_init_fix(struct fb_info *info)
 		if (tga_bus_tc)
 			tga_type_name = "Digital ZLX-E3";
 		break;
-	default:
-		tga_type_name = "Unknown";
-		break;
 	}
+	if (!tga_type_name)
+		tga_type_name = "Unknown";
 
 	strlcpy(info->fix.id, tga_type_name, sizeof(info->fix.id));
 
#1 lore raw

drivers/video/tgafb.c tgafb_init_fix(struct fb_info *info) · line 1478

Quoted 8 lines · Dan Carpenter
drivers/video/tgafb.c +1 -1
@@ -1475,7 +1475,7 @@ tgafb_init_fix(struct fb_info *info)
        int tga_bus_pci = TGA_BUS_PCI(par->dev);
        int tga_bus_tc = TGA_BUS_TC(par->dev);
        u8 tga_type = par->tga_type;
-       const char *tga_type_name = NULL;
+       const char *tga_type_name;

Now the real compiler (at least some versions of gcc) will complain about an uninitialized variable...

drivers/video/tgafb.c tgafb_init_fix(struct fb_info *info) · line 1500

Quoted 11 lines
drivers/video/tgafb.c +1 -3
        switch (tga_type) {
        case TGA_TYPE_8PLANE:
@@ -1496,10 +1496,9 @@ tgafb_init_fix(struct fb_info *info)
                if (tga_bus_tc)
                        tga_type_name = "Digital ZLX-E3";
                break;
-       default:
-               tga_type_name = "Unknown";
-               break;
        }
+       if (!tga_type_name)

It will only by NULL if the garbage on the stack was NULL...

drivers/video/tgafb.c tgafb_init_fix(struct fb_info *info) · line 1503

Quoted 3 lines
drivers/video/tgafb.c +1 -0
+               tga_type_name = "Unknown";
 
        strlcpy(info->fix.id, tga_type_name, sizeof(info->fix.id));

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

#2 lore raw
In reply to Dan Carpenter
Quoted 12 lines · Geert Uytterhoeven

drivers/video/tgafb.c tgafb_init_fix(struct fb_info *info) · line 1478

Quoted 8 lines · Dan Carpenter
drivers/video/tgafb.c +1 -1
@@ -1475,7 +1475,7 @@ tgafb_init_fix(struct fb_info *info)
        int tga_bus_pci = TGA_BUS_PCI(par->dev);
        int tga_bus_tc = TGA_BUS_TC(par->dev);
        u8 tga_type = par->tga_type;
-       const char *tga_type_name = NULL;
+       const char *tga_type_name;

Now the real compiler (at least some versions of gcc) will complain about an uninitialized variable...

Oh crap! The compiler is totally correct here. I don't know what I was thinking. I've just double checked now and my compiler does not catch this (GCC 4.7.2).

Sorry about that.

regards,
dan carpenter