The line_ptr is shared globally. It can happen that another function like
parse_hosts_file() allocated the buffer successfully. But the next
getline() in read_file() fails - but keeps the line_ptr valid. In this
case, the function would return a success - even when the buffer contains
stale data.

Instead only set the return value to EXIT_SUCCESS when a single line could
be read.

Fixes: deb324e65044 ("batctl: buffer based reading replaced by line-by-line 
reading")
Signed-off-by: Sven Eckelmann <[email protected]>
---
 functions.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/functions.c b/functions.c
index 00dbd3d..349569d 100644
--- a/functions.c
+++ b/functions.c
@@ -147,6 +147,8 @@ int read_file(const char *full_path, int read_opt)
        }
 
        while (getline(&line_ptr, &len, fp) != -1) {
+               res = EXIT_SUCCESS;
+
                /* the buffer will be handled elsewhere */
                if (read_opt & USE_READ_BUFF)
                        break;
@@ -154,9 +156,6 @@ int read_file(const char *full_path, int read_opt)
                printf("%s", line_ptr);
        }
 
-       if (line_ptr)
-               res = EXIT_SUCCESS;
-
        fclose(fp);
        return res;
 }

-- 
2.47.3

Reply via email to