All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Hartmut Knaack <knaack.h@gmx.de>, linux-iio@vger.kernel.org
Cc: Lars-Peter Clausen <lars@metafoo.de>,
	Peter Meerwald <pmeerw@pmeerw.net>,
	Roberta Dobrescu <roberta.dobrescu@gmail.com>,
	Daniel Baluta <daniel.baluta@intel.com>,
	Irina Tirdea <irina.tirdea@intel.com>
Subject: Re: [PATCH 21/32] tools:iio:iio_utils: add error handling
Date: Sun, 31 May 2015 19:44:11 +0100	[thread overview]
Message-ID: <556B567B.5030600@kernel.org> (raw)
In-Reply-To: <e655a43af0f41e20a66b798e1d1859afa89dc9ef.1433072539.git.knaack.h@gmx.de>

On 31/05/15 13:40, Hartmut Knaack wrote:
> Add error handling to calls which can indicate a major problem by
> returning an error code.
> This also sets ret to -ENOENT in iioutils_get_type() and
> iioutils_get_param_float() to indicate if no matching directory entry was
> found.
> 
> Signed-off-by: Hartmut Knaack <knaack.h@gmx.de>
Applied.
> ---
>  tools/iio/iio_utils.c | 265 ++++++++++++++++++++++++++++++++++++++++++--------
>  1 file changed, 223 insertions(+), 42 deletions(-)
> 
> diff --git a/tools/iio/iio_utils.c b/tools/iio/iio_utils.c
> index f0896f46..e1828d0 100644
> --- a/tools/iio/iio_utils.c
> +++ b/tools/iio/iio_utils.c
> @@ -50,6 +50,10 @@ int iioutils_break_up_name(const char *full_name,
>  		return -ENOMEM;
>  
>  	working = strtok(current, "_\0");
> +	if (!working) {
> +		free(current);
> +		return -EINVAL;
> +	}
>  
>  	w = working;
>  	r = working;
> @@ -117,6 +121,7 @@ int iioutils_get_type(unsigned *is_signed,
>  		ret = -errno;
>  		goto error_free_builtname_generic;
>  	}
> +	ret = -ENOENT;
>  	while (ent = readdir(dp), ent != NULL)
>  		/*
>  		 * Do we allow devices to override a generic name with
> @@ -162,7 +167,12 @@ int iioutils_get_type(unsigned *is_signed,
>  				*is_signed = 1;
>  			else
>  				*is_signed = 0;
> -			fclose(sysfsfp);
> +			if (fclose(sysfsfp)) {
> +				ret = -errno;
> +				printf("Failed to close %s\n", filename);
> +				goto error_free_filename;
> +			}
> +
>  			free(filename);
>  
>  			filename = 0;
> @@ -170,12 +180,16 @@ int iioutils_get_type(unsigned *is_signed,
>  		}
>  error_close_sysfsfp:
>  	if (sysfsfp)
> -		fclose(sysfsfp);
> +		if (fclose(sysfsfp))
> +			perror("iioutils_get_type(): Failed to close file");
> +
>  error_free_filename:
>  	if (filename)
>  		free(filename);
>  error_closedir:
> -	closedir(dp);
> +	if (closedir(dp) == -1)
> +		perror("iioutils_get_type(): Failed to close directory");
> +
>  error_free_builtname_generic:
>  	free(builtname_generic);
>  error_free_builtname:
> @@ -215,6 +229,7 @@ int iioutils_get_param_float(float *output,
>  		ret = -errno;
>  		goto error_free_builtname_generic;
>  	}
> +	ret = -ENOENT;
>  	while (ent = readdir(dp), ent != NULL)
>  		if ((strcmp(builtname, ent->d_name) == 0) ||
>  		    (strcmp(builtname_generic, ent->d_name) == 0)) {
> @@ -229,14 +244,19 @@ int iioutils_get_param_float(float *output,
>  				ret = -errno;
>  				goto error_free_filename;
>  			}
> -			fscanf(sysfsfp, "%f", output);
> +			errno = 0;
> +			if (fscanf(sysfsfp, "%f", output) != 1)
> +				ret = errno ? -errno : -ENODATA;
> +
>  			break;
>  		}
>  error_free_filename:
>  	if (filename)
>  		free(filename);
>  error_closedir:
> -	closedir(dp);
> +	if (closedir(dp) == -1)
> +		perror("iioutils_get_param_float(): Failed to close directory");
> +
>  error_free_builtname_generic:
>  	free(builtname_generic);
>  error_free_builtname:
> @@ -310,10 +330,24 @@ int build_channel_array(const char *device_dir,
>  				free(filename);
>  				goto error_close_dir;
>  			}
> -			fscanf(sysfsfp, "%i", &ret);
> +			errno = 0;
> +			if (fscanf(sysfsfp, "%i", &ret) != 1) {
> +				ret = errno ? -errno : -ENODATA;
> +				if (fclose(sysfsfp))
> +					perror("build_channel_array(): Failed to close file");
> +
> +				free(filename);
> +				goto error_close_dir;
> +			}
> +
>  			if (ret == 1)
>  				(*counter)++;
> -			fclose(sysfsfp);
> +			if (fclose(sysfsfp)) {
> +				ret = -errno;
> +				free(filename);
> +				goto error_close_dir;
> +			}
> +
>  			free(filename);
>  		}
>  	*ci_array = malloc(sizeof(**ci_array) * (*counter));
> @@ -344,8 +378,20 @@ int build_channel_array(const char *device_dir,
>  				count--;
>  				goto error_cleanup_array;
>  			}
> -			fscanf(sysfsfp, "%i", &current_enabled);
> -			fclose(sysfsfp);
> +			errno = 0;
> +			if (fscanf(sysfsfp, "%i", &current_enabled) != 1) {
> +				ret = errno ? -errno : -ENODATA;
> +				free(filename);
> +				count--;
> +				goto error_cleanup_array;
> +			}
> +
> +			if (fclose(sysfsfp)) {
> +				ret = -errno;
> +				free(filename);
> +				count--;
> +				goto error_cleanup_array;
> +			}
>  
>  			if (!current_enabled) {
>  				free(filename);
> @@ -383,8 +429,29 @@ int build_channel_array(const char *device_dir,
>  				goto error_cleanup_array;
>  			}
>  			sysfsfp = fopen(filename, "r");
> -			fscanf(sysfsfp, "%u", &current->index);
> -			fclose(sysfsfp);
> +			if (sysfsfp == NULL) {
> +				ret = -errno;
> +				printf("failed to open %s\n", filename);
> +				free(filename);
> +				goto error_cleanup_array;
> +			}
> +
> +			errno = 0;
> +			if (fscanf(sysfsfp, "%u", &current->index) != 1) {
> +				ret = errno ? -errno : -ENODATA;
> +				if (fclose(sysfsfp))
> +					perror("build_channel_array(): Failed to close file");
> +
> +				free(filename);
> +				goto error_cleanup_array;
> +			}
> +
> +			if (fclose(sysfsfp)) {
> +				ret = -errno;
> +				free(filename);
> +				goto error_cleanup_array;
> +			}
> +
>  			free(filename);
>  			/* Find the scale */
>  			ret = iioutils_get_param_float(&current->scale,
> @@ -410,10 +477,16 @@ int build_channel_array(const char *device_dir,
>  						device_dir,
>  						current->name,
>  						current->generic_name);
> +			if (ret < 0)
> +				goto error_cleanup_array;
>  		}
>  	}
>  
> -	closedir(dp);
> +	if (closedir(dp) == -1) {
> +		ret = -errno;
> +		goto error_cleanup_array;
> +	}
> +
>  	free(scan_el_dir);
>  	/* reorder so that the array is in index order */
>  	bsort_channel_array_by_index(ci_array, *counter);
> @@ -427,7 +500,10 @@ error_cleanup_array:
>  	}
>  	free(*ci_array);
>  error_close_dir:
> -	closedir(dp);
> +	if (dp)
> +		if (closedir(dp) == -1)
> +			perror("build_channel_array(): Failed to close dir");
> +
>  error_free_name:
>  	free(scan_el_dir);
>  error_ret:
> @@ -496,29 +572,45 @@ int find_type_by_name(const char *name, const char *type)
>  						+ numstrlen
>  						+ 6);
>  				if (filename == NULL) {
> -					closedir(dp);
> -					return -ENOMEM;
> +					ret = -ENOMEM;
> +					goto error_close_dir;
> +				}
> +
> +				ret = sprintf(filename, "%s%s%d/name", iio_dir,
> +					      type, number);
> +				if (ret < 0) {
> +					free(filename);
> +					goto error_close_dir;
>  				}
> -				sprintf(filename, "%s%s%d/name",
> -					iio_dir,
> -					type,
> -					number);
> +
>  				nameFile = fopen(filename, "r");
>  				if (!nameFile) {
>  					free(filename);
>  					continue;
>  				}
>  				free(filename);
> -				fscanf(nameFile, "%s", thisname);
> -				fclose(nameFile);
> +				errno = 0;
> +				if (fscanf(nameFile, "%s", thisname) != 1) {
> +					ret = errno ? -errno : -ENODATA;
> +					goto error_close_dir;
> +				}
> +
> +				if (fclose(nameFile)) {
> +					ret = -errno;
> +					goto error_close_dir;
> +				}
> +
>  				if (strcmp(name, thisname) == 0) {
> -					closedir(dp);
> +					if (closedir(dp) == -1)
> +						return -errno;
>  					return number;
>  				}
>  			}
>  		}
>  	}
> -	closedir(dp);
> +	if (closedir(dp) == -1)
> +		return -errno;
> +
>  	return -ENODEV;
>  
>  error_close_dir:
> @@ -536,15 +628,29 @@ static int _write_sysfs_int(char *filename, char *basedir, int val, int verify)
>  
>  	if (temp == NULL)
>  		return -ENOMEM;
> -	sprintf(temp, "%s/%s", basedir, filename);
> +	ret = sprintf(temp, "%s/%s", basedir, filename);
> +	if (ret < 0)
> +		goto error_free;
> +
>  	sysfsfp = fopen(temp, "w");
>  	if (sysfsfp == NULL) {
>  		ret = -errno;
>  		printf("failed to open %s\n", temp);
>  		goto error_free;
>  	}
> -	fprintf(sysfsfp, "%d", val);
> -	fclose(sysfsfp);
> +	ret = fprintf(sysfsfp, "%d", val);
> +	if (ret < 0) {
> +		if (fclose(sysfsfp))
> +			perror("_write_sysfs_int(): Failed to close dir");
> +
> +		goto error_free;
> +	}
> +
> +	if (fclose(sysfsfp)) {
> +		ret = -errno;
> +		goto error_free;
> +	}
> +
>  	if (verify) {
>  		sysfsfp = fopen(temp, "r");
>  		if (sysfsfp == NULL) {
> @@ -552,8 +658,19 @@ static int _write_sysfs_int(char *filename, char *basedir, int val, int verify)
>  			printf("failed to open %s\n", temp);
>  			goto error_free;
>  		}
> -		fscanf(sysfsfp, "%d", &test);
> -		fclose(sysfsfp);
> +		if (fscanf(sysfsfp, "%d", &test) != 1) {
> +			ret = errno ? -errno : -ENODATA;
> +			if (fclose(sysfsfp))
> +				perror("_write_sysfs_int(): Failed to close dir");
> +
> +			goto error_free;
> +		}
> +
> +		if (fclose(sysfsfp)) {
> +			ret = -errno;
> +			goto error_free;
> +		}
> +
>  		if (test != val) {
>  			printf("Possible failure in int write %d to %s%s\n",
>  				val,
> @@ -588,15 +705,29 @@ static int _write_sysfs_string(char *filename, char *basedir, char *val,
>  		printf("Memory allocation failed\n");
>  		return -ENOMEM;
>  	}
> -	sprintf(temp, "%s/%s", basedir, filename);
> +	ret = sprintf(temp, "%s/%s", basedir, filename);
> +	if (ret < 0)
> +		goto error_free;
> +
>  	sysfsfp = fopen(temp, "w");
>  	if (sysfsfp == NULL) {
>  		ret = -errno;
>  		printf("Could not open %s\n", temp);
>  		goto error_free;
>  	}
> -	fprintf(sysfsfp, "%s", val);
> -	fclose(sysfsfp);
> +	ret = fprintf(sysfsfp, "%s", val);
> +	if (ret < 0) {
> +		if (fclose(sysfsfp))
> +			perror("_write_sysfs_string(): Failed to close dir");
> +
> +		goto error_free;
> +	}
> +
> +	if (fclose(sysfsfp)) {
> +		ret = -errno;
> +		goto error_free;
> +	}
> +
>  	if (verify) {
>  		sysfsfp = fopen(temp, "r");
>  		if (sysfsfp == NULL) {
> @@ -604,8 +735,19 @@ static int _write_sysfs_string(char *filename, char *basedir, char *val,
>  			printf("could not open file to verify\n");
>  			goto error_free;
>  		}
> -		fscanf(sysfsfp, "%s", temp);
> -		fclose(sysfsfp);
> +		if (fscanf(sysfsfp, "%s", temp) != 1) {
> +			ret = errno ? -errno : -ENODATA;
> +			if (fclose(sysfsfp))
> +				perror("_write_sysfs_string(): Failed to close dir");
> +
> +			goto error_free;
> +		}
> +
> +		if (fclose(sysfsfp)) {
> +			ret = -errno;
> +			goto error_free;
> +		}
> +
>  		if (strcmp(temp, val) != 0) {
>  			printf("Possible failure in string write of %s "
>  				"Should be %s "
> @@ -649,14 +791,27 @@ int read_sysfs_posint(char *filename, char *basedir)
>  		printf("Memory allocation failed");
>  		return -ENOMEM;
>  	}
> -	sprintf(temp, "%s/%s", basedir, filename);
> +	ret = sprintf(temp, "%s/%s", basedir, filename);
> +	if (ret < 0)
> +		goto error_free;
> +
>  	sysfsfp = fopen(temp, "r");
>  	if (sysfsfp == NULL) {
>  		ret = -errno;
>  		goto error_free;
>  	}
> -	fscanf(sysfsfp, "%d\n", &ret);
> -	fclose(sysfsfp);
> +	errno = 0;
> +	if (fscanf(sysfsfp, "%d\n", &ret) != 1) {
> +		ret = errno ? -errno : -ENODATA;
> +		if (fclose(sysfsfp))
> +			perror("read_sysfs_posint(): Failed to close dir");
> +
> +		goto error_free;
> +	}
> +
> +	if (fclose(sysfsfp))
> +		ret = -errno;
> +
>  error_free:
>  	free(temp);
>  	return ret;
> @@ -672,14 +827,27 @@ int read_sysfs_float(char *filename, char *basedir, float *val)
>  		printf("Memory allocation failed");
>  		return -ENOMEM;
>  	}
> -	sprintf(temp, "%s/%s", basedir, filename);
> +	ret = sprintf(temp, "%s/%s", basedir, filename);
> +	if (ret < 0)
> +		goto error_free;
> +
>  	sysfsfp = fopen(temp, "r");
>  	if (sysfsfp == NULL) {
>  		ret = -errno;
>  		goto error_free;
>  	}
> -	fscanf(sysfsfp, "%f\n", val);
> -	fclose(sysfsfp);
> +	errno = 0;
> +	if (fscanf(sysfsfp, "%f\n", val) != 1) {
> +		ret = errno ? -errno : -ENODATA;
> +		if (fclose(sysfsfp))
> +			perror("read_sysfs_float(): Failed to close dir");
> +
> +		goto error_free;
> +	}
> +
> +	if (fclose(sysfsfp))
> +		ret = -errno;
> +
>  error_free:
>  	free(temp);
>  	return ret;
> @@ -695,14 +863,27 @@ int read_sysfs_string(const char *filename, const char *basedir, char *str)
>  		printf("Memory allocation failed");
>  		return -ENOMEM;
>  	}
> -	sprintf(temp, "%s/%s", basedir, filename);
> +	ret = sprintf(temp, "%s/%s", basedir, filename);
> +	if (ret < 0)
> +		goto error_free;
> +
>  	sysfsfp = fopen(temp, "r");
>  	if (sysfsfp == NULL) {
>  		ret = -errno;
>  		goto error_free;
>  	}
> -	fscanf(sysfsfp, "%s\n", str);
> -	fclose(sysfsfp);
> +	errno = 0;
> +	if (fscanf(sysfsfp, "%s\n", str) != 1) {
> +		ret = errno ? -errno : -ENODATA;
> +		if (fclose(sysfsfp))
> +			perror("read_sysfs_string(): Failed to close dir");
> +
> +		goto error_free;
> +	}
> +
> +	if (fclose(sysfsfp))
> +		ret = -errno;
> +
>  error_free:
>  	free(temp);
>  	return ret;
> 


  reply	other threads:[~2015-06-01 21:16 UTC|newest]

Thread overview: 62+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-31 12:39 [PATCH 00/32] iio-tools out-of-staging cleanup Hartmut Knaack
2015-05-31 12:39 ` [PATCH 01/32] tools:iio:generic_buffer: fix order of freeing data Hartmut Knaack
2015-05-31 16:13   ` Jonathan Cameron
2015-05-31 12:39 ` [PATCH 02/32] tools:iio:generic_buffer: free dev_dir_name on exit Hartmut Knaack
2015-05-31 16:17   ` Jonathan Cameron
2015-05-31 12:39 ` [PATCH 03/32] tools:iio:iio_utils: free scan_el_dir " Hartmut Knaack
2015-05-31 16:19   ` Jonathan Cameron
2015-05-31 12:39 ` [PATCH 04/32] tools:iio: free channel-array completely Hartmut Knaack
2015-05-31 16:20   ` Jonathan Cameron
2015-05-31 12:39 ` [PATCH 05/32] tools:iio:iio_utils: fix allocation handling Hartmut Knaack
2015-05-31 16:23   ` Jonathan Cameron
2015-05-31 12:39 ` [PATCH 06/32] tools:iio:generic_buffer: add check before free Hartmut Knaack
2015-05-31 16:29   ` Jonathan Cameron
2015-05-31 12:39 ` [PATCH 07/32] tools:iio:lsiio: add closedir before exit Hartmut Knaack
2015-05-31 16:30   ` Jonathan Cameron
2015-05-31 12:39 ` [PATCH 08/32] tools:iio: save errno first Hartmut Knaack
2015-05-31 16:33   ` Jonathan Cameron
2015-05-31 12:39 ` [PATCH 09/32] tools:iio:iio_event_monitor: save right errno Hartmut Knaack
2015-05-31 16:34   ` Jonathan Cameron
2015-05-31 12:39 ` [PATCH 10/32] tools:iio:generic_buffer: fix check of errno Hartmut Knaack
2015-05-31 16:35   ` Jonathan Cameron
2015-05-31 12:39 ` [PATCH 11/32] tools:iio:generic_buffer: pass up right error code Hartmut Knaack
2015-05-31 16:36   ` Jonathan Cameron
2015-05-31 12:39 ` [PATCH 12/32] tools:iio:generic_buffer: sign-extend and shift data Hartmut Knaack
2015-05-31 16:41   ` Jonathan Cameron
2015-05-31 12:39 ` [PATCH 13/32] tools:iio:iio_utils: check amount of matches Hartmut Knaack
2015-05-31 16:41   ` Jonathan Cameron
2015-05-31 12:40 ` [PATCH 14/32] tools:iio:iio_utils: implement digit calculation Hartmut Knaack
2015-05-31 18:19   ` Jonathan Cameron
2015-05-31 12:40 ` [PATCH 15/32] tools:iio:iio_utils: mark private function static Hartmut Knaack
2015-05-31 12:40 ` [PATCH 16/32] tools:iio: catch errors in string allocation Hartmut Knaack
2015-05-31 18:21   ` Jonathan Cameron
2015-05-31 12:40 ` [PATCH 17/32] tools:iio:generic_buffer: catch errors for arguments conversion Hartmut Knaack
2015-05-31 12:40 ` [PATCH 18/32] tools:iio:generic_buffer: add error handling Hartmut Knaack
2015-05-31 12:40 ` [PATCH 19/32] tools:iio:iio_event_monitor: " Hartmut Knaack
2015-05-31 18:25   ` Jonathan Cameron
2015-05-31 12:40 ` [PATCH 20/32] tools:iio:iio_event_monitor: check if event is fully read Hartmut Knaack
2015-05-31 18:36   ` Jonathan Cameron
2015-06-01 22:00     ` Hartmut Knaack
2015-05-31 12:40 ` [PATCH 21/32] tools:iio:iio_utils: add error handling Hartmut Knaack
2015-05-31 18:44   ` Jonathan Cameron [this message]
2015-05-31 12:40 ` [PATCH 22/32] tools:iio:lsiio: " Hartmut Knaack
2015-05-31 18:45   ` Jonathan Cameron
2015-05-31 12:40 ` [PATCH 23/32] tools:iio:iio_utils: add missing documentation Hartmut Knaack
2015-06-01  7:33   ` Jonathan Cameron
2015-05-31 12:40 ` [PATCH 24/32] tools:iio: return values directly Hartmut Knaack
2015-06-01  7:34   ` Jonathan Cameron
2015-05-31 12:40 ` [PATCH 25/32] tools:iio:iio_event_monitor: refactor events output Hartmut Knaack
2015-06-01  7:34   ` Jonathan Cameron
2015-05-31 12:40 ` [PATCH 26/32] tools:iio:iio_utils: refactor assignment of is_signed Hartmut Knaack
2015-06-01  7:35   ` Jonathan Cameron
2015-05-31 12:40 ` [PATCH 27/32] tools:iio:iio_utils: move up reset of sysfsfp Hartmut Knaack
2015-06-01  7:37   ` Jonathan Cameron
2015-05-31 12:40 ` [PATCH 28/32] tools:iio:iio_utils: initialize count during declaration Hartmut Knaack
2015-06-01  7:38   ` Jonathan Cameron
2015-05-31 12:40 ` [PATCH 29/32] tools:iio: rework program parameters Hartmut Knaack
2015-06-01  7:40   ` Jonathan Cameron
2015-05-31 12:40 ` [PATCH 30/32] tools:iio:iio_utils: pass strings as const Hartmut Knaack
2015-06-01  7:40   ` Jonathan Cameron
2015-05-31 12:40 ` [PATCH 31/32] tools:iio: adjust coding style Hartmut Knaack
2015-06-01  7:46   ` Jonathan Cameron
2015-05-31 12:40 ` [PATCH 32/32] tools:iio: rename variables Hartmut Knaack

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=556B567B.5030600@kernel.org \
    --to=jic23@kernel.org \
    --cc=daniel.baluta@intel.com \
    --cc=irina.tirdea@intel.com \
    --cc=knaack.h@gmx.de \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=pmeerw@pmeerw.net \
    --cc=roberta.dobrescu@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.