Skip to content

Commit 82cd7ad

Browse files
moonlight83340fabiobaltieri
authored andcommitted
drivers: dac: sam: Add max value check
Add max size check to dac sam and sam0 There is no size check in dac_sam_write_value and dac_sam0_write_value. Besides, the ret value should also be different. Fixes #65021 signed-off-by: Gaetan Perrot <[email protected]>
1 parent fc8b53b commit 82cd7ad

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

drivers/dac/dac_sam.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ static int dac_sam_write_value(const struct device *dev, uint8_t channel,
107107
return -EINVAL;
108108
}
109109

110+
if (value >= BIT(12)) {
111+
LOG_ERR("value %d out of range", value);
112+
return -EINVAL;
113+
}
114+
110115
k_sem_take(&dev_data->dac_channels[channel].sem, K_FOREVER);
111116

112117
/* Trigger conversion */

drivers/dac/dac_sam0.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include <zephyr/drivers/dac.h>
1212
#include <zephyr/drivers/pinctrl.h>
1313
#include <soc.h>
14+
#include <zephyr/logging/log.h>
15+
LOG_MODULE_REGISTER(dac_sam0, CONFIG_DAC_LOG_LEVEL);
1416

1517
/*
1618
* Maps between the DTS reference property names and register values. Note that
@@ -37,6 +39,11 @@ static int dac_sam0_write_value(const struct device *dev, uint8_t channel,
3739
const struct dac_sam0_cfg *const cfg = dev->config;
3840
Dac *regs = cfg->regs;
3941

42+
if (value >= BIT(12)) {
43+
LOG_ERR("value %d out of range", value);
44+
return -EINVAL;
45+
}
46+
4047
regs->DATA.reg = (uint16_t)value;
4148

4249
return 0;

0 commit comments

Comments
 (0)