Skip to content

Commit 25c1f5c

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 6f208e9 commit 25c1f5c

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
@@ -129,19 +129,28 @@ sgetsgent(const char *string)
129129
/*@observer@*//*@null@*/struct sgrp *
130130
fgetsgent(/*@null@*/FILE *fp)
131131
{
132-
static size_t buflen = 0;
133-
static char *buf = NULL;
132+
char *buf;
133+
size_t buflen;
134+
struct sgrp *sg;
134135

135136
if (NULL == fp) {
136137
return NULL;
137138
}
138139

140+
buf = NULL;
141+
buflen = 0;
139142
if (getline(&buf, &buflen, fp) == -1)
140-
return NULL;
143+
goto fail;
141144
if (stpsep(buf, "\n") == NULL)
142-
return NULL;
145+
goto fail;
146+
147+
sg = sgetsgent(buf);
143148

144-
return (sgetsgent (buf));
149+
free(buf);
150+
return sg;
151+
fail:
152+
free(buf);
153+
return NULL;
145154
}
146155

147156

0 commit comments

Comments
 (0)