Linux-XFS Archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 0/3] Refactoring and error handling from Coverity scan fixes
@ 2024-04-17 16:19 Andrey Albershteyn
  2024-04-17 16:19 ` [PATCH v3 1/3] xfs_fsr: replace atoi() with strtol() Andrey Albershteyn
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Andrey Albershteyn @ 2024-04-17 16:19 UTC (permalink / raw
  To: cem, linux-xfs; +Cc: djwong, hch, Andrey Albershteyn

Hi all,

Just a few minor patches from feedback on coverity fixes.

v3:
- error message change to "invalid runtime"
v2:
- reduce unnecessary indentation in new flist_find_type() helper
- make new helper static
- add patch to handle strtol() errors
- set errno to zero to avoid catching random errnos

--
Andrey

--
Andrey

Andrey Albershteyn (3):
  xfs_fsr: replace atoi() with strtol()
  xfs_db: add helper for flist_find_type for clearer field matching
  xfs_repair: catch strtol() errors

 db/flist.c          | 60 ++++++++++++++++++++++++++++-----------------
 fsr/xfs_fsr.c       | 26 +++++++++++++++++---
 repair/xfs_repair.c | 40 +++++++++++++++++++++++++++++-
 3 files changed, 100 insertions(+), 26 deletions(-)

-- 
2.42.0


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

* [PATCH v3 1/3] xfs_fsr: replace atoi() with strtol()
  2024-04-17 16:19 [PATCH v3 0/3] Refactoring and error handling from Coverity scan fixes Andrey Albershteyn
@ 2024-04-17 16:19 ` Andrey Albershteyn
  2024-04-17 16:19 ` [PATCH v3 2/3] xfs_db: add helper for flist_find_type for clearer field matching Andrey Albershteyn
  2024-04-17 16:19 ` [PATCH v3 3/3] xfs_repair: catch strtol() errors Andrey Albershteyn
  2 siblings, 0 replies; 4+ messages in thread
