From 2811aea03894e1306f9426c78a4b8b8a6f906c56 Mon Sep 17 00:00:00 2001 From: Ian Abbott Date: Tue, 7 May 2013 12:32:26 +0100 Subject: [PATCH] lib: check some malloc/calloc/realloc failures Some calls to malloc(), realloc() or calloc() do not check the result. Do so. Also call internal function libc_error() if they fail to set the result for comedi_errno(). --- lib/calib_yacc.y | 18 ++++++++++++++++++ lib/cmd.c | 18 ++++++++++++++++-- lib/sv.c | 8 ++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/lib/calib_yacc.y b/lib/calib_yacc.y index 9624143..59643aa 100644 --- a/lib/calib_yacc.y +++ b/lib/calib_yacc.y @@ -114,6 +114,7 @@ static int add_calibration_setting( comedi_calibration_t *file_contents ) ( file_contents->num_settings + 1 ) * sizeof( comedi_calibration_setting_t ) ); if( temp == NULL ) { + libc_error(); fprintf(stderr, "%s: realloc failed to allocate memory.\n", __FUNCTION__); return -1; } @@ -148,6 +149,7 @@ static int add_channel( calib_yyparse_private_t *priv, int channel ) temp = realloc( setting->channels, ( setting->num_channels + 1 ) * sizeof(unsigned) ); if( temp == NULL ) { + libc_error(); fprintf(stderr, "%s: realloc failed to allocate memory.\n", __FUNCTION__); return -1; } @@ -167,6 +169,7 @@ static int add_range( calib_yyparse_private_t *priv, int range ) temp = realloc( setting->ranges, ( setting->num_ranges + 1 ) * sizeof(unsigned) ); if( temp == NULL ) { + libc_error(); fprintf(stderr, "%s: realloc failed to allocate memory.\n", __FUNCTION__); return -1; } @@ -202,6 +205,7 @@ static int add_caldac( calib_yyparse_private_t *priv, sizeof( comedi_caldac_t ) ); if( temp == NULL ) { + libc_error(); fprintf(stderr, "%s: realloc failed to allocate memory.\n", __FUNCTION__); return -1; } @@ -229,11 +233,21 @@ static int add_polynomial(calib_yyparse_private_t *priv, enum polynomial_directi { if(setting->soft_calibration.to_phys) return -1; setting->soft_calibration.to_phys = malloc(sizeof(comedi_polynomial_t)); + if(!setting->soft_calibration.to_phys){ + libc_error(); + fprintf(stderr, "%s: malloc failed to allocate memory.\n", __FUNCTION__); + return -1; + } *setting->soft_calibration.to_phys = priv->polynomial; }else { if(setting->soft_calibration.from_phys) return -1; setting->soft_calibration.from_phys = malloc(sizeof(comedi_polynomial_t)); + if(!setting->soft_calibration.from_phys){ + libc_error(); + fprintf(stderr, "%s: malloc failed to allocate memory.\n", __FUNCTION__); + return -1; + } *setting->soft_calibration.from_phys = priv->polynomial; } return 0; @@ -285,6 +299,10 @@ static comedi_polynomial_t* alloc_inverse_linear_polynomial(const comedi_polynom comedi_polynomial_t *inverse; if(polynomial->order != 1) return NULL; inverse = malloc(sizeof(comedi_polynomial_t)); + if(!inverse){ + libc_error(); + return NULL; + } memset(inverse, 0, sizeof(comedi_polynomial_t)); inverse->order = 1; inverse->expansion_origin = polynomial->coefficients[0]; diff --git a/lib/cmd.c b/lib/cmd.c index 27fd4cc..0daa604 100644 --- a/lib/cmd.c +++ b/lib/cmd.c @@ -55,6 +55,10 @@ int _comedi_get_cmd_src_mask(comedi_t *it,unsigned int subd,comedi_cmd *cmd) comedi_cmd *mask; mask = malloc(sizeof(comedi_cmd)); + if(!mask){ + libc_error(); + return -1; + } memset(mask,0,sizeof(*cmd)); @@ -174,8 +178,13 @@ int _comedi_get_cmd_generic_timed_obsolete(comedi_t *it,unsigned int subd,comedi return -1; } - if(!s->cmd_timed) + if(!s->cmd_timed){ s->cmd_timed = malloc(sizeof(comedi_cmd)); + if(!s->cmd_timed){ + libc_error(); + return -1; + } + } ret = __generic_timed(it, subd, s->cmd_timed, 1, ns); if(ret<0){ @@ -202,8 +211,13 @@ int _comedi_get_cmd_generic_timed(comedi_t *it, unsigned subd, comedi_cmd *cmd, return -1; } - if(!s->cmd_timed) + if(!s->cmd_timed){ s->cmd_timed = malloc(sizeof(comedi_cmd)); + if(!s->cmd_timed){ + libc_error(); + return -1; + } + } ret = __generic_timed(it, subd, s->cmd_timed, chanlist_len, scan_period_ns); if(ret<0){ diff --git a/lib/sv.c b/lib/sv.c index bb96c46..cd98d24 100644 --- a/lib/sv.c +++ b/lib/sv.c @@ -93,6 +93,10 @@ int sv_measure_l(comedi_sv_t *it,double *data) int n; val=malloc(sizeof(*val)*it->n); + if(!val){ + libc_error(); + return -1; + } chan=CR_PACK(it->chan,it->range,it->aref); @@ -144,6 +148,10 @@ int sv_measure_s(comedi_sv_t *it,double *data) int n; val=malloc(sizeof(*val)*it->n); + if(!val){ + libc_error(); + return -1; + } chan=CR_PACK(it->chan,it->range,it->aref);