From 4df5ffd451692818cfa12d01219542bccd5c366a Mon Sep 17 00:00:00 2001 From: Ian Abbott Date: Wed, 5 Nov 2008 12:52:25 +0000 Subject: [PATCH] Added some device and subdevice validity checks. --- lib/buffer.c | 7 +++++++ lib/calib.c | 2 ++ lib/comedi.c | 16 ++++++++++++---- lib/get.c | 8 +++++--- lib/range.c | 1 + lib/sv.c | 2 ++ lib/timer.c | 2 +- 7 files changed, 30 insertions(+), 8 deletions(-) diff --git a/lib/buffer.c b/lib/buffer.c index 8210067..dd286aa 100644 --- a/lib/buffer.c +++ b/lib/buffer.c @@ -30,6 +30,7 @@ int _comedi_set_buffer_size(comedi_t *it, unsigned int subdev, unsigned int size int ret; comedi_bufconfig bc; + if(!valid_subd(it,subdev)) return -1; memset(&bc, 0, sizeof(bc)); bc.subdevice = subdev; bc.size = size; @@ -45,6 +46,7 @@ int _comedi_set_max_buffer_size(comedi_t *it, unsigned int subdev, unsigned int int ret; comedi_bufconfig bc; + if(!valid_subd(it,subdev)) return -1; memset(&bc, 0, sizeof(bc)); bc.subdevice = subdev; bc.maximum_size = max_size; @@ -72,6 +74,7 @@ int _comedi_get_buffer_contents(comedi_t *it, unsigned int subdev) int ret; comedi_bufinfo bi; + if(!valid_subd(it,subdev)) return -1; memset(&bi, 0, sizeof(bi)); bi.subdevice = subdev; ret = comedi_ioctl(it->fd, COMEDI_BUFINFO, &bi); @@ -89,6 +92,7 @@ int _comedi_mark_buffer_read(comedi_t *it, unsigned int subdev, unsigned int byt int ret; comedi_bufinfo bi; + if(!valid_subd(it,subdev)) return -1; memset(&bi, 0, sizeof(bi)); bi.subdevice = subdev; bi.bytes_read = bytes; @@ -107,6 +111,7 @@ int _comedi_mark_buffer_written(comedi_t *it, unsigned int subdev, unsigned int int ret; comedi_bufinfo bi; + if(!valid_subd(it,subdev)) return -1; memset(&bi, 0, sizeof(bi)); bi.subdevice = subdev; bi.bytes_written = bytes; @@ -125,6 +130,7 @@ int _comedi_get_buffer_offset(comedi_t *it, unsigned int subdev) int ret; comedi_bufinfo bi; + if(!valid_subd(it,subdev)) return -1; memset(&bi, 0, sizeof(bi)); bi.subdevice = subdev; ret = comedi_ioctl(it->fd, COMEDI_BUFINFO, &bi); @@ -138,6 +144,7 @@ int _comedi_get_front_count(comedi_t *it, unsigned int subdev) int ret; comedi_bufinfo bi; + if(!valid_subd(it,subdev)) return -1; memset(&bi, 0, sizeof(bi)); bi.subdevice = subdev; ret = comedi_ioctl(it->fd, COMEDI_BUFINFO, &bi); diff --git a/lib/calib.c b/lib/calib.c index 0038c82..9415570 100644 --- a/lib/calib.c +++ b/lib/calib.c @@ -156,6 +156,7 @@ int _comedi_apply_parsed_calibration( comedi_t *dev, unsigned int subdev, unsign { int retval; + if(!valid_dev(dev)) return -1; retval = check_cal_file( dev, calibration ); if( retval < 0 ) return retval; @@ -185,6 +186,7 @@ char* _comedi_get_default_calibration_path( comedi_t *dev ) char *board_name; const char *driver_name; + if(!valid_dev(dev)) return NULL; if( fstat( comedi_fileno( dev ), &file_stats ) < 0 ) { COMEDILIB_DEBUG( 3, "failed to get file stats of comedi device file\n" ); diff --git a/lib/comedi.c b/lib/comedi.c index 3a152c1..5877e6a 100644 --- a/lib/comedi.c +++ b/lib/comedi.c @@ -89,6 +89,8 @@ int _comedi_close(comedi_t *it) subdevice *s; int i,j; + if(!valid_dev(it)) + return -1; it->magic=0; for(i=0;in_subdevices;i++){ @@ -124,28 +126,28 @@ int _comedi_close(comedi_t *it) EXPORT_ALIAS_DEFAULT(_comedi_cancel,comedi_cancel,0.7.18); int _comedi_cancel(comedi_t *it,unsigned int subdevice) { + if(!valid_dev(it)) return -1; return comedi_ioctl(it->fd, COMEDI_CANCEL, (void*)(unsigned long)subdevice); } EXPORT_ALIAS_DEFAULT(_comedi_poll,comedi_poll,0.7.18); int _comedi_poll(comedi_t *it,unsigned int subdevice) { + if(!valid_dev(it)) return -1; return comedi_ioctl(it->fd, COMEDI_POLL, (void*)(unsigned long)subdevice); } EXPORT_ALIAS_DEFAULT(_comedi_fileno,comedi_fileno,0.7.18); int _comedi_fileno(comedi_t *it) { - if(!it) - return -1; - + if(!valid_dev(it)) return -1; return it->fd; } EXPORT_ALIAS_DEFAULT(_comedi_trigger,comedi_trigger,0.7.18); int _comedi_trigger(comedi_t *it,comedi_trig *t) { - if(!it || !t) + if(!valid_dev(it) || !t) return -1; return comedi_ioctl(it->fd, COMEDI_TRIG, t); @@ -155,6 +157,7 @@ EXPORT_ALIAS_DEFAULT(_comedi_command,comedi_command,0.7.18); int _comedi_command(comedi_t *it,comedi_cmd *t) { int ret; + if(!valid_dev(it)) return -1; ret = comedi_ioctl(it->fd, COMEDI_CMD, t); __comedi_errno = errno; switch(__comedi_errno){ @@ -169,6 +172,7 @@ EXPORT_ALIAS_DEFAULT(_comedi_command_test,comedi_command_test,0.7.18); int _comedi_command_test(comedi_t *it,comedi_cmd *t) { int ret; + if(!valid_dev(it)) return -1; ret = comedi_ioctl(it->fd, COMEDI_CMDTEST, t); __comedi_errno = errno; switch(__comedi_errno){ @@ -183,6 +187,7 @@ EXPORT_ALIAS_DEFAULT(_comedi_do_insnlist,comedi_do_insnlist,0.7.18); int _comedi_do_insnlist(comedi_t *it,comedi_insnlist *il) { int ret; + if(!valid_dev(it)) return -1; ret = comedi_ioctl(it->fd, COMEDI_INSNLIST, il); __comedi_errno = errno; return ret; @@ -191,6 +196,7 @@ int _comedi_do_insnlist(comedi_t *it,comedi_insnlist *il) EXPORT_ALIAS_DEFAULT(_comedi_do_insn,comedi_do_insn,0.7.18); int _comedi_do_insn(comedi_t *it,comedi_insn *insn) { + if(!valid_dev(it)) return -1; if(it->has_insn_ioctl){ return comedi_ioctl(it->fd, COMEDI_INSN, insn); }else{ @@ -210,12 +216,14 @@ int _comedi_do_insn(comedi_t *it,comedi_insn *insn) EXPORT_ALIAS_DEFAULT(_comedi_lock,comedi_lock,0.7.18); int _comedi_lock(comedi_t *it,unsigned int subdevice) { + if(!valid_dev(it)) return -1; return comedi_ioctl(it->fd, COMEDI_LOCK, (void*)(unsigned long)subdevice); } EXPORT_ALIAS_DEFAULT(_comedi_unlock,comedi_unlock,0.7.18); int _comedi_unlock(comedi_t *it,unsigned int subdevice) { + if(!valid_dev(it)) return -1; return comedi_ioctl(it->fd, COMEDI_UNLOCK, (void*)(unsigned long)subdevice); } diff --git a/lib/get.c b/lib/get.c index 02f0e7f..0856d99 100644 --- a/lib/get.c +++ b/lib/get.c @@ -74,7 +74,7 @@ const char* _comedi_get_board_name(comedi_t *it) EXPORT_ALIAS_VER(_comedi_get_subdevice_flags_old, comedi_get_subdevice_flags,0.7.18); int _comedi_get_subdevice_flags_old(comedi_t *it,unsigned int subd) { - if(!valid_dev(it)) + if(!valid_subd(it,subd)) return 0; return it->subdevices[subd].subd_flags; } @@ -85,7 +85,7 @@ int _comedi_get_subdevice_flags(comedi_t *it,unsigned int subd) comedi_subdinfo *s; int flags; int ret; - if(!valid_dev(it)) + if(!valid_subd(it,subd)) return -1; s = calloc(it->n_subdevices, sizeof(comedi_subdinfo)); if(s == NULL) @@ -107,7 +107,7 @@ int _comedi_get_subdevice_flags(comedi_t *it,unsigned int subd) EXPORT_ALIAS_DEFAULT(_comedi_get_subdevice_type,comedi_get_subdevice_type,0.7.18); int _comedi_get_subdevice_type(comedi_t *it,unsigned int subd) { - if(!valid_dev(it)) + if(!valid_subd(it,subd)) return -1; return it->subdevices[subd].type; @@ -171,6 +171,8 @@ lsampl_t _comedi_get_maxdata(comedi_t *it,unsigned int subdevice,unsigned int ch EXPORT_ALIAS_DEFAULT(_comedi_maxdata_is_chan_specific,comedi_maxdata_is_chan_specific,0.7.18); int _comedi_maxdata_is_chan_specific(comedi_t *it,unsigned int subdevice) { + if(!valid_subd(it,subdevice)) + return -1; if(it->subdevices[subdevice].maxdata_list) return 1; return 0; diff --git a/lib/range.c b/lib/range.c index cc6115d..361ac68 100644 --- a/lib/range.c +++ b/lib/range.c @@ -136,6 +136,7 @@ int _comedi_get_n_ranges(comedi_t *it,unsigned int subd,unsigned int chan) EXPORT_ALIAS_DEFAULT(_comedi_range_is_chan_specific,comedi_range_is_chan_specific,0.7.18); int _comedi_range_is_chan_specific(comedi_t *it,unsigned int subd) { + if(!valid_subd(it,subd)) return -1; return (it->subdevices[subd].subd_flags&SDF_RANGETYPE)?1:0; } diff --git a/lib/sv.c b/lib/sv.c index 821a256..bb96c46 100644 --- a/lib/sv.c +++ b/lib/sv.c @@ -72,6 +72,8 @@ int _comedi_sv_update(comedi_sv_t *it) EXPORT_ALIAS_DEFAULT(_comedi_sv_measure,comedi_sv_measure,0.7.18); int _comedi_sv_measure(comedi_sv_t *it,double *data) { + if(!it)return -1; + if(!valid_subd(it->dev,it->subdevice))return -1; if(it->dev->subdevices[it->subdevice].subd_flags & SDF_LSAMPL){ return sv_measure_l(it,data); }else{ diff --git a/lib/timer.c b/lib/timer.c index a12ee8d..220974b 100644 --- a/lib/timer.c +++ b/lib/timer.c @@ -147,7 +147,7 @@ int _comedi_get_timer(comedi_t *it,unsigned int subdev,double freq, { int timer_type; - if(!it || !trigvar || !actual_freq) + if(!valid_subd(it,subdev) || !trigvar || !actual_freq) return -1; timer_type=it->subdevices[subdev].timer_type; -- 2.26.2