[PATCH 4/4] loadenv: detect truncated environment files

Sascha Hauer s.hauer at pengutronix.de
Wed Apr 9 00:39:26 PDT 2014


Properly detect when an environment file is truncated. This can happen
when a previous saveenv failed because the environment partition is too
small.

Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>
---
 common/environment.c | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)

diff --git a/common/environment.c b/common/environment.c
index bf813b4..abd69c5 100644
--- a/common/environment.c
+++ b/common/environment.c
@@ -409,11 +409,24 @@ int envfs_load(const char *filename, const char *dir, unsigned flags)
 		goto out;
 
 	buf = xmalloc(size);
-	ret = read(envfd, buf, size);
-	if (ret < size) {
-		perror("read");
-		ret = -errno;
-		goto out;
+
+	while (size) {
+		ssize_t now;
+
+		now = read(envfd, buf, size);
+		if (now < 0) {
+			perror("read");
+			ret = -errno;
+			goto out;
+		}
+
+		if (!now) {
+			printf("%s: premature end of file\n", filename);
+			ret = -EINVAL;
+			goto out;
+		}
+
+		size -= now;
 	}
 
 	ret = envfs_check_data(&super, buf, size);
-- 
1.9.1




More information about the barebox mailing list