From: Andrey Albershteyn @ 2024-04-17 16:19 UTC (permalink / raw
  To: cem, linux-xfs; +Cc: djwong, hch, Andrey Albershteyn, Christoph Hellwig

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

Signed-off-by: Andrey Albershteyn <aalbersh@redhat.com>
Reviewed-by: Darrick J. Wong <djwong@kernel.org>
Reviewed-by: Christoph Hellwig <hch@lst.de>
---
 fsr/xfs_fsr.c | 26 +++++++++++++++++++++++---
 1 file changed, 23 insertions(+), 3 deletions(-)

diff --git a/fsr/xfs_fsr.c b/fsr/xfs_fsr.c
index 02d61ef9399a..fdd37756030a 100644
--- a/fsr/xfs_fsr.c
+++ b/fsr/xfs_fsr.c
@@ -164,7 +164,13 @@ main(int argc, char **argv)
 			usage(1);
 			break;
 		case 't':
-			howlong = atoi(optarg);
+			errno = 0;
+			howlong = strtol(optarg, NULL, 10);
+			if (errno) {
+				fprintf(stderr, _("%s: invalid runtime: %s\n"),
+					optarg, strerror(errno));
+				exit(1);
+			}
 			if (howlong > INT_MAX) {
 				fprintf(stderr,
 				_("%s: the maximum runtime is %d seconds.\n"),
@@ -179,10 +185,24 @@ main(int argc, char **argv)
 			mtab = optarg;
 			break;
 		case 'b':
-			argv_blksz_dio = atoi(optarg);
+			errno = 0;
+			argv_blksz_dio = strtol(optarg, NULL, 10);
+			if (errno) {
+				fprintf(stderr,
+					_("%s: invalid block size: %s\n"),
+					optarg, strerror(errno));
+				exit(1);
+			}
 			break;
 		case 'p':
-			npasses = atoi(optarg);
+			errno = 0;
+			npasses = strtol(optarg, NULL, 10);
+			if (errno) {
+				fprintf(stderr,
+					_("%s: invalid number of passes: %s\n"),
+					optarg, strerror(errno));
+				exit(1);
+			}
 			break;
 		case 'C':
 			/* Testing opt: coerses frag count in result */
-- 
2.42.0


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

* [PATCH v3 2/3] xfs_db: add helper for flist_find_type for clearer field matching
  2024-04-17 16:19 [PATCH v3 0/3] Refactoring and error handling from Coverity scan fixes Andrey Albershteyn
  2024-04-17 16:19 ` [PATCH v3 1/3] xfs_fsr: replace atoi() with strtol() Andrey Albershteyn
@ 2024-04-17 16:19 ` Andrey Albershteyn
  2024-04-17 16:19 ` [PATCH v3 3/3] xfs_repair: catch strtol() errors Andrey Albershteyn
  2 siblings, 0 replies; 4+ messages in thread
From: Andrey Albershteyn @ 2024-04-17 16:19 UTC (permalink / raw
  To: cem, linux-xfs; +Cc: djwong, hch, Andrey Albershteyn, Christoph Hellwig

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

Signed-off-by: Andrey Albershteyn <aalbersh@redhat.com>
Reviewed-by: Darrick J. Wong <djwong@kernel.org>
Reviewed-by: Christoph Hellwig <hch@lst.de>
---
 db/flist.c | 60 ++++++++++++++++++++++++++++++++++--------------------
 1 file changed, 38 insertions(+), 22 deletions(-)

diff --git a/db/flist.c b/db/flist.c
index 0a6cc5fcee43..ab0a0f133804 100644
--- a/db/flist.c
+++ b/db/flist.c
@@ -400,6 +400,40 @@ flist_split(
 	return v;
 }
 
+static 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;
+	flist_t			*nfl;
+
+	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)
+		goto out;
+
+	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,15 @@ 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)
+		fl = flist_field_match(f, type, obj, startoff);
+		if (fl)
 			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] 4+ messages in thread

* [PATCH v3 3/3] xfs_repair: catch strtol() errors
  2024-04-17 16:19 [PATCH v3 0/3] Refactoring and error handling from Coverity scan fixes Andrey Albershteyn
  2024-04-17 16:19 ` [PATCH v3 1/3] xfs_fsr: replace atoi() with strtol() Andrey Albershteyn
  2024-04-17 16:19 ` [PATCH v3 2/3] xfs_db: add helper for flist_find_type for clearer field matching Andrey Albershteyn
@ 2024-04-17 16:19 ` Andrey Albershteyn
  2 siblings, 0 replies; 4+ messages in thread
From: Andrey Albershteyn @ 2024-04-17 16:19 UTC (permalink / raw
  To: cem, linux-xfs; +Cc: djwong, hch, Andrey Albershteyn, Christoph Hellwig

strtol() sets errno if string parsing. Abort and tell user which
parameter is wrong.

Signed-off-by: Andrey Albershteyn <aalbersh@redhat.com>
Reviewed-by: Darrick J. Wong <djwong@kernel.org>
Reviewed-by: Christoph Hellwig <hch@lst.de>
---
 repair/xfs_repair.c | 40 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 39 insertions(+), 1 deletion(-)

diff --git a/repair/xfs_repair.c b/repair/xfs_repair.c
index 2ceea87dc57d..2fc89dac345d 100644
--- a/repair/xfs_repair.c
+++ b/repair/xfs_repair.c
@@ -252,14 +252,22 @@ process_args(int argc, char **argv)
 					if (!val)
 						do_abort(
 		_("-o bhash requires a parameter\n"));
+					errno = 0;
 					libxfs_bhash_size = (int)strtol(val, NULL, 0);
+					if (errno)
+						do_abort(
+		_("-o bhash invalid parameter: %s\n"), strerror(errno));
 					bhash_option_used = 1;
 					break;
 				case AG_STRIDE:
 					if (!val)
 						do_abort(
 		_("-o ag_stride requires a parameter\n"));
+					errno = 0;
 					ag_stride = (int)strtol(val, NULL, 0);
+					if (errno)
+						do_abort(
+		_("-o ag_stride invalid parameter: %s\n"), strerror(errno));
 					break;
 				case FORCE_GEO:
 					if (val)
@@ -272,19 +280,31 @@ process_args(int argc, char **argv)
 					if (!val)
 						do_abort(
 		_("-o phase2_threads requires a parameter\n"));
+					errno = 0;
 					phase2_threads = (int)strtol(val, NULL, 0);
+					if (errno)
+						do_abort(
+		_("-o phase2_threads invalid parameter: %s\n"), strerror(errno));
 					break;
 				case BLOAD_LEAF_SLACK:
 					if (!val)
 						do_abort(
 		_("-o debug_bload_leaf_slack requires a parameter\n"));
+					errno = 0;
 					bload_leaf_slack = (int)strtol(val, NULL, 0);
+					if (errno)
+						do_abort(
+		_("-o debug_bload_leaf_slack invalid parameter: %s\n"), strerror(errno));
 					break;
 				case BLOAD_NODE_SLACK:
 					if (!val)
 						do_abort(
 		_("-o debug_bload_node_slack requires a parameter\n"));
+					errno = 0;
 					bload_node_slack = (int)strtol(val, NULL, 0);
+					if (errno)
+						do_abort(
+		_("-o debug_bload_node_slack invalid parameter: %s\n"), strerror(errno));
 					break;
 				case NOQUOTA:
 					quotacheck_skip();
@@ -305,7 +325,11 @@ process_args(int argc, char **argv)
 					if (!val)
 						do_abort(
 		_("-c lazycount requires a parameter\n"));
+					errno = 0;
 					lazy_count = (int)strtol(val, NULL, 0);
+					if (errno)
+						do_abort(
+		_("-o lazycount invalid parameter: %s\n"), strerror(errno));
 					convert_lazy_count = 1;
 					break;
 				case CONVERT_INOBTCOUNT:
@@ -356,7 +380,11 @@ process_args(int argc, char **argv)
 			if (bhash_option_used)
 				do_abort(_("-m option cannot be used with "
 						"-o bhash option\n"));
+			errno = 0;
 			max_mem_specified = strtol(optarg, NULL, 0);
+			if (errno)
+				do_abort(
+		_("%s: invalid memory amount: %s\n"), optarg, strerror(errno));
 			break;
 		case 'L':
 			zap_log = 1;
@@ -377,7 +405,11 @@ process_args(int argc, char **argv)
 			do_prefetch = 0;
 			break;
 		case 't':
+			errno = 0;
 			report_interval = strtol(optarg, NULL, 0);
+			if (errno)
+				do_abort(
+		_("%s: invalid interval: %s\n"), optarg, strerror(errno));
 			break;
 		case 'e':
 			report_corrected = true;
@@ -397,8 +429,14 @@ process_args(int argc, char **argv)
 		usage();
 
 	p = getenv("XFS_REPAIR_FAIL_AFTER_PHASE");
-	if (p)
+	if (p) {
+		errno = 0;
 		fail_after_phase = (int)strtol(p, NULL, 0);
+		if (errno)
+			do_abort(
+		_("%s: invalid phase in XFS_REPAIR_FAIL_AFTER_PHASE: %s\n"),
+				p, strerror(errno));
+	}
 }
 
 void __attribute__((noreturn))
-- 
2.42.0


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

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

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-04-17 16:19 [PATCH v3 0/3] Refactoring and error handling from Coverity scan fixes Andrey Albershteyn
2024-04-17 16:19 ` [PATCH v3 1/3] xfs_fsr: replace atoi() with strtol() Andrey Albershteyn
2024-04-17 16:19 ` [PATCH v3 2/3] xfs_db: add helper for flist_find_type for clearer field matching Andrey Albershteyn
2024-04-17 16:19 ` [PATCH v3 3/3] xfs_repair: catch strtol() errors Andrey Albershteyn

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).