public inbox for linux-xfs@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] Refactoring from Coverity scan fixes
@ 2024-04-16 20:28 Andrey Albershteyn
  2024-04-16 20:28 ` [PATCH 1/2] xfs_fsr: replace atoi() with strtol() Andrey Albershteyn
  2024-04-16 20:28 ` [PATCH 2/2] xfs_db: add helper for flist_find_type for clearer field matching Andrey Albershteyn
  0 siblings, 2 replies; 6+ messages in thread
From: Andrey Albershteyn @ 2024-04-16 20:28 UTC (permalink / raw)
  To: cem, linux-xfs; +Cc: djwong, hch, Andrey Albershteyn

Hi all,

Just a two minor patches from feedback on Coverity fixes.

--
Andrey

Andrey Albershteyn (2):
  xfs_fsr: replace atoi() with strtol()
  xfs_db: add helper for flist_find_type for clearer field matching

 db/flist.c    | 59 ++++++++++++++++++++++++++++++++-------------------
 fsr/xfs_fsr.c | 23 +++++++++++++++++---
 2 files changed, 57 insertions(+), 25 deletions(-)

-- 
2.42.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 1/2] xfs_fsr: replace atoi() with strtol()
  2024-04-16 20:28 [PATCH 0/2] Refactoring from Coverity scan fixes Andrey Albershteyn
@ 2024-04-16 20:28 ` Andrey Albershteyn
  2024-04-16 20:49   ` Darrick J. Wong
  2024-04-16 20:28 ` [PATCH 2/2] xfs_db: add helper for flist_find_type for clearer field matching Andrey Albershteyn
  1 sibling, 1 reply; 6+ messages in thread
From: Andrey Albershteyn @ 2024-04-16 20:28 UTC (permalink / raw)
  To: cem, linux-xfs; +Cc: djwong, hch, Andrey Albershteyn

Replace atoi() which silently fails with strtol() and report the
error.

Signed-off-by: Andrey Albershteyn <aalbersh@redhat.com>
---
 fsr/xfs_fsr.c | 23 ++++++++++++++++++++---
 1 file changed, 20 insertions(+), 3 deletions(-)

diff --git a/fsr/xfs_fsr.c b/fsr/xfs_fsr.c
index 4e29a8a2c548..5fabc965183e 100644
--- a/fsr/xfs_fsr.c
+++ b/fsr/xfs_fsr.c
@@ -164,7 +164,12 @@ main(int argc, char **argv)
 			usage(1);
 			break;
 		case 't':
-			howlong = atoi(optarg);
+			howlong = strtol(optarg, NULL, 10);
+			if (errno) {
+				fprintf(stderr, _("%s: invalid interval: %s\n"),
+					progname, strerror(errno));
+				exit(1);
+			}
 			if (howlong > INT_MAX) {
 				fprintf(stderr, _("%s: too long\n"), progname);
 				exit(1);
@@ -177,10 +182,22 @@ main(int argc, char **argv)
 			mtab = optarg;
 			break;
 		case 'b':
-			argv_blksz_dio = atoi(optarg);
+			argv_blksz_dio = strtol(optarg, NULL, 10);
+			if (errno) {
+				fprintf(stderr,
+					_("%s: invalid block size: %s\n"),
+					progname, strerror(errno));
+				exit(1);
+			}
 			break;
 		case 'p':
-			npasses = atoi(optarg);
+			npasses = strtol(optarg, NULL, 10);
+			if (errno) {
+				fprintf(stderr,
+					_("%s: invalid number of passes: %s\n"),
+					progname, strerror(errno));
+				exit(1);
+			}
 			break;
 		case 'C':
 			/* Testing opt: coerses frag count in result */
-- 
2.42.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 2/2] xfs_db: add helper for flist_find_type for clearer field matching
  2024-04-16 20:28 [PATCH 0/2] Refactoring from Coverity scan fixes Andrey Albershteyn
  2024-04-16 20:28 ` [PATCH 1/2] xfs_fsr: replace atoi() with strtol() Andrey Albershteyn
@ 2024-04-16 20:28 ` Andrey Albershteyn
  2024-04-16 20:53   ` Darrick J. Wong
  1 sibling, 1 reply; 6+ messages in thread
From: Andrey Albershteyn @ 2024-04-16 20:28 UTC (permalink / raw)
  To: cem, linux-xfs; +Cc: djwong, hch, Andrey Albershteyn

Make flist_find_type() more readable by unloading field type
matching to the helper.

Signed-off-by: Andrey Albershteyn <aalbersh@redhat.com>
---
 db/flist.c | 59 ++++++++++++++++++++++++++++++++++--------------------
 1 file changed, 37 insertions(+), 22 deletions(-)

diff --git a/db/flist.c b/db/flist.c
index 0a6cc5fcee43..18052a744a65 100644
--- a/db/flist.c
+++ b/db/flist.c
@@ -400,6 +400,40 @@ flist_split(
 	return v;
 }
 
