From ebe72a2b2a306948ab6dca429b9f910b9af061fb Mon Sep 17 00:00:00 2001 From: Frank Mori Hess Date: Wed, 16 Jan 2008 19:45:05 +0000 Subject: [PATCH] Added more memory initialization for the sake of valgrind (especially memory that gets set by ioctls that valgrind can't see). Added a little error handling for a few memory allocations. --- lib/comedi.c | 7 +++--- lib/data.c | 10 ++++---- lib/dio.c | 3 ++- lib/filler.c | 64 ++++++++++++++++++++++++++++++++++++---------------- lib/get.c | 4 ++-- 5 files changed, 59 insertions(+), 29 deletions(-) diff --git a/lib/comedi.c b/lib/comedi.c index 5003f79..34ed3e9 100644 --- a/lib/comedi.c +++ b/lib/comedi.c @@ -39,7 +39,7 @@ INTERNAL int __comedi_init=0; INTERNAL void initialize(void) { char *s; - + __comedi_init=1; if( (s=getenv("COMEDILIB_LOGLEVEL")) ){ @@ -47,7 +47,7 @@ INTERNAL void initialize(void) COMEDILIB_DEBUG(3,"setting loglevel to %d\n",__comedi_loglevel); } } - + EXPORT_ALIAS_DEFAULT(_comedi_open,comedi_open,0.7.18); comedi_t* _comedi_open(const char *fn) { @@ -70,7 +70,8 @@ comedi_t* _comedi_open(const char *fn) it->n_subdevices=it->devinfo.n_subdevs; - get_subdevices(it); + if(get_subdevices(it) < 0) + goto cleanup; it->magic=COMEDILIB_MAGIC; diff --git a/lib/data.c b/lib/data.c index 3dbf875..dc4fbc8 100644 --- a/lib/data.c +++ b/lib/data.c @@ -45,7 +45,7 @@ int _comedi_data_write(comedi_t *it,unsigned int subdev,unsigned int chan,unsign return -1; s=it->subdevices+subdev; - + if(it->has_insnlist_ioctl){ comedi_insn insn; @@ -69,7 +69,7 @@ int _comedi_data_write(comedi_t *it,unsigned int subdev,unsigned int chan,unsign trigvar1: 0, }; sampl_t sdata[2]; - + sdata[0]=data & 0xffff; sdata[1]=(data >> 16) & 0xffff; chan=CR_PACK(chan,range,aref); @@ -94,6 +94,7 @@ static int comedi_internal_data_read_n(comedi_t *it, if(!valid_subd(it,subdev)) return -1; + if(n == 0) return 0; s = it->subdevices + subdev; @@ -107,7 +108,7 @@ static int comedi_internal_data_read_n(comedi_t *it, insn.data = data; insn.subdev = subdev; insn.chanspec = chanspec; - + memset(insn.data, 0, n * sizeof(data[0])); // for valgrind return comedi_do_insn(it,&insn); }else{ comedi_trig cmd={ @@ -196,7 +197,7 @@ int _comedi_data_read_delayed( comedi_t *it, unsigned int subdev, comedi_insnlist ilist; comedi_insn insn[3]; lsampl_t delay = nano_sec; - + if( !valid_chan( it, subdev, chan ) ) return -1; @@ -204,6 +205,7 @@ int _comedi_data_read_delayed( comedi_t *it, unsigned int subdev, memset( insn, 0, sizeof(insn) ); memset( &ilist, 0, sizeof(ilist) ); + memset(data, 0, sizeof(*data)); // for valgrind // setup, no conversions insn[0].insn = INSN_READ; diff --git a/lib/dio.c b/lib/dio.c index 5d5cb08..68806c9 100644 --- a/lib/dio.c +++ b/lib/dio.c @@ -105,6 +105,7 @@ int _comedi_dio_get_config(comedi_t *it,unsigned int subdev, unsigned int chan, insn.data = data; insn.subdev = subdev; insn.chanspec = CR_PACK(chan,0,0); + memset(data, 0, insn.n * sizeof(*data)); data[0] = INSN_CONFIG_DIO_QUERY; retval = comedi_do_insn(it,&insn); @@ -131,7 +132,7 @@ int _comedi_dio_read(comedi_t *it,unsigned int subdev,unsigned int chan, if(it->has_insnlist_ioctl){ comedi_insn insn; - lsampl_t data; + lsampl_t data = 0; memset(&insn,0,sizeof(insn)); insn.insn = INSN_READ; diff --git a/lib/filler.c b/lib/filler.c index 1f012ee..ec32ce4 100644 --- a/lib/filler.c +++ b/lib/filler.c @@ -21,6 +21,7 @@ USA. */ +#include #include #include #include @@ -51,16 +52,31 @@ int get_subdevices(comedi_t *it) comedi_chaninfo ci; subdevice *r; - s=malloc(sizeof(comedi_subdinfo)*it->n_subdevices); - debug_ptr(s); + s = calloc(it->n_subdevices, sizeof(comedi_subdinfo)); + if(s == NULL) + { + debug_ptr(s); + libc_error(); + return -1; + } ret = comedi_ioctl(it->fd, COMEDI_SUBDINFO, (unsigned long)s); - debug_int(ret); + if(ret < 0) + { + debug_int(ret); + free(s); + return -1; + } - r=it->subdevices=realloc(it->subdevices, - sizeof(subdevice)*it->n_subdevices); - debug_ptr(r); - memset(r,0,sizeof(subdevice)*it->n_subdevices); + assert(it->subdevices == NULL); + r = it->subdevices = calloc(it->n_subdevices, sizeof(subdevice)); + if(r == NULL) + { + debug_ptr(r); + libc_error(); + free(s); + return -1; + } it->has_insnlist_ioctl = do_test_for_insnlist(it); it->has_insn_ioctl = do_test_for_insn(it); @@ -76,15 +92,15 @@ int get_subdevices(comedi_t *it) r[i].range_type = s[i].range_type; if(r[i].subd_flags&SDF_FLAGS){ - r[i].flags_list=malloc(sizeof(*r[i].flags_list)*r[i].n_chan); + r[i].flags_list = calloc(r[i].n_chan, sizeof(*r[i].flags_list)); debug_ptr(r[i].flags_list); } if(r[i].subd_flags&SDF_MAXDATA){ - r[i].maxdata_list=malloc(sizeof(*r[i].maxdata_list)*r[i].n_chan); + r[i].maxdata_list = calloc(r[i].n_chan, sizeof(*r[i].maxdata_list)); debug_ptr(r[i].maxdata_list); } if(r[i].subd_flags&SDF_RANGETYPE){ - r[i].range_type_list=malloc(sizeof(*r[i].range_type_list)*r[i].n_chan); + r[i].range_type_list = calloc(r[i].n_chan, sizeof(*r[i].range_type_list)); debug_ptr(r[i].range_type_list); } ci.subdev = i; @@ -125,9 +141,21 @@ comedi_range *get_rangeinfo(int fd,unsigned int range_type) int ret; int i; - kr=malloc(sizeof(comedi_krange)*RANGE_LENGTH(range_type)); - r=malloc(sizeof(comedi_range)*RANGE_LENGTH(range_type)); + kr = calloc(RANGE_LENGTH(range_type), sizeof(comedi_krange)); + if(kr == NULL) + { + libc_error(); + return NULL; + } + r = malloc(sizeof(comedi_range)*RANGE_LENGTH(range_type)); + if(r == NULL) + { + libc_error(); + free(kr); + return NULL; + } + memset(&ri, 0, sizeof(ri)); ri.range_type = range_type; ri.range_ptr = kr; ret = comedi_ioctl(fd, COMEDI_RANGEINFO, (unsigned long)&ri); @@ -188,14 +216,14 @@ static int do_test_for_insnlist(comedi_t *dev) lsampl_t data[2]; int ret; - memset(&insn,0,sizeof(insn)); - il.n_insns = 1; il.insns = &insn; + memset(&insn,0,sizeof(insn)); insn.insn = INSN_GTOD; insn.n = 2; insn.data = data; + memset(insn.data, 0, insn.n * sizeof(insn.data[0])); ret = comedi_ioctl(dev->fd, COMEDI_INSNLIST, (unsigned long)&il); @@ -216,14 +244,14 @@ static int do_test_for_insn(comedi_t *dev) lsampl_t data[2]; int ret; - memset(&insn,0,sizeof(insn)); - il.n_insns = 1; il.insns = &insn; + memset(&insn,0,sizeof(insn)); insn.insn = INSN_GTOD; insn.n = 2; insn.data = data; + memset(insn.data, 0, insn.n * sizeof(insn.data[0])); ret = comedi_ioctl(dev->fd, COMEDI_INSN, (unsigned long)&insn); @@ -255,9 +283,7 @@ static int do_test_for_insn_bits(comedi_t *dev,unsigned int subdevice) insn.n = 2; insn.data = data; insn.subdev = subdevice; - - data[0]=0; - data[1]=0; + memset(data, 0, insn.n * sizeof(data[0])); ret = comedi_do_insnlist(dev,&il); diff --git a/lib/get.c b/lib/get.c index 00a0375..6d683d9 100644 --- a/lib/get.c +++ b/lib/get.c @@ -87,10 +87,10 @@ int _comedi_get_subdevice_flags(comedi_t *it,unsigned int subd) int ret; if(!valid_dev(it)) return -1; - s = malloc(sizeof(comedi_subdinfo) * it->n_subdevices); + s = calloc(it->n_subdevices, sizeof(comedi_subdinfo)); if(s == NULL) { - __comedi_errno = errno; + libc_error(); return -1; } ret = comedi_ioctl(it->fd, COMEDI_SUBDINFO, (unsigned long)s); -- 2.26.2