fix file-helper return value on premature EOF 38/219238/3
authorMaciej Slodczyk <m.slodczyk2@partner.samsung.com>
Tue, 3 Dec 2019 15:27:45 +0000 (16:27 +0100)
committerMichal Bloch <m.bloch@partner.samsung.com>
Wed, 4 Dec 2019 12:34:51 +0000 (13:34 +0100)
Change-Id: I022f22746db4a9285f50208fe53ec7c49063cb0d
Signed-off-by: Maciej Slodczyk <m.slodczyk2@partner.samsung.com>
src/common/file-helper.c

index 15e3eac..e82a88b 100644 (file)
@@ -46,8 +46,9 @@ int fwrite_str(const char *path, const char *str)
        ret_value_errno_msg_if(!f, -errno,
                               "Fail to open file %s", path);
 
+       errno = 0;
        ret = fputs(str, f);
-       ret_value_errno_msg_if(ret == EOF, errno ? -errno : -EIO,
+       ret_value_errno_msg_if(ret == EOF, -(errno ?: EIO),
                               "Fail to write file");
 
        return RESOURCED_ERROR_NONE;
@@ -115,9 +116,10 @@ int fread_nth_int(const char *path, size_t n, int32_t *number)
        ret_value_errno_msg_if(!f, -errno,
                               "Fail to open  %s file.", path);
 
+       errno = 0;
        for (i = 0; i <= n; i++) {
                ret = fscanf(f, "%d", &t);
-               ret_value_errno_msg_if(ret == EOF, -errno,
+               ret_value_errno_msg_if(ret == EOF, -(errno ?: ENOENT),
                                       "Fail to read file\n");
        }
 
@@ -136,9 +138,10 @@ int fread_nth_uint(const char *path, size_t n, u_int32_t *number)
        ret_value_errno_msg_if(!f, -errno,
                               "Fail to open %s file.", path);
 
+       errno = 0;
        for (i = 0; i <= n; i++) {
                ret = fscanf(f, "%u", &t);
-               ret_value_errno_msg_if(ret == EOF, -errno,
+               ret_value_errno_msg_if(ret == EOF, -(errno ?: ENOENT),
                                       "Fail to read file\n");
        }
 
@@ -157,9 +160,10 @@ int fread_nth_ulong(const char *path, size_t n, unsigned long *number)
        ret_value_errno_msg_if(!f, -errno,
                               "Fail to open %s file.", path);
 
+       errno = 0;
        for (i = 0; i <= n; i++) {
                ret = fscanf(f, "%lu", &t);
-               ret_value_errno_msg_if(ret == EOF, -errno,
+               ret_value_errno_msg_if(ret == EOF, -(errno ?: ENOENT),
                                       "Fail to read file\n");
        }