+flist_t *
+flist_field_match(
+	const field_t		*field,
+	fldt_t			type,
+	void			*obj,
+	int			startoff)
+{
+	flist_t			*fl;
+	int			count;
+	const ftattr_t		*fa;
+
+	fl = flist_make(field->name);
+	fl->fld = field;
+	if (field->ftyp == type)
+		return fl;
+	count = fcount(field, obj, startoff);
+	if (!count)
+		goto out;
+	fa = &ftattrtab[field->ftyp];
+	if (fa->subfld) {
+		flist_t *nfl;
+
+		nfl = flist_find_ftyp(fa->subfld, type, obj, startoff);
+		if (nfl) {
+			fl->child = nfl;
+			return fl;
+		}
+	}
+
+out:
+	flist_free(fl);
+	return NULL;
+}
+
 /*
  * Given a set of fields, scan for a field of the given type.
  * Return an flist leading to the first found field
@@ -413,33 +447,14 @@ flist_find_ftyp(
 	void		*obj,
 	int		startoff)
 {
-	flist_t	*fl;
 	const field_t	*f;
-	int		count;
-	const ftattr_t  *fa;
+	flist_t		*fl;
 
 	for (f = fields; f->name; f++) {
-		fl = flist_make(f->name);
-		fl->fld = f;
-		if (f->ftyp == type)
+		if ((fl = flist_field_match(f, type, obj, startoff)) != NULL)
 			return fl;
-		count = fcount(f, obj, startoff);
-		if (!count) {
-			flist_free(fl);
-			continue;
-		}
-		fa = &ftattrtab[f->ftyp];
-		if (fa->subfld) {
-			flist_t *nfl;
-
-			nfl = flist_find_ftyp(fa->subfld, type, obj, startoff);
-			if (nfl) {
-				fl->child = nfl;
-				return fl;
-			}
-		}
-		flist_free(fl);
 	}
+
 	return NULL;
 }
 
-- 
2.42.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/2] xfs_fsr: replace atoi() with strtol()
  2024-04-16 20:28 ` [PATCH 1/2] xfs_fsr: replace atoi() with strtol() Andrey Albershteyn
@ 2024-04-16 20:49   ` Darrick J. Wong
  0 siblings, 0 replies; 6+ messages in thread
From: Darrick J. Wong @ 2024-04-16 20:49 UTC (permalink / raw)
  To: Andrey Albershteyn; +Cc: cem, linux-xfs, hch

On Tue, Apr 16, 2024 at 10:28:41PM +0200, Andrey Albershteyn wrote:
> Replace atoi() which silently fails with strtol() and report the
> error.
> 
> Signed-off-by: Andrey Albershteyn <aalbersh@redhat.com>
> ---
>  fsr/xfs_fsr.c | 23 ++++++++++++++++++++---
>  1 file changed, 20 insertions(+), 3 deletions(-)
> 
> diff --git a/fsr/xfs_fsr.c b/fsr/xfs_fsr.c
> index 4e29a8a2c548..5fabc965183e 100644
> --- a/fsr/xfs_fsr.c
> +++ b/fsr/xfs_fsr.c
> @@ -164,7 +164,12 @@ main(int argc, char **argv)
>  			usage(1);
>  			break;
>  		case 't':
> -			howlong = atoi(optarg);
> +			howlong = strtol(optarg, NULL, 10);

C library functions don't clear errno; they only set it after something
goes wrong.  For functions that don't return -1 to indicate that
something went wrong, you have to clear errno explicitly before calling
the function:

	errno = 0;
	howlong = strtol(optarg, NULL, 10);
	if (errno)
		fprintf(...);

If you don't clear it, you can then pick up an errno set by some past
library call, which can lead to strange error messages.

--D

> +			if (errno) {
> +				fprintf(stderr, _("%s: invalid interval: %s\n"),
> +					progname, strerror(errno));
> +				exit(1);
> +			}
>  			if (howlong > INT_MAX) {
>  				fprintf(stderr, _("%s: too long\n"), progname);
>  				exit(1);
> @@ -177,10 +182,22 @@ main(int argc, char **argv)
>  			mtab = optarg;
>  			break;
>  		case 'b':
> -			argv_blksz_dio = atoi(optarg);
> +			argv_blksz_dio = strtol(optarg, NULL, 10);
> +			if (errno) {
> +				fprintf(stderr,
> +					_("%s: invalid block size: %s\n"),
> +					progname, strerror(errno));
> +				exit(1);
> +			}
>  			break;
>  		case 'p':
> -			npasses = atoi(optarg);
> +			npasses = strtol(optarg, NULL, 10);
> +			if (errno) {
> +				fprintf(stderr,
> +					_("%s: invalid number of passes: %s\n"),
> +					progname, strerror(errno));
> +				exit(1);
> +			}
>  			break;
>  		case 'C':
>  			/* Testing opt: coerses frag count in result */
> -- 
> 2.42.0
> 
> 

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 2/2] xfs_db: add helper for flist_find_type for clearer field matching
  2024-04-16 20:28 ` [PATCH 2/2] xfs_db: add helper for flist_find_type for clearer field matching Andrey Albershteyn
@ 2024-04-16 20:53   ` Darrick J. Wong
  2024-04-17  5:03     ` Christoph Hellwig
  0 siblings, 1 reply; 6+ messages in thread
From: Darrick J. Wong @ 2024-04-16 20:53 UTC (permalink / raw)
  To: Andrey Albershteyn; +Cc: cem, linux-xfs, hch

On Tue, Apr 16, 2024 at 10:28:42PM +0200, Andrey Albershteyn wrote:
> Make flist_find_type() more readable by unloading field type
> matching to the helper.
> 
> Signed-off-by: Andrey Albershteyn <aalbersh@redhat.com>
> ---
>  db/flist.c | 59 ++++++++++++++++++++++++++++++++++--------------------
>  1 file changed, 37 insertions(+), 22 deletions(-)
> 
> diff --git a/db/flist.c b/db/flist.c
> index 0a6cc5fcee43..18052a744a65 100644
> --- a/db/flist.c
> +++ b/db/flist.c
> @@ -400,6 +400,40 @@ flist_split(
>  	return v;
>  }
>  
> +flist_t *
> +flist_field_match(

Is this function going to be used outside of this module, or should it
be declared static?

> +	const field_t		*field,
> +	fldt_t			type,
> +	void			*obj,
> +	int			startoff)
> +{
> +	flist_t			*fl;
> +	int			count;
> +	const ftattr_t		*fa;
> +
> +	fl = flist_make(field->name);
> +	fl->fld = field;
> +	if (field->ftyp == type)
> +		return fl;
> +	count = fcount(field, obj, startoff);
> +	if (!count)
> +		goto out;
> +	fa = &ftattrtab[field->ftyp];
> +	if (fa->subfld) {

You could do:

	if (!fa->subfld)
		goto out;

	nfl = flist_find_ftyp(...);

to reduce the indenting here.  But I think you were trying to make
before and after as identical as possible, right?

> +		flist_t *nfl;
> +
> +		nfl = flist_find_ftyp(fa->subfld, type, obj, startoff);
> +		if (nfl) {
> +			fl->child = nfl;
> +			return fl;
> +		}
> +	}
> +
> +out:
> +	flist_free(fl);
> +	return NULL;
> +}
> +
>  /*
>   * Given a set of fields, scan for a field of the given type.
>   * Return an flist leading to the first found field
> @@ -413,33 +447,14 @@ flist_find_ftyp(
>  	void		*obj,
>  	int		startoff)
>  {
> -	flist_t	*fl;
>  	const field_t	*f;
> -	int		count;
> -	const ftattr_t  *fa;
> +	flist_t		*fl;
>  
>  	for (f = fields; f->name; f++) {
> -		fl = flist_make(f->name);
> -		fl->fld = f;
> -		if (f->ftyp == type)
> +		if ((fl = flist_field_match(f, type, obj, startoff)) != NULL)
>  			return fl;

Normally these days we expand assign-and-check when not in a loop
control test:

		fl = flist_field_match(...);
		if (fl)
			return fl;

But those last two comments merely reflect my style preference; the
question I care most about is the one about the static attribute.

--D

> -		count = fcount(f, obj, startoff);
> -		if (!count) {
> -			flist_free(fl);
> -			continue;
> -		}
> -		fa = &ftattrtab[f->ftyp];
> -		if (fa->subfld) {
> -			flist_t *nfl;
> -
> -			nfl = flist_find_ftyp(fa->subfld, type, obj, startoff);
> -			if (nfl) {
> -				fl->child = nfl;
> -				return fl;
> -			}
> -		}
> -		flist_free(fl);
>  	}
> +
>  	return NULL;
>  }
>  
> -- 
> 2.42.0
> 
> 

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 2/2] xfs_db: add helper for flist_find_type for clearer field matching
  2024-04-16 20:53   ` Darrick J. Wong
