]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
media: v4l2-dv-timings: prevent possible overflow in v4l2_detect_gtf()
authorKarina Yankevich <k.yankevich@omp.ru>
Wed, 21 Aug 2024 11:31:34 +0000 (14:31 +0300)
committerHans Verkuil <hverkuil@xs4all.nl>
Thu, 6 Mar 2025 16:28:27 +0000 (17:28 +0100)
In v4l2_detect_gtf(), it seems safer to cast the 32-bit image_width
variable to the 64-bit type u64 before multiplying to avoid
a possible overflow. The resulting object code even seems to
look better, at least on x86_64.

Found by Linux Verification Center (linuxtesting.org) with Svace.

[Sergey: rewrote the patch subject/descripition]

Fixes: c9bc9f50753d ("[media] v4l2-dv-timings: fix overflow in gtf timings calculation")
Cc: stable@vger.kernel.org
Signed-off-by: Karina Yankevich <k.yankevich@omp.ru>
Signed-off-by: Sergey Shtylyov <s.shtylyov@omp.ru>
Signed-off-by: Hans Verkuil <hverkuil@xs4all.nl>
drivers/media/v4l2-core/v4l2-dv-timings.c

index 6361e11d2be7cd1a509e1d43ba66ed381ea9d013..7710cb26bea0020c4f6ef6615e6a203d6ea472cf 100644 (file)
@@ -764,7 +764,7 @@ bool v4l2_detect_gtf(unsigned int frame_height,
                u64 num;
                u32 den;
 
-               num = ((image_width * GTF_D_C_PRIME * (u64)hfreq) -
+               num = (((u64)image_width * GTF_D_C_PRIME * hfreq) -
                      ((u64)image_width * GTF_D_M_PRIME * 1000));
                den = (hfreq * (100 - GTF_D_C_PRIME) + GTF_D_M_PRIME * 1000) *
                      (2 * GTF_CELL_GRAN);
@@ -774,7 +774,7 @@ bool v4l2_detect_gtf(unsigned int frame_height,
                u64 num;
                u32 den;
 
-               num = ((image_width * GTF_S_C_PRIME * (u64)hfreq) -
+               num = (((u64)image_width * GTF_S_C_PRIME * hfreq) -
                      ((u64)image_width * GTF_S_M_PRIME * 1000));
                den = (hfreq * (100 - GTF_S_C_PRIME) + GTF_S_M_PRIME * 1000) *
                      (2 * GTF_CELL_GRAN);