drm: simpledrm: fix a potential NULL dereference
authorDan Carpenter <dan.carpenter@oracle.com>
Sat, 15 May 2021 09:53:15 +0000 (12:53 +0300)
committerThomas Zimmermann <tzimmermann@suse.de>
Sat, 15 May 2021 12:04:42 +0000 (14:04 +0200)
The drm_format_info() function returns NULL if the format is
unsupported, but the simplefb_get_validated_format() is expected to
return error pointers.  If we propagate the NULL return then it will
lead to a NULL dereference in the callers.  Swap the NULL and trade it
in for an ERR_PTR(-EINVAL).

Fixes: 11e8f5fd223b ("drm: Add simpledrm driver")
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
Signed-off-by: Thomas Zimmermann <tzimmermann@suse.de>
Link: https://patchwork.freedesktop.org/patch/msgid/YJ+aC47XX58ICXax@mwanda
drivers/gpu/drm/tiny/simpledrm.c

index f72ca3a..4f605c5 100644 (file)
@@ -72,6 +72,7 @@ simplefb_get_validated_format(struct drm_device *dev, const char *format_name)
        static const struct simplefb_format formats[] = SIMPLEFB_FORMATS;
        const struct simplefb_format *fmt = formats;
        const struct simplefb_format *end = fmt + ARRAY_SIZE(formats);
+       const struct drm_format_info *info;
 
        if (!format_name) {
                drm_err(dev, "simplefb: missing framebuffer format\n");
@@ -79,8 +80,12 @@ simplefb_get_validated_format(struct drm_device *dev, const char *format_name)
        }
 
        while (fmt < end) {
-               if (!strcmp(format_name, fmt->name))
-                       return drm_format_info(fmt->fourcc);
+               if (!strcmp(format_name, fmt->name)) {
+                       info = drm_format_info(fmt->fourcc);
+                       if (!info)
+                               return ERR_PTR(-EINVAL);
+                       return info;
+               }
                ++fmt;
        }