Skip to content

Commit af556bc

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 f04a652 commit af556bc

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
@@ -104,19 +104,28 @@ sgetsgent(const char *s)
104104
/*@observer@*//*@null@*/struct sgrp *
105105
fgetsgent(/*@null@*/FILE *fp)
106106
{
107-
static size_t buflen = 0;
108-
static char *buf = NULL;
107+
char *buf;
108+
size_t buflen;
109+
struct sgrp *sg;
109110

110111
if (NULL == fp) {
111112
return NULL;
112113
}
113114

115+
buf = NULL;
116+
buflen = 0;
114117
if (getline(&buf, &buflen, fp) == -1)
115-
return NULL;
118+
goto fail;
116119
if (stpsep(buf, "\n") == NULL)
117-
return NULL;
120+
goto fail;
121+
122+
sg = sgetsgent(buf);
118123

119-
return (sgetsgent (buf));
124+
free(buf);
125+
return sg;
126+
fail:
127+
free(buf);
128+
return NULL;
120129
}
121130

122131

0 commit comments

Comments
 (0)