Skip to content

Commit 9d98c22

Browse files
lib/gshadow.c: fgetsgent(): Don't use static variables
BTW, getline(3) says we must free(3) the buffer on error. Reported-by: Chris Hofstaedtler <[email protected]> Signed-off-by: Alejandro Colomar <[email protected]>
1 parent 4f8cb8c commit 9d98c22

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

lib/gshadow.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,28 @@ sgetsgent(const char *s)
123123
/*@observer@*//*@null@*/struct sgrp *
124124
fgetsgent(/*@null@*/FILE *fp)
125125
{
126-
static size_t buflen = 0;
127-
static char *buf = NULL;
126+
char *buf;
127+
size_t buflen;
128+
struct sgrp *sg;
128129

129130
if (NULL == fp) {
130131
return NULL;
131132
}
132133

134+
buf = NULL;
135+
buflen = 0;
133136
if (getline(&buf, &buflen, fp) == -1)
134-
return NULL;
137+
goto fail;
135138
if (stpsep(buf, "\n") == NULL)
136-
return NULL;
139+
goto fail;
140+
141+
sg = sgetsgent(buf);
137142

138-
return (sgetsgent (buf));
143+
free(buf);
144+
return sg;
145+
fail:
146+
free(buf);
147+
return NULL;
139148
}
140149

141150

0 commit comments

Comments
 (0)