From: Cosmin Tanislav <demonsingur@gmail.com>
Cc: Sean Young <sean@mess.org>,
Mauro Carvalho Chehab <mchehab@kernel.org>,
linux-media@vger.kernel.org, linux-kernel@vger.kernel.org,
Cosmin Tanislav <demonsingur@gmail.com>
Subject: [PATCH v5 3/3] media: rc: ir-spi: avoid overflow in multiplication
Date: Fri, 13 Jun 2025 14:21:53 +0300 [thread overview]
Message-ID: <20250613112210.22731-4-demonsingur@gmail.com> (raw)
In-Reply-To: <20250613112210.22731-1-demonsingur@gmail.com>
Switch to u64 arithmetic and use DIV_ROUND_CLOSEST_ULL() to avoid
the overflow.
buffer[i] is unsigned int and is limited by the lirc core to
IR_MAX_DURATION, which is 500000.
idata->freq is u32, which has a max value of 0xFFFFFFFF.
In the case where buffer[i] is 500000, idata->freq overflows the u32
multiplication for any values >= 8590.
0xFFFFFFFF / 500000 ~= 8589
By casting buffer[i] to u64, idata->freq can be any u32 value without
overflowing the multiplication.
0xFFFFFFFFFFFFFFFF / 500000 ~= 36893488147419 (> 4294967295)
The result of the final operation will fit back into the unsigned int
limits without any issues.
500000 * 0xFFFFFFFF / 1000000 = 0x80000000 (< 0xFFFFFFFF)
Signed-off-by: Cosmin Tanislav <demonsingur@gmail.com>
---
drivers/media/rc/ir-spi.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/drivers/media/rc/ir-spi.c b/drivers/media/rc/ir-spi.c
index 0b54ad74cec0..392441e0c116 100644
--- a/drivers/media/rc/ir-spi.c
+++ b/drivers/media/rc/ir-spi.c
@@ -46,7 +46,8 @@ static int ir_spi_tx(struct rc_dev *dev, unsigned int *buffer, unsigned int coun
/* convert the pulse/space signal to raw binary signal */
for (i = 0; i < count; i++) {
- buffer[i] = DIV_ROUND_CLOSEST(buffer[i] * idata->freq, 1000000);
+ buffer[i] = DIV_ROUND_CLOSEST_ULL((u64)buffer[i] * idata->freq,
+ 1000000);
len += buffer[i];
}
--
2.49.0
next prev parent reply other threads:[~2025-06-13 11:22 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-13 11:21 [PATCH v5 0/3] media: rc: ir-spi: allocate buffer dynamically Cosmin Tanislav
2025-06-13 11:21 ` [PATCH v5 1/3] " Cosmin Tanislav
2025-06-13 11:21 ` [PATCH v5 2/3] media: rc: ir-spi: constrain carrier frequency Cosmin Tanislav
2025-06-13 11:21 ` Cosmin Tanislav [this message]
2025-06-20 16:45 ` [PATCH v5 0/3] media: rc: ir-spi: allocate buffer dynamically Sean Young
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250613112210.22731-4-demonsingur@gmail.com \
--to=demonsingur@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-media@vger.kernel.org \
--cc=mchehab@kernel.org \
--cc=sean@mess.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®