Skip to contentSkip to conversation

iio: adc: ti-ads1018: fix type overflow for data rate

PATCH Superseded Chunyang Chen 4 participants 10 messages Mar 5 – Mar 9

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

PATCH 1/1 iio: adc: ti-ads1018: fix type overflow for data rate

The variable 'drate' is currently defined as u8. However, the data rate values in ads1018 can reach up to 3300 Hz (e.g., in ads1018_data_rate[]), which exceeds the maximum value of 255 that a u8 can hold.

Change the type of 'drate' to u16 to prevent integer overflow and ensure the data rate is handled correctly.

---
 drivers/iio/adc/ti-ads1018.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/adc/ti-ads1018.c +1 -1
index 6246b3cab..049b8c00f 100644
@@ -249,7 +249,7 @@ static int ads1018_single_shot(struct ads1018 *ads1018,
 			       struct iio_chan_spec const *chan, u16 *cnv)
 {
 	u8 max_drate_mode = ads1018->chip_info->num_data_rate_mode_to_hz - 1;
-	u8 drate = ads1018->chip_info->data_rate_mode_to_hz[max_drate_mode];
+	u16 drate = ads1018->chip_info->data_rate_mode_to_hz[max_drate_mode];
 	u8 pga_mode = ads1018->chan_data[chan->scan_index].pga_mode;
 	struct spi_transfer xfer[2] = {
 		{
Signature · 1 line
2.34.1
maintainer
#1 lore raw

Hi Chunyang,

Quoted 6 lines · Chunyang Chen

The variable 'drate' is currently defined as u8. However, the data rate values in ads1018 can reach up to 3300 Hz (e.g., in ads1018_data_rate[]), which exceeds the maximum value of 255 that a u8 can hold.

Change the type of 'drate' to u16 to prevent integer overflow and ensure the data rate is handled correctly.

Good catch -- thanks!

Interestingly, my SPI controller has so much latency that I didn't catch this while developing :/

Quoted 11 lines
---
 drivers/iio/adc/ti-ads1018.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/adc/ti-ads1018.c +0 -0
index 6246b3cab..049b8c00f 100644

drivers/iio/adc/ti-ads1018.c static int ads1018_single_shot(struct ads1018 *ads1018, · line 252

Quoted 5 lines
drivers/iio/adc/ti-ads1018.c +1 -1
@@ -249,7 +249,7 @@ static int ads1018_single_shot(struct ads1018 *ads1018,
 			       struct iio_chan_spec const *chan, u16 *cnv)
 {
 	u8 max_drate_mode = ads1018->chip_info->num_data_rate_mode_to_hz - 1;
-	u8 drate = ads1018->chip_info->data_rate_mode_to_hz[max_drate_mode];
+	u16 drate = ads1018->chip_info->data_rate_mode_to_hz[max_drate_mode];

data_rate_mode_to_hz[] is u32 so I prefer for this to be u32. This also kinda matches ads1018_calc_delay() which takes unsigned int.

I'm a bit disturbed by the type inconsistency here...

Quoted 3 lines
drivers/iio/adc/ti-ads1018.c +0 -0
 	u8 pga_mode = ads1018->chan_data[chan->scan_index].pga_mode;
 	struct spi_transfer xfer[2] = {
 		{
Signature · 2 lines
Thanks,
 ~ Kurt
#2 lore raw
In reply to Chunyang Chen
Quoted 6 lines · Chunyang Chen

The variable 'drate' is currently defined as u8. However, the data rate values in ads1018 can reach up to 3300 Hz (e.g., in ads1018_data_rate[]), which exceeds the maximum value of 255 that a u8 can hold.

Change the type of 'drate' to u16 to prevent integer overflow and ensure the data rate is handled correctly.

Needs Fixes tag.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw

Hi Kurt, Andy,

Thanks for your review and suggestions!
I have sent out the v2 patch, which addresses the following:

Quoted 2 lines · Kurt Borja

data_rate_mode_to_hz[] is u32 so I prefer for this to be u32. This also kinda matches ads1018_calc_delay() which takes unsigned int.

Changed 'drate' type to u32 to match the array definition and maintain consistency, as suggested by Kurt.

Quoted 1 line · Andy Shevchenko

Needs Fixes tag.

Added the Fixes tag as requested by Andy.

Best regards,
Chunyang Chen

#4 lore raw

PATCH v2 1/1 iio: adc: ti-ads1018: fix type overflow for data rate

In reply to Chunyang Chen

The variable 'drate' is currently defined as u8. However, the data rate values in ads1018 can reach up to 3300 Hz, which exceeds the maximum value of 255 that a u8 can hold.

Change the type of 'drate' to u32 to match the data_rate_mode_to_hz array definition and ensure the data rate is handled correctly.

Fixes bf0bba486b5b ("iio: adc: ti-ads1018: add ti-ads1018 driver")

---
v2:
- Change drate type to u32 as suggested by Kurt.
- Add Fixes tag as requested by Andy.

---
 drivers/iio/adc/ti-ads1018.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/adc/ti-ads1018.c +1 -1
index 6246b3cab..0780abd0d 100644
@@ -249,7 +249,7 @@ static int ads1018_single_shot(struct ads1018 *ads1018,
 			       struct iio_chan_spec const *chan, u16 *cnv)
 {
 	u8 max_drate_mode = ads1018->chip_info->num_data_rate_mode_to_hz - 1;
-	u8 drate = ads1018->chip_info->data_rate_mode_to_hz[max_drate_mode];
+	u32 drate = ads1018->chip_info->data_rate_mode_to_hz[max_drate_mode];
 	u8 pga_mode = ads1018->chan_data[chan->scan_index].pga_mode;
 	struct spi_transfer xfer[2] = {
 		{
Signature · 1 line
2.34.1
maintainer Reviewed-by on v2 1/1
#5 lore raw
Quoted 9 lines · Chunyang Chen

The variable 'drate' is currently defined as u8. However, the data rate values in ads1018 can reach up to 3300 Hz, which exceeds the maximum value of 255 that a u8 can hold.

Change the type of 'drate' to u32 to match the data_rate_mode_to_hz array definition and ensure the data rate is handled correctly.

Fixes bf0bba486b5b ("iio: adc: ti-ads1018: add ti-ads1018 driver")
Quoted 22 lines

---
v2:
- Change drate type to u32 as suggested by Kurt.
- Add Fixes tag as requested by Andy.

---
 drivers/iio/adc/ti-ads1018.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/adc/ti-ads1018.c +1 -1
index 6246b3cab..0780abd0d 100644
@@ -249,7 +249,7 @@ static int ads1018_single_shot(struct ads1018 *ads1018,
 			       struct iio_chan_spec const *chan, u16 *cnv)
 {
 	u8 max_drate_mode = ads1018->chip_info->num_data_rate_mode_to_hz - 1;
-	u8 drate = ads1018->chip_info->data_rate_mode_to_hz[max_drate_mode];
+	u32 drate = ads1018->chip_info->data_rate_mode_to_hz[max_drate_mode];
 	u8 pga_mode = ads1018->chan_data[chan->scan_index].pga_mode;
 	struct spi_transfer xfer[2] = {
 		{
Signature · 2 lines
Thanks,
 ~ Kurt
Reviewed-by on v2 1/1
#6 lore raw
In reply to Chunyang Chen
Quoted 9 lines · Chunyang Chen

The variable 'drate' is currently defined as u8. However, the data rate values in ads1018 can reach up to 3300 Hz, which exceeds the maximum value of 255 that a u8 can hold.

Change the type of 'drate' to u32 to match the data_rate_mode_to_hz array definition and ensure the data rate is handled correctly.

Fixes bf0bba486b5b ("iio: adc: ti-ads1018: add ti-ads1018 driver")

New version — new email thread.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer Applied
#7 lore raw
Quoted 5 lines · On Fri, 6 Mar 2026 14:08:15 +0200 Andy Shevchenko…
Quoted 4 lines · Chunyang Chen

The variable 'drate' is currently defined as u8. However, the data rate values in ads1018 can reach up to 3300 Hz, which exceeds the maximum value of 255 that a u8 can hold.

Quoted 9 lines
Quoted 5 lines

Change the type of 'drate' to u32 to match the data_rate_mode_to_hz array definition and ensure the data rate is handled correctly.

Fixes bf0bba486b5b ("iio: adc: ti-ads1018: add ti-ads1018 driver")

New version — new email thread.

Applied to the fixes-togreg branch of iio.git

Thanks,

Jonathan

maintainer
#8 lore raw
In reply to Chunyang Chen
Quoted 8 lines · On Thu, 5 Mar 2026 20:43:02 +0800 Chunyang Chen <[email protected]>…

The variable 'drate' is currently defined as u8. However, the data rate values in ads1018 can reach up to 3300 Hz, which exceeds the maximum value of 255 that a u8 can hold.

Change the type of 'drate' to u32 to match the data_rate_mode_to_hz array definition and ensure the data rate is handled correctly.

Fixes bf0bba486b5b ("iio: adc: ti-ads1018: add ti-ads1018 driver")

The patch name in this fixes tag is wrong. I fixed up.

J

Quoted 23 lines

---
v2:
- Change drate type to u32 as suggested by Kurt.
- Add Fixes tag as requested by Andy.

---
 drivers/iio/adc/ti-ads1018.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/adc/ti-ads1018.c +1 -1
index 6246b3cab..0780abd0d 100644
@@ -249,7 +249,7 @@ static int ads1018_single_shot(struct ads1018 *ads1018,
 			       struct iio_chan_spec const *chan, u16 *cnv)
 {
 	u8 max_drate_mode = ads1018->chip_info->num_data_rate_mode_to_hz - 1;
-	u8 drate = ads1018->chip_info->data_rate_mode_to_hz[max_drate_mode];
+	u32 drate = ads1018->chip_info->data_rate_mode_to_hz[max_drate_mode];
 	u8 pga_mode = ads1018->chan_data[chan->scan_index].pga_mode;
 	struct spi_transfer xfer[2] = {
 		{
#9 lore raw

Hi Jonathan, Andy, Kurt,

Thanks for the review and for applying this patch to iio/fixes-togreg! Also, thanks Jonathan for fixing up the patch name in the Fixes tag.

Best regards,
Chunyang Chen