Skip to contentSkip to conversation

nfs-utils: Fix the error handling if the lseek fails

PATCH In review Alice J Mitchell 2 participants 2 messages Aug 1, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nfs-utils: Fix the error handling if the lseek fails

The error case when lseek returns a negative value was not correctly handled, and the error cleanup routine was potentially leaking memory also.

---
 support/nfs/conffile.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
support/nfs/conffile.c +7 -1
index b6400be..6ba8a35 100644
@@ -500,7 +500,7 @@ conf_readfile(const char *path)
 
 	if ((stat (path, &sb) == 0) || (errno != ENOENT)) {
 		char *new_conf_addr = NULL;
-		size_t sz = sb.st_size;
+		off_t sz;
 		int fd = open (path, O_RDONLY, 0);
 
 		if (fd == -1) {
@@ -517,6 +517,11 @@ conf_readfile(const char *path)
 
 		/* only after we have the lock, check the file size ready to read it */
 		sz = lseek(fd, 0, SEEK_END);
+		if (sz < 0) {
+			xlog_warn("conf_readfile: unable to determine file size: %s",
+				  strerror(errno));
+			goto fail;
+		}
 		lseek(fd, 0, SEEK_SET);
 
 		new_conf_addr = malloc(sz+1);
@@ -2162,6 +2167,7 @@ conf_write(const char *filename, const char *section, const char *arg,
 	ret = 0;
 
 cleanup:
+	flush_outqueue(&inqueue, NULL);
 	flush_outqueue(&outqueue, NULL);
 
 	if (buff)
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 4 lines · Alice J Mitchell

The error case when lseek returns a negative value was not correctly handled, and the error cleanup routine was potentially leaking memory also.

Committed...

steved.

Quoted 38 lines
---
 support/nfs/conffile.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
support/nfs/conffile.c +7 -1
index b6400be..6ba8a35 100644
@@ -500,7 +500,7 @@ conf_readfile(const char *path)
 
 	if ((stat (path, &sb) == 0) || (errno != ENOENT)) {
 		char *new_conf_addr = NULL;
-		size_t sz = sb.st_size;
+		off_t sz;
 		int fd = open (path, O_RDONLY, 0);
 
 		if (fd == -1) {
@@ -517,6 +517,11 @@ conf_readfile(const char *path)
 
 		/* only after we have the lock, check the file size ready to read it */
 		sz = lseek(fd, 0, SEEK_END);
+		if (sz < 0) {
+			xlog_warn("conf_readfile: unable to determine file size: %s",
+				  strerror(errno));
+			goto fail;
+		}
 		lseek(fd, 0, SEEK_SET);
 
 		new_conf_addr = malloc(sz+1);
@@ -2162,6 +2167,7 @@ conf_write(const char *filename, const char *section, const char *arg,
 	ret = 0;
 
 cleanup:
+	flush_outqueue(&inqueue, NULL);
 	flush_outqueue(&outqueue, NULL);
 
 	if (buff)