9
0
Fork 0

loadenv: detect truncated environment files

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@pengutronix.de>
This commit is contained in:
Sascha Hauer 2014-04-08 17:32:05 +02:00
parent aa49d86b2d
commit 55fb660e0b
1 changed files with 24 additions and 7 deletions

View File

@ -388,10 +388,10 @@ int envfs_load_from_buf(void *buf, int len, const char *dir, unsigned flags)
int envfs_load(const char *filename, const char *dir, unsigned flags)
{
struct envfs_super super;
void *buf = NULL;
void *buf = NULL, *rbuf;
int envfd;
int ret = 0;
size_t size;
size_t size, rsize;
envfd = open(filename, O_RDONLY);
if (envfd < 0) {
@ -412,11 +412,28 @@ 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;
rbuf = buf;
rsize = size;
while (rsize) {
ssize_t now;
now = read(envfd, rbuf, rsize);
if (now < 0) {
perror("read");
ret = -errno;
goto out;
}
if (!now) {
printf("%s: premature end of file\n", filename);
ret = -EINVAL;
goto out;
}
rbuf += now;
rsize -= now;
}
ret = envfs_check_data(&super, buf, size);