@ 2024-04-17  5:03     ` Christoph Hellwig
  0 siblings, 0 replies; 6+ messages in thread
From: Christoph Hellwig @ 2024-04-17  5:03 UTC (permalink / raw)
  To: Darrick J. Wong; +Cc: Andrey Albershteyn, cem, linux-xfs, hch

On Tue, Apr 16, 2024 at 01:53:48PM -0700, Darrick J. Wong wrote:
> You could do:
> 
> 	if (!fa->subfld)
> 		goto out;
> 
> 	nfl = flist_find_ftyp(...);
> 
> to reduce the indenting here.  But I think you were trying to make
> before and after as identical as possible, right?

FYI, the goto version s what I had in mind with my original suggestion,
although the version here shoud be ok as well.  We'll need the static
either way, though.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-04-17  5:03 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-04-16 20:28 [PATCH 0/2] Refactoring from Coverity scan fixes Andrey Albershteyn
2024-04-16 20:28 ` [PATCH 1/2] xfs_fsr: replace atoi() with strtol() Andrey Albershteyn
2024-04-16 20:49   ` Darrick J. Wong
2024-04-16 20:28 ` [PATCH 2/2] xfs_db: add helper for flist_find_type for clearer field matching Andrey Albershteyn
2024-04-16 20:53   ` Darrick J. Wong
2024-04-17  5:03     ` Christoph Hellwig

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox