Skip to contentSkip to conversation

fix potential integer overflows

PATCH RESEND In review Gustavo A. R. Silva 4 participants 18 messages Jan 30, 2018 – Feb 2, 2018

Subsystems (8)
lore
Share & tools
lore
#0 lore raw

cover fix potential integer overflows

This patchset aims to fix potential integer overflows reported by Coverity.

In all cases the potential overflowing expressions are evaluated using 32-bit arithmetic before being used in contexts that expect a 64-bit arithmetic. So a cast to the proper type was added to each of those expressions in order to avoid any potential integer overflow.

This also gives the compiler complete information about the proper arithmetic for each expression and improves code quality.

Addresses the following Coverity IDs reported as
"Unintentional integer overflow" issues:

200604, 1056807, 1056808, 1271223,
1324146, 1392628, 1392630, 1446589,
1454996, 1458347.

Thank you

Gustavo A. R. Silva (8):
rtl2832: fix potential integer overflow
dvb-frontends: ves1820: fix potential integer overflow
i2c: max2175: fix potential integer overflow in max2175_set_nco_freq
i2c: ov9650: fix potential integer overflow in

    __ov965x_set_frame_interval

pci: cx88-input: fix potential integer overflow rockchip/rga: fix potential integer overflow in rga_buf_map
platform: sh_veu: fix potential integer overflow in
sh_veu_colour_offset
platform: vivid-cec: fix potential integer overflow in
vivid_cec_pin_adap_events

 drivers/media/dvb-frontends/rtl2832.c         | 4 ++--
 drivers/media/dvb-frontends/ves1820.c         | 2 +-
 drivers/media/i2c/max2175.c                   | 2 +-
 drivers/media/i2c/ov9650.c                    | 2 +-
 drivers/media/pci/cx88/cx88-input.c           | 4 ++--
 drivers/media/platform/rockchip/rga/rga-buf.c | 2 +-
 drivers/media/platform/sh_veu.c               | 4 ++--
 drivers/media/platform/vivid/vivid-cec.c      | 2 +-
 8 files changed, 11 insertions(+), 11 deletions(-)
Signature · 1 line
2.7.4
#1 lore raw

PATCH 1/8 rtl2832: fix potential integer overflow in rtl2832_set_frontend

Cast dev->pdata->clk to u64 in order to avoid a potential integer overflow. This variable is being used in a context that expects an expression of type u64.

Addresses-Coverity-ID: 1271223 ("Unintentional integer overflow")

---
 drivers/media/dvb-frontends/rtl2832.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/dvb-frontends/rtl2832.c +2 -2
index 94bf5b7..eefc301 100644
@@ -498,7 +498,7 @@ static int rtl2832_set_frontend(struct dvb_frontend *fe)
 	* RSAMP_RATIO = floor(CrystalFreqHz * 7 * pow(2, 22)
 	*	/ ConstWithBandwidthMode)
 	*/
-	num = dev->pdata->clk * 7;
+	num = (u64)dev->pdata->clk * 7;
 	num *= 0x400000;
 	num = div_u64(num, bw_mode);
 	resamp_ratio =  num & 0x3ffffff;
@@ -511,7 +511,7 @@ static int rtl2832_set_frontend(struct dvb_frontend *fe)
 	*	/ (CrystalFreqHz * 7))
 	*/
 	num = bw_mode << 20;
-	num2 = dev->pdata->clk * 7;
+	num2 = (u64)dev->pdata->clk * 7;
 	num = div_u64(num, num2);
 	num = -num;
 	cfreq_off_ratio = num & 0xfffff;
Signature · 1 line
2.7.4
#2 lore raw

PATCH 2/8 dvb-frontends: ves1820: fix potential integer overflow

In reply to Gustavo A. R. Silva

Cast state->config->xin to u64 in order to avoid a potential integer overflow. This variable is being used in a context that expects an expression of type u64.

Addresses-Coverity-ID: 200604 ("Unintentional integer overflow")

---
 drivers/media/dvb-frontends/ves1820.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/dvb-frontends/ves1820.c +1 -1
index 1d89792..9dbd582 100644
@@ -137,7 +137,7 @@ static int ves1820_set_symbolrate(struct ves1820_state *state, u32 symbolrate)
 		NDEC = 3;
 
 	/* yeuch! */
-	fpxin = state->config->xin * 10;
+	fpxin = (u64)state->config->xin * 10;
 	fptmp = fpxin; do_div(fptmp, 123);
 	if (symbolrate < fptmp)
 		SFIL = 1;
Signature · 1 line
2.7.4
#3 lore raw

PATCH 3/8 i2c: max2175: fix potential integer overflow in max2175_set_nco_freq

In reply to Gustavo A. R. Silva

Cast expression (clock_rate - abs_nco_freq) to s64 in order to avoid a potential integer overflow. This variable is being used in a context that expects an expression of type s64.

Addresses-Coverity-ID: 1446589 ("Unintentional integer overflow")

---
 drivers/media/i2c/max2175.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/i2c/max2175.c +1 -1
index 2f1966b..3401ae6 100644
@@ -643,7 +643,7 @@ static int max2175_set_nco_freq(struct max2175 *ctx, s32 nco_freq)
 	if (abs_nco_freq < clock_rate / 2) {
 		nco_val_desired = 2 * nco_freq;
 	} else {
-		nco_val_desired = 2 * (clock_rate - abs_nco_freq);
+		nco_val_desired = 2 * (s64)(clock_rate - abs_nco_freq);
 		if (nco_freq < 0)
 			nco_val_desired = -nco_val_desired;
 	}
Signature · 1 line
2.7.4
#4 lore raw

PATCH 4/8 i2c: ov9650: fix potential integer overflow in __ov965x_set_frame_interval

In reply to Gustavo A. R. Silva

Cast fi->interval.numerator to u64 in order to avoid a potential integer overflow. This variable is being used in a context that expects an expression of type u64.

Addresses-Coverity-ID: 1324146 ("Unintentional integer overflow")

---
 drivers/media/i2c/ov9650.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/i2c/ov9650.c +1 -1
