]> git.ipfire.org Git - thirdparty/libbsd.git/commitdiff
Lock the file streams in fgetln() and fparseln()
authorGuillem Jover <guillem@hadrons.org>
Wed, 23 Sep 2015 05:10:18 +0000 (07:10 +0200)
committerGuillem Jover <guillem@hadrons.org>
Wed, 23 Sep 2015 05:59:34 +0000 (07:59 +0200)
The fparseln() function had the NetBSD uppercase macros stubbed out,
so replace them with the actual stdio ones. The fgetln() function was
missing any locking at all.

src/fgetln.c
src/fparseln.c

index 5f646e4e5485c9d45db02878b8a9cc13d218b3fb..4d1726e8ce3e6fe73052f691124e2038c20197b1 100644 (file)
@@ -50,6 +50,8 @@ fgetln(FILE *stream, size_t *len)
        struct filebuf *fb;
        ssize_t nread;
 
+       flockfile(stream);
+
        /* Try to diminish the possibility of several fgetln() calls being
         * used on different streams, by using a pool of buffers per file. */
        fb = &fb_pool[fb_pool_cur];
@@ -61,6 +63,9 @@ fgetln(FILE *stream, size_t *len)
        fb->fp = stream;
 
        nread = getline(&fb->buf, &fb->len, stream);
+
+       funlockfile(stream);
+
        /* Note: the getdelim/getline API ensures nread != 0. */
        if (nread == -1) {
                *len = 0;
index 959df11b3a5ac054ea14eebb39e6f556c75237d0..effb849281002230b7b6859ac6ff7e3e1e179a0e 100644 (file)
@@ -35,8 +35,6 @@ __RCSID("$NetBSD: fparseln.c,v 1.10 2009/10/21 01:07:45 snj Exp $");
 #include <string.h>
 #include <stdlib.h>
 
-#define FLOCKFILE(fp)
-#define FUNLOCKFILE(fp)
 #define _DIAGASSERT(t)
 
 static int isescaped(const char *, const char *, int);
@@ -104,7 +102,7 @@ fparseln(FILE *fp, size_t *size, size_t *lineno, const char str[3], int flags)
         */
        nl  = '\n';
 
-       FLOCKFILE(fp);
+       flockfile(fp);
 
        while (cnt) {
                cnt = 0;
@@ -151,7 +149,7 @@ fparseln(FILE *fp, size_t *size, size_t *lineno, const char str[3], int flags)
                }
 
                if ((cp = realloc(buf, len + s + 1)) == NULL) {
-                       FUNLOCKFILE(fp);
+                       funlockfile(fp);
                        free(buf);
                        free(ptr);
                        return NULL;
@@ -163,7 +161,7 @@ fparseln(FILE *fp, size_t *size, size_t *lineno, const char str[3], int flags)
                buf[len] = '\0';
        }
 
-       FUNLOCKFILE(fp);
+       funlockfile(fp);
        free(ptr);
 
        if ((flags & FPARSELN_UNESCALL) != 0 && esc && buf != NULL &&