1
0
mirror of https://git.FreeBSD.org/src.git synced 2024-11-21 07:15:49 +00:00

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
This commit is contained in:
Faraz Vahedi 2024-08-21 16:49:38 +03:30 committed by Warner Losh
parent c3c06f94f0
commit f144058b40

View File

@ -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);
}