index e519f27..c674a49 100644
@@ -1130,7 +1130,7 @@ static int __ov965x_set_frame_interval(struct ov965x *ov965x,
 	if (fi->interval.denominator == 0)
 		return -EINVAL;
 
-	req_int = (u64)(fi->interval.numerator * 10000) /
+	req_int = (u64)fi->interval.numerator * 10000 /
 		fi->interval.denominator;
 
 	for (i = 0; i < ARRAY_SIZE(ov965x_intervals); i++) {
Signature · 1 line
2.7.4
#5 lore raw

Re: [PATCH 4/8] i2c: ov9650: fix potential integer overflow in __ov965x_set_frame_interval

Quoted 15 lines · Gustavo A. R. Silva

Cast fi->interval.numerator to u64 in order to avoid a potential integer overflow. This variable is being used in a context that expects an expression of type u64.

Addresses-Coverity-ID: 1324146 ("Unintentional integer overflow")

---
 drivers/media/i2c/ov9650.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/i2c/ov9650.c +0 -0
index e519f27..c674a49 100644

drivers/media/i2c/ov9650.c static int __ov965x_set_frame_interval(struct ov965x *ov965x, · line 1134

Quoted 6 lines
drivers/media/i2c/ov9650.c +1 -1
@@ -1130,7 +1130,7 @@ static int __ov965x_set_frame_interval(struct ov965x *ov965x,
 	if (fi->interval.denominator == 0)
 		return -EINVAL;
 
-	req_int = (u64)(fi->interval.numerator * 10000) /
+	req_int = (u64)fi->interval.numerator * 10000 /
 		fi->interval.denominator;

This requires do_div(). I've applied the patch with this change:

drivers/media/i2c/ov9650.c +2 -2
index 88276dba828d..5bea31cd41aa 100644
@@ -1136,8 +1136,8 @@ static int __ov965x_set_frame_interval(struct ov965x *ov965x,
 	if (fi->interval.denominator == 0)
 		return -EINVAL;
 
-	req_int = (u64)fi->interval.numerator * 10000 /
-		fi->interval.denominator;
+	req_int = (u64)fi->interval.numerator * 10000;
+	do_div(req_int, fi->interval.denominator);
 
 	for (i = 0; i < ARRAY_SIZE(ov965x_intervals); i++) {
 		const struct ov965x_interval *iv = &ov965x_intervals[i];
Quoted 5 lines
drivers/media/i2c/ov9650.c +0 -0
 	for (i = 0; i < ARRAY_SIZE(ov965x_intervals); i++) {
Signature · 1 line
2.7.4
Signature · 2 lines
Sakari Ailus
e-mail: [email protected]
#6 lore raw

PATCH 5/8 pci: cx88-input: fix potential integer overflow

In reply to Gustavo A. R. Silva

Cast ir->polling to ktime_t in order to avoid a potential integer overflow. This variable is being used in a context that expects an expression of type ktime_t (s64).

Addresses-Coverity-ID: 1392628 ("Unintentional integer overflow")
Addresses-Coverity-ID: 1392630 ("Unintentional integer overflow")

---
 drivers/media/pci/cx88/cx88-input.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/pci/cx88/cx88-input.c +2 -2
index 4e9953e..096b350 100644
@@ -180,7 +180,7 @@ static enum hrtimer_restart cx88_ir_work(struct hrtimer *timer)
 	struct cx88_IR *ir = container_of(timer, struct cx88_IR, timer);
 
 	cx88_ir_handle_key(ir);
-	missed = hrtimer_forward_now(&ir->timer, ir->polling * 1000000);
+	missed = hrtimer_forward_now(&ir->timer, (ktime_t)ir->polling * 1000000);
 	if (missed > 1)
 		ir_dprintk("Missed ticks %ld\n", missed - 1);
 
@@ -200,7 +200,7 @@ static int __cx88_ir_start(void *priv)
 	if (ir->polling) {
 		hrtimer_init(&ir->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
 		ir->timer.function = cx88_ir_work;
-		hrtimer_start(&ir->timer, ir->polling * 1000000,
+		hrtimer_start(&ir->timer, (ktime_t)ir->polling * 1000000,
 			      HRTIMER_MODE_REL);
 	}
 	if (ir->sampling) {
Signature · 1 line
2.7.4
#7 lore raw

PATCH 6/8 rockchip/rga: fix potential integer overflow in rga_buf_map

In reply to Gustavo A. R. Silva

Cast p to dma_addr_t in order to avoid a potential integer overflow. This variable is being used in a context that expects an expression of type dma_addr_t (u64).

Addresses-Coverity-ID: 1458347 ("Unintentional integer overflow")

---
 drivers/media/platform/rockchip/rga/rga-buf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/platform/rockchip/rga/rga-buf.c +1 -1
index 49cacc7..3dd29b2 100644
@@ -140,7 +140,7 @@ void rga_buf_map(struct vb2_buffer *vb)
 		address = sg_phys(sgl);
 
 		for (p = 0; p < len; p++) {
-			dma_addr_t phys = address + (p << PAGE_SHIFT);
+			dma_addr_t phys = address + ((dma_addr_t)p << PAGE_SHIFT);
 
 			pages[mapped_size + p] = phys;
 		}
Signature · 1 line
2.7.4
#8 lore raw

PATCH 7/8 platform: sh_veu: fix potential integer overflow in sh_veu_colour_offset

In reply to Gustavo A. R. Silva

Cast left and top to dma_addr_t in order to avoid a potential integer overflow. This variable is being used in a context that expects an expression of type dma_addr_t (u64).

Addresses-Coverity-ID: 1056807 ("Unintentional integer overflow")
Addresses-Coverity-ID: 1056808 ("Unintentional integer overflow")

---
 drivers/media/platform/sh_veu.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/platform/sh_veu.c +2 -2
index 976ea0b..e2795d0 100644
@@ -520,8 +520,8 @@ static void sh_veu_colour_offset(struct sh_veu_dev *veu, struct sh_veu_vfmt *vfm
 	/* dst_left and dst_top validity will be verified in CROP / COMPOSE */
 	unsigned int left = vfmt->frame.left & ~0x03;
 	unsigned int top = vfmt->frame.top;
-	dma_addr_t offset = ((left * veu->vfmt_out.fmt->depth) >> 3) +
-		top * veu->vfmt_out.bytesperline;
+	dma_addr_t offset = (((dma_addr_t)left * veu->vfmt_out.fmt->depth) >> 3) +
+			    (dma_addr_t)top * veu->vfmt_out.bytesperline;
 	unsigned int y_line;
 
 	vfmt->offset_y = offset;
Signature · 1 line
2.7.4
#9 lore raw

PATCH 8/8 platform: vivid-cec: fix potential integer overflow in vivid_cec_pin_adap_events

In reply to Gustavo A. R. Silva

Cast len to const u64 in order to avoid a potential integer overflow. This variable is being used in a context that expects an expression of type const u64.

Addresses-Coverity-ID: 1454996 ("Unintentional integer overflow")

---
 drivers/media/platform/vivid/vivid-cec.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/platform/vivid/vivid-cec.c +1 -1
index b55d278..30240ab 100644
@@ -83,7 +83,7 @@ static void vivid_cec_pin_adap_events(struct cec_adapter *adap, ktime_t ts,
 	if (adap == NULL)
 		return;
 	ts = ktime_sub_us(ts, (CEC_TIM_START_BIT_TOTAL +
-			       len * 10 * CEC_TIM_DATA_BIT_TOTAL));
+			       (const u64)len * 10 * CEC_TIM_DATA_BIT_TOTAL));
 	cec_queue_pin_cec_event(adap, false, ts);
 	ts = ktime_add_us(ts, CEC_TIM_START_BIT_LOW);
 	cec_queue_pin_cec_event(adap, true, ts);
Signature · 1 line
2.7.4
#10 lore raw

Re: [PATCH 8/8] platform: vivid-cec: fix potential integer overflow in vivid_cec_pin_adap_events

Hi Gustavo,

Quoted 15 lines · Gustavo A. R. Silva

Cast len to const u64 in order to avoid a potential integer overflow. This variable is being used in a context that expects an expression of type const u64.

Addresses-Coverity-ID: 1454996 ("Unintentional integer overflow")

---
 drivers/media/platform/vivid/vivid-cec.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/platform/vivid/vivid-cec.c +0 -0
index b55d278..30240ab 100644

drivers/media/platform/vivid/vivid-cec.c static void vivid_cec_pin_adap_events(struct cec_adapter *adap, ktime_t ts, · line 86

Quoted 5 lines
drivers/media/platform/vivid/vivid-cec.c +1 -1
@@ -83,7 +83,7 @@ static void vivid_cec_pin_adap_events(struct cec_adapter *adap, ktime_t ts,
 	if (adap == NULL)
 		return;
 	ts = ktime_sub_us(ts, (CEC_TIM_START_BIT_TOTAL +
-			       len * 10 * CEC_TIM_DATA_BIT_TOTAL));
+			       (const u64)len * 10 * CEC_TIM_DATA_BIT_TOTAL));

This makes no sense. Certainly the const part is pointless. And given that len is always <= 16 there definitely is no overflow.

I don't really want this cast in the code.

Sorry,

Hans

Quoted 4 lines
drivers/media/platform/vivid/vivid-cec.c +0 -0
 	cec_queue_pin_cec_event(adap, false, ts);
 	ts = ktime_add_us(ts, CEC_TIM_START_BIT_LOW);
 	cec_queue_pin_cec_event(adap, true, ts);
#11 lore raw

Re: [PATCH 8/8] platform: vivid-cec: fix potential integer overflow in vivid_cec_pin_adap_events

Hi Hans,

Quoting Hans Verkuil <[email protected]>:

Quoted 13 lines

Hi Gustavo,

Quoted 10 lines · Gustavo A. R. Silva

Cast len to const u64 in order to avoid a potential integer overflow. This variable is being used in a context that expects an expression of type const u64.

Addresses-Coverity-ID: 1454996 ("Unintentional integer overflow")

---
 drivers/media/platform/vivid/vivid-cec.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 15 lines

drivers/media/platform/vivid/vivid-cec.c static void vivid_cec_pin_adap_events(struct · line 87

Quoted 11 lines
drivers/media/platform/vivid/vivid-cec.c +1 -1
index b55d278..30240ab 100644
@@ -83,7 +83,7 @@ static void vivid_cec_pin_adap_events(struct  
 cec_adapter *adap, ktime_t ts,
 	if (adap == NULL)
 		return;
 	ts = ktime_sub_us(ts, (CEC_TIM_START_BIT_TOTAL +
-			       len * 10 * CEC_TIM_DATA_BIT_TOTAL));
+			       (const u64)len * 10 * CEC_TIM_DATA_BIT_TOTAL));

This makes no sense. Certainly the const part is pointless. And given that len is always <= 16 there definitely is no overflow.

Yeah, I understand your point and I know there is no chance of an overflow in this particular case.

Quoted 4 lines

I don't really want this cast in the code.

Sorry,

I'm working through all the Linux kernel Coverity reports, and I thought of sending a patch for this because IMHO it doesn't hurt to give the compiler complete information about the arithmetic in which an expression is intended to be evaluated.

I agree that the _const_ part is a bit odd. What do you think about the cast to u64 alone?

I appreciate your feedback.

Thanks
--
Gustavo

#12 lore raw

Re: [PATCH 8/8] platform: vivid-cec: fix potential integer overflow in vivid_cec_pin_adap_events

Quoted 42 lines · Gustavo A. R. Silva

Hi Hans,

Quoting Hans Verkuil <[email protected]>:

Quoted 13 lines

Hi Gustavo,

Quoted 10 lines · Gustavo A. R. Silva

Cast len to const u64 in order to avoid a potential integer overflow. This variable is being used in a context that expects an expression of type const u64.

Addresses-Coverity-ID: 1454996 ("Unintentional integer overflow")

---
 drivers/media/platform/vivid/vivid-cec.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 16 lines

drivers/media/platform/vivid/vivid-cec.c static void vivid_cec_pin_adap_events(struct · line 87

Quoted 12 lines
drivers/media/platform/vivid/vivid-cec.c +1 -1
index b55d278..30240ab 100644
@@ -83,7 +83,7 @@ static void vivid_cec_pin_adap_events(struct  
 cec_adapter *adap, ktime_t ts,
 	if (adap == NULL)
 		return;
 	ts = ktime_sub_us(ts, (CEC_TIM_START_BIT_TOTAL +
-			       len * 10 * CEC_TIM_DATA_BIT_TOTAL));
+			       (const u64)len * 10 * CEC_TIM_DATA_BIT_TOTAL));

This makes no sense. Certainly the const part is pointless. And given that len is always <= 16 there definitely is no overflow.

Yeah, I understand your point and I know there is no chance of an overflow in this particular case.

Quoted 4 lines

I don't really want this cast in the code.

Sorry,

Quoted 7 lines

I'm working through all the Linux kernel Coverity reports, and I thought of sending a patch for this because IMHO it doesn't hurt to give the compiler complete information about the arithmetic in which an expression is intended to be evaluated.

I agree that the _const_ part is a bit odd. What do you think about the cast to u64 alone?

What happens if you do: ((u64)CEC_TIM_START_BIT_TOTAL +

I think that forces everything else in the expression to be evaluated as u64.

It definitely needs a comment that this fixes a bogus Coverity report.

Regards,

Hans

Quoted 11 lines

I appreciate your feedback.

Thanks
--
Gustavo

#13 lore raw

Re: [PATCH 8/8] platform: vivid-cec: fix potential integer overflow in vivid_cec_pin_adap_events

Quoting Hans Verkuil <[email protected]>:

Quoted 48 lines · Gustavo A. R. Silva
Quoted 47 lines

Hi Hans,

Quoting Hans Verkuil <[email protected]>:

Quoted 13 lines

Hi Gustavo,

Quoted 10 lines · Gustavo A. R. Silva

Cast len to const u64 in order to avoid a potential integer overflow. This variable is being used in a context that expects an expression of type const u64.

Addresses-Coverity-ID: 1454996 ("Unintentional integer overflow")

---
 drivers/media/platform/vivid/vivid-cec.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 16 lines

drivers/media/platform/vivid/vivid-cec.c static void vivid_cec_pin_adap_events(struct · line 87

Quoted 12 lines
drivers/media/platform/vivid/vivid-cec.c +1 -1
index b55d278..30240ab 100644
@@ -83,7 +83,7 @@ static void vivid_cec_pin_adap_events(struct
 cec_adapter *adap, ktime_t ts,
 	if (adap == NULL)
 		return;
 	ts = ktime_sub_us(ts, (CEC_TIM_START_BIT_TOTAL +
-			       len * 10 * CEC_TIM_DATA_BIT_TOTAL));
+			       (const u64)len * 10 * CEC_TIM_DATA_BIT_TOTAL));

This makes no sense. Certainly the const part is pointless. And given that len is always <= 16 there definitely is no overflow.

Yeah, I understand your point and I know there is no chance of an overflow in this particular case.

Quoted 4 lines

I don't really want this cast in the code.

Sorry,

I'm working through all the Linux kernel Coverity reports, and I thought of sending a patch for this because IMHO it doesn't hurt to give the compiler complete information about the arithmetic in which an expression is intended to be evaluated.

Quoted 8 lines
Quoted 2 lines

I agree that the _const_ part is a bit odd. What do you think about the cast to u64 alone?

What happens if you do: ((u64)CEC_TIM_START_BIT_TOTAL +

I think that forces everything else in the expression to be evaluated as u64.

Well, in this case the operator precedence takes place and the expression len * 10 * CEC_TIM_DATA_BIT_TOTAL is computed first. So the issue remains the same.

I can switch the expressions as follows:

(u64)len * 10 * CEC_TIM_DATA_BIT_TOTAL + CEC_TIM_START_BIT_TOTAL

and avoid the cast in the middle.

What do you think?

Quoted 2 lines

It definitely needs a comment that this fixes a bogus Coverity report.

I actually added the following line to the message changelog:
Addresses-Coverity-ID: 1454996 ("Unintentional integer overflow")

Certainly, I've run across multiple false positives as in this case, but I have also fixed many actual bugs thanks to the Coverity reports. So I think in general it is valuable to take a look into these reports, either if they spot actual bugs or promote code correctness.

Thanks
--
Gustavo

#14 lore raw

Re: [PATCH 8/8] platform: vivid-cec: fix potential integer overflow in vivid_cec_pin_adap_events

Quoted 60 lines · Gustavo A. R. Silva

Quoting Hans Verkuil <[email protected]>:

Quoted 48 lines · Gustavo A. R. Silva
Quoted 47 lines

Hi Hans,

Quoting Hans Verkuil <[email protected]>:

Quoted 13 lines

Hi Gustavo,

Quoted 10 lines · Gustavo A. R. Silva

Cast len to const u64 in order to avoid a potential integer overflow. This variable is being used in a context that expects an expression of type const u64.

Addresses-Coverity-ID: 1454996 ("Unintentional integer overflow")

---
 drivers/media/platform/vivid/vivid-cec.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 16 lines

drivers/media/platform/vivid/vivid-cec.c static void vivid_cec_pin_adap_events(struct · line 87

Quoted 12 lines
drivers/media/platform/vivid/vivid-cec.c +1 -1
index b55d278..30240ab 100644
@@ -83,7 +83,7 @@ static void vivid_cec_pin_adap_events(struct
 cec_adapter *adap, ktime_t ts,
 	if (adap == NULL)
 		return;
 	ts = ktime_sub_us(ts, (CEC_TIM_START_BIT_TOTAL +
-			       len * 10 * CEC_TIM_DATA_BIT_TOTAL));
+			       (const u64)len * 10 * CEC_TIM_DATA_BIT_TOTAL));

This makes no sense. Certainly the const part is pointless. And given that len is always <= 16 there definitely is no overflow.

Yeah, I understand your point and I know there is no chance of an overflow in this particular case.

Quoted 4 lines

I don't really want this cast in the code.

Sorry,

I'm working through all the Linux kernel Coverity reports, and I thought of sending a patch for this because IMHO it doesn't hurt to give the compiler complete information about the arithmetic in which an expression is intended to be evaluated.

Quoted 8 lines
Quoted 2 lines

I agree that the _const_ part is a bit odd. What do you think about the cast to u64 alone?

What happens if you do: ((u64)CEC_TIM_START_BIT_TOTAL +

I think that forces everything else in the expression to be evaluated as u64.

Quoted 7 lines

Well, in this case the operator precedence takes place and the expression len * 10 * CEC_TIM_DATA_BIT_TOTAL is computed first. So the issue remains the same.

I can switch the expressions as follows:

(u64)len * 10 * CEC_TIM_DATA_BIT_TOTAL + CEC_TIM_START_BIT_TOTAL

What about:

10ULL * len * ...

Quoted 4 lines

and avoid the cast in the middle.

What do you think?

My problem is that (u64)len suggests that there is some problem with len specifically, which isn't true.

Quoted 6 lines
Quoted 3 lines

It definitely needs a comment that this fixes a bogus Coverity report.

I actually added the following line to the message changelog:
Addresses-Coverity-ID: 1454996 ("Unintentional integer overflow")

That needs to be in the source, otherwise someone will remove the cast (or ULL) at some time in the future since it isn't clear why it is done. And nobody reads commit logs from X years back :-)

Quoted 5 lines

Certainly, I've run across multiple false positives as in this case, but I have also fixed many actual bugs thanks to the Coverity reports. So I think in general it is valuable to take a look into these reports, either if they spot actual bugs or promote code correctness.

Regards,

Hans

#15 lore raw

Re: [PATCH 8/8] platform: vivid-cec: fix potential integer overflow in vivid_cec_pin_adap_events

Quoting Hans Verkuil <[email protected]>:

[...]

Quoted 10 lines
Quoted 10 lines
Quoted 5 lines

What happens if you do: ((u64)CEC_TIM_START_BIT_TOTAL +

I think that forces everything else in the expression to be evaluated as u64.

Well, in this case the operator precedence takes place and the expression len * 10 * CEC_TIM_DATA_BIT_TOTAL is computed first. So the issue remains the same.

Quoted 8 lines
Quoted 3 lines

I can switch the expressions as follows:

(u64)len * 10 * CEC_TIM_DATA_BIT_TOTAL + CEC_TIM_START_BIT_TOTAL

What about:

10ULL * len * ...

Yeah, I like it.

Quoted 8 lines
Quoted 4 lines

and avoid the cast in the middle.

What do you think?

My problem is that (u64)len suggests that there is some problem with len specifically, which isn't true.

That's a good point. Actually, I think the same applies for the rest of the patch series. Maybe it is a good idea to send a v2 of the whole patchset with that update.

Quoted 11 lines
Quoted 6 lines
Quoted 3 lines

It definitely needs a comment that this fixes a bogus Coverity report.

I actually added the following line to the message changelog:
Addresses-Coverity-ID: 1454996 ("Unintentional integer overflow")

That needs to be in the source, otherwise someone will remove the cast (or ULL) at some time in the future since it isn't clear why it is done. And nobody reads commit logs from X years back :-)

You're right. I thought you were talking about the changelog.

And unless you think otherwise, I think there is no need for any additional code comment if the update you suggest is applied:

len * 10ULL * CEC_TIM_DATA_BIT_TOTAL

Thanks
--
Gustavo

#16 lore raw

Re: [PATCH 8/8] platform: vivid-cec: fix potential integer overflow in vivid_cec_pin_adap_events

Quoted 51 lines · Gustavo A. R. Silva

Quoting Hans Verkuil <[email protected]>:

[...]

Quoted 10 lines
Quoted 10 lines
Quoted 5 lines

What happens if you do: ((u64)CEC_TIM_START_BIT_TOTAL +

I think that forces everything else in the expression to be evaluated as u64.

Well, in this case the operator precedence takes place and the expression len * 10 * CEC_TIM_DATA_BIT_TOTAL is computed first. So the issue remains the same.

Quoted 8 lines
Quoted 3 lines

I can switch the expressions as follows:

(u64)len * 10 * CEC_TIM_DATA_BIT_TOTAL + CEC_TIM_START_BIT_TOTAL

What about:

10ULL * len * ...

Yeah, I like it.

Quoted 8 lines
Quoted 4 lines

and avoid the cast in the middle.

What do you think?

My problem is that (u64)len suggests that there is some problem with len specifically, which isn't true.

That's a good point. Actually, I think the same applies for the rest of the patch series. Maybe it is a good idea to send a v2 of the whole patchset with that update.

Quoted 11 lines
Quoted 6 lines
Quoted 3 lines

It definitely needs a comment that this fixes a bogus Coverity report.

I actually added the following line to the message changelog:
Addresses-Coverity-ID: 1454996 ("Unintentional integer overflow")

That needs to be in the source, otherwise someone will remove the cast (or ULL) at some time in the future since it isn't clear why it is done. And nobody reads commit logs from X years back :-)

Quoted 6 lines

You're right. I thought you were talking about the changelog.

And unless you think otherwise, I think there is no need for any additional code comment if the update you suggest is applied:

len * 10ULL * CEC_TIM_DATA_BIT_TOTAL

I still think I'd like to see a comment. It is still not obvious why you would want to use ULL here.

Please use "10ULL * len", it's actually a bit better to have it in that order (it's 10 bits per character, so '10 * len' is a more logical order).

Regards,

Hans

#17 lore raw

Re: [PATCH 8/8] platform: vivid-cec: fix potential integer overflow in vivid_cec_pin_adap_events

Quoting Hans Verkuil <[email protected]>:

Quoted 54 lines · Gustavo A. R. Silva
Quoted 53 lines

Quoting Hans Verkuil <[email protected]>:

[...]

Quoted 10 lines
Quoted 10 lines
Quoted 5 lines

What happens if you do: ((u64)CEC_TIM_START_BIT_TOTAL +

I think that forces everything else in the expression to be evaluated as u64.

Well, in this case the operator precedence takes place and the expression len * 10 * CEC_TIM_DATA_BIT_TOTAL is computed first. So the issue remains the same.

Quoted 8 lines
Quoted 3 lines

I can switch the expressions as follows:

(u64)len * 10 * CEC_TIM_DATA_BIT_TOTAL + CEC_TIM_START_BIT_TOTAL

What about:

10ULL * len * ...

Yeah, I like it.

Quoted 8 lines
Quoted 4 lines

and avoid the cast in the middle.

What do you think?

My problem is that (u64)len suggests that there is some problem with len specifically, which isn't true.

That's a good point. Actually, I think the same applies for the rest of the patch series. Maybe it is a good idea to send a v2 of the whole patchset with that update.

Quoted 11 lines
Quoted 6 lines
Quoted 3 lines

It definitely needs a comment that this fixes a bogus Coverity report.

I actually added the following line to the message changelog:
Addresses-Coverity-ID: 1454996 ("Unintentional integer overflow")

That needs to be in the source, otherwise someone will remove the cast (or ULL) at some time in the future since it isn't clear why it is done. And nobody reads commit logs from X years back :-)

You're right. I thought you were talking about the changelog.

Quoted 8 lines
Quoted 4 lines

And unless you think otherwise, I think there is no need for any additional code comment if the update you suggest is applied:

len * 10ULL * CEC_TIM_DATA_BIT_TOTAL

I still think I'd like to see a comment. It is still not obvious why you would want to use ULL here.

OK. That's fine.

Quoted 4 lines

Please use "10ULL * len", it's actually a bit better to have it in that order (it's 10 bits per character, so '10 * len' is a more logical order).

OK. I got it.

Thanks for all the feedback, Hans.
--
Gustavo