Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 21 Sep 2024 11:26:23 GMT
From:      Warner Losh <imp@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: f144058b4066 - main - Refactor error handling in lseek operations
Message-ID:  <202409211126.48LBQNmk040280@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=f144058b406656fa4678f8ff2f04c2da46176c79

commit f144058b406656fa4678f8ff2f04c2da46176c79
Author:     Faraz Vahedi <kfv@kfv.io>
AuthorDate: 2024-08-21 13:19:38 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2024-09-21 11:25:15 +0000

    Refactor error handling in lseek operations
    
    1. Subtraction was performed on the current position
       regardless of the success of the lseek operation.
       In the event of an error, this resulted in the
       current position being erroneously set to -2,
       which bypassed the intended error handling
       mechanism. The proposed change performs error
       checking immediately following the lseek operation,
       prior to any modification of the current position.
       This ensures that a failed lseek operation will
       correctly trigger the appropriate error handling.
    
    2. The error checking logic was based on the assumption
       that lseek would return `offset - 1` upon failure.
       However, this is not consistent with the behaviour of
       lseek as specified in the POSIX standard, which
       stipulates that lseek shall return -1 in case of
       an error. The code has been updated to reflect this
       standard, improving reliability and compliance.
    
    Reviewed by: imp
    Pull Request: https://github.com/freebsd/freebsd-src/pull/1392
---
 lib/libfigpar/figpar.c | 30 ++++++++++++------------------
 1 file changed, 12 insertions(+), 18 deletions(-)

diff --git a/lib/libfigpar/figpar.c b/lib/libfigpar/figpar.c
index 19fb91f08900..fbc6aaaba606 100644
--- a/lib/libfigpar/figpar.c
+++ b/lib/libfigpar/figpar.c
@@ -160,11 +160,11 @@ parse_config(struct figpar_config options[], const char *path,
 		}
 
 		/* Get the current offset */
-		curpos = lseek(fd, 0, SEEK_CUR) - 1;
-		if (curpos == -1) {
+		if ((curpos = lseek(fd, 0, SEEK_CUR)) == -1) {
 			close(fd);
 			return (-1);
 		}
+		curpos--;
 
 		/* Find the length of the directive */
 		for (n = 0; r != 0; n++) {
@@ -186,8 +186,7 @@ parse_config(struct figpar_config options[], const char *path,
 		}
 
 		/* Go back to the beginning of the directive */
-		error = (int)lseek(fd, curpos, SEEK_SET);
-		if (error == (curpos - 1)) {
+		if (lseek(fd, curpos, SEEK_SET) == -1) {
 			close(fd);
 			return (-1);
 		}
@@ -245,11 +244,11 @@ parse_config(struct figpar_config options[], const char *path,
 		}
 
 		/* Get the current offset */
-		curpos = lseek(fd, 0, SEEK_CUR) - 1;
-		if (curpos == -1) {
+		if ((curpos = lseek(fd, 0, SEEK_CUR)) == -1) {
 			close(fd);
 			return (-1);
 		}
+		curpos--;
 
 		/* Find the end of the value */
 		quote = 0;
@@ -267,19 +266,18 @@ parse_config(struct figpar_config options[], const char *path,
 			 */
 
 			/* Get the current offset */
-			charpos = lseek(fd, 0, SEEK_CUR) - 1;
-			if (charpos == -1) {
+			if ((charpos = lseek(fd, 0, SEEK_CUR)) == -1) {
 				close(fd);
 				return (-1);
 			}
+			charpos--;
 
 			/*
 			 * Go back so we can read the character before the key
 			 * to check if the character is escaped (which means we
 			 * should continue).
 			 */
-			error = (int)lseek(fd, -2, SEEK_CUR);
-			if (error == -3) {
+			if (lseek(fd, -2, SEEK_CUR) == -1) {
 				close(fd);
 				return (-1);
 			}
@@ -291,8 +289,7 @@ parse_config(struct figpar_config options[], const char *path,
 			 */
 			for (n = 1; *p == '\\'; n++) {
 				/* Move back another offset to read */
-				error = (int)lseek(fd, -2, SEEK_CUR);
-				if (error == -3) {
+				if (lseek(fd, -2, SEEK_CUR) == -1) {
 					close(fd);
 					return (-1);
 				}
@@ -300,8 +297,7 @@ parse_config(struct figpar_config options[], const char *path,
 			}
 
 			/* Move offset back to the key and read it */
-			error = (int)lseek(fd, charpos, SEEK_SET);
-			if (error == (charpos - 1)) {
+			if (lseek(fd, charpos, SEEK_SET) == -1) {
 				close(fd);
 				return (-1);
 			}
@@ -352,8 +348,7 @@ parse_config(struct figpar_config options[], const char *path,
 		}
 
 		/* Get the current offset */
-		charpos = lseek(fd, 0, SEEK_CUR) - 1;
-		if (charpos == -1) {
+		if ((charpos = lseek(fd, 0, SEEK_CUR)) == -1) {
 			close(fd);
 			return (-1);
 		}
@@ -364,8 +359,7 @@ parse_config(struct figpar_config options[], const char *path,
 			n--;
 
 		/* Move offset back to the beginning of the value */
-		error = (int)lseek(fd, curpos, SEEK_SET);
-		if (error == (curpos - 1)) {
+		if (lseek(fd, curpos, SEEK_SET) == -1) {
 			close(fd);
 			return (-1);
 		}



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202409211126.48LBQNmk040280>