media: coda: jpeg: Add check for kmalloc
authorJiasheng Jiang <jiasheng@iscas.ac.cn>
Tue, 27 Sep 2022 01:28:13 +0000 (09:28 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sat, 31 Dec 2022 12:14:06 +0000 (13:14 +0100)
[ Upstream commit f30ce3d3760b22ee33c8d9c2e223764ad30bdc5f ]

As kmalloc can return NULL pointer, it should be better to
check the return value and return error, same as
coda_jpeg_decode_header.

Fixes: 96f6f62c4656 ("media: coda: jpeg: add CODA960 JPEG encoder support")
Signed-off-by: Jiasheng Jiang <jiasheng@iscas.ac.cn>
Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
Signed-off-by: Sasha Levin <sashal@kernel.org>
drivers/media/platform/coda/coda-jpeg.c

index a72f4655e5ad56312d8fe4c113452f5b3baebe86..b7bf529f18f774d7561cce6cf259c94d0f924fd0 100644 (file)
@@ -1052,10 +1052,16 @@ static int coda9_jpeg_start_encoding(struct coda_ctx *ctx)
                v4l2_err(&dev->v4l2_dev, "error loading Huffman tables\n");
                return ret;
        }
-       if (!ctx->params.jpeg_qmat_tab[0])
+       if (!ctx->params.jpeg_qmat_tab[0]) {
                ctx->params.jpeg_qmat_tab[0] = kmalloc(64, GFP_KERNEL);
-       if (!ctx->params.jpeg_qmat_tab[1])
+               if (!ctx->params.jpeg_qmat_tab[0])
+                       return -ENOMEM;
+       }
+       if (!ctx->params.jpeg_qmat_tab[1]) {
                ctx->params.jpeg_qmat_tab[1] = kmalloc(64, GFP_KERNEL);
+               if (!ctx->params.jpeg_qmat_tab[1])
+                       return -ENOMEM;
+       }
        coda_set_jpeg_compression_quality(ctx, ctx->params.jpeg_quality);
 
        return 0;