]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/commitdiff
port_adc_get: Do not leak data
authorMichal Sojka <sojkam1@fel.cvut.cz>
Wed, 2 Sep 2015 15:59:46 +0000 (17:59 +0200)
committerMichal Sojka <sojkam1@fel.cvut.cz>
Thu, 3 Sep 2015 08:22:12 +0000 (10:22 +0200)
If the caller requested more data than what ADC actually returns
zero the fields not filled by ADC.

rpp/src/drv/adc.c

index 02c0861c24aad26f7f85646fcb063f373be50554..3951dae7b2c1ff692ef94d515bff233982121d58 100644 (file)
@@ -110,20 +110,24 @@ int8_t port_adc_get(const struct port_desc *port, void *values, size_t size)
 {
        uint32_t i;
        adcData_t data[ADC_MAX_CHANNELS];
-       int count = MIN(port->numchn, size/sizeof(uint16_t));
+       uint32_t count = MIN(port->numchn, size/sizeof(uint16_t));
+       uint32_t ret;
        uint16_t *adcval = values;
 
        assert(port->bpch == 16);
+       assert(port->numchn <= ADC_MAX_CHANNELS);
        assert(size % sizeof(uint16_t) == 0);
 
-       drv_adc_generic_read(
+       ret = drv_adc_generic_read(
                port->cfg.adc.reg,
                port->cfg.adc.group,
                data
                );
 
-       for (i = 0; i < count; i++)
+       for (i = 0; i < ret; i++)
                adcval[i] = data[i].value;
+       for (i = ret; i < count; i++)
+               adcval[i] = 0;
 
        return 0;
 }