]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/commitdiff
video: fbdev: fbcvt.c: fix printing in fb_cvt_print_name()
authorDan Carpenter <dan.carpenter@oracle.com>
Thu, 16 Sep 2021 13:29:19 +0000 (16:29 +0300)
committerStefan Bader <stefan.bader@canonical.com>
Fri, 20 May 2022 12:38:14 +0000 (14:38 +0200)
BugLink: https://bugs.launchpad.net/bugs/1969110
[ Upstream commit 78482af095abd9f4f29f1aa3fe575d25c6ae3028 ]

This code has two bugs:
1) "cnt" is 255 but the size of the buffer is 256 so the last byte is
   not used.
2) If we try to print more than 255 characters then "cnt" will be
   negative and that will trigger a WARN() in snprintf(). The fix for
   this is to use scnprintf() instead of snprintf().

We can re-write this code to be cleaner:
1) Rename "offset" to "off" because that's shorter.
2) Get rid of the "cnt" variable and just use "size - off" directly.
3) Get rid of the "read" variable and just increment "off" directly.

Fixes: 96fe6a2109db ("fbdev: Add VESA Coordinated Video Timings (CVT) support")
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
Signed-off-by: Helge Deller <deller@gmx.de>
Signed-off-by: Sasha Levin <sashal@kernel.org>
(cherry picked from commit c3364cbc376202cf7b41e95467f4cc0604c4f627)
Signed-off-by: Paolo Pisati <paolo.pisati@canonical.com>
drivers/video/fbdev/core/fbcvt.c

index 55d2bd0ce5c0229cddf48cb0960a59e31d604060..64843464c66135b4c5c083418c8c89ae237f052a 100644 (file)
@@ -214,9 +214,11 @@ static u32 fb_cvt_aspect_ratio(struct fb_cvt_data *cvt)
 static void fb_cvt_print_name(struct fb_cvt_data *cvt)
 {
        u32 pixcount, pixcount_mod;
-       int cnt = 255, offset = 0, read = 0;
-       u8 *buf = kzalloc(256, GFP_KERNEL);
+       int size = 256;
+       int off = 0;
+       u8 *buf;
 
+       buf = kzalloc(size, GFP_KERNEL);
        if (!buf)
                return;
 
@@ -224,43 +226,30 @@ static void fb_cvt_print_name(struct fb_cvt_data *cvt)
        pixcount_mod = (cvt->xres * (cvt->yres/cvt->interlace)) % 1000000;
        pixcount_mod /= 1000;
 
-       read = snprintf(buf+offset, cnt, "fbcvt: %dx%d@%d: CVT Name - ",
-                       cvt->xres, cvt->yres, cvt->refresh);
-       offset += read;
-       cnt -= read;
+       off += scnprintf(buf + off, size - off, "fbcvt: %dx%d@%d: CVT Name - ",
+                           cvt->xres, cvt->yres, cvt->refresh);
 
-       if (cvt->status)
-               snprintf(buf+offset, cnt, "Not a CVT standard - %d.%03d Mega "
-                        "Pixel Image\n", pixcount, pixcount_mod);
-       else {
-               if (pixcount) {
-                       read = snprintf(buf+offset, cnt, "%d", pixcount);
-                       cnt -= read;
-                       offset += read;
-               }
+       if (cvt->status) {
+               off += scnprintf(buf + off, size - off,
+                                "Not a CVT standard - %d.%03d Mega Pixel Image\n",
+                                pixcount, pixcount_mod);
+       } else {
+               if (pixcount)
+                       off += scnprintf(buf + off, size - off, "%d", pixcount);
 
-               read = snprintf(buf+offset, cnt, ".%03dM", pixcount_mod);
-               cnt -= read;
-               offset += read;
+               off += scnprintf(buf + off, size - off, ".%03dM", pixcount_mod);
 
                if (cvt->aspect_ratio == 0)
-                       read = snprintf(buf+offset, cnt, "3");
+                       off += scnprintf(buf + off, size - off, "3");
                else if (cvt->aspect_ratio == 3)
-                       read = snprintf(buf+offset, cnt, "4");
+                       off += scnprintf(buf + off, size - off, "4");
                else if (cvt->aspect_ratio == 1 || cvt->aspect_ratio == 4)
-                       read = snprintf(buf+offset, cnt, "9");
+                       off += scnprintf(buf + off, size - off, "9");
                else if (cvt->aspect_ratio == 2)
-                       read = snprintf(buf+offset, cnt, "A");
-               else
-                       read = 0;
-               cnt -= read;
-               offset += read;
-
-               if (cvt->flags & FB_CVT_FLAG_REDUCED_BLANK) {
-                       read = snprintf(buf+offset, cnt, "-R");
-                       cnt -= read;
-                       offset += read;
-               }
+                       off += scnprintf(buf + off, size - off, "A");
+
+               if (cvt->flags & FB_CVT_FLAG_REDUCED_BLANK)
+                       off += scnprintf(buf + off, size - off, "-R");
        }
 
        printk(KERN_INFO "%s\n", buf);