Lock the file streams in fgetln() and fparseln() 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
diff --git a/src/fgetln.c b/src/fgetln.c
index 5f646e4..4d1726e 100644
--- a/src/fgetln.c
+++ b/src/fgetln.c
@@ -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;
diff --git a/src/fparseln.c b/src/fparseln.c
index 959df11..effb849 100644
--- a/src/fparseln.c
+++ b/src/fparseln.c
@@ -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 &&