X-Git-Url: https://git.infodrom.org/?p=infodrom%2Fcgilib;a=blobdiff_plain;f=aux.c;h=dfb0f96f20f324e12d836a19e790b7b8311d55ca;hp=a808b9d0ea8b720c1a674a4ed6fe3125e2cbfafd;hb=d4402aecd61143979b686107f561dd28a5a67d09;hpb=75db2910edc7279c482ed77f3a9033de858220d9 diff --git a/aux.c b/aux.c index a808b9d..dfb0f96 100644 --- a/aux.c +++ b/aux.c @@ -117,6 +117,7 @@ char *cgiGetLine (FILE *stream) { static char *line = NULL; static size_t size = 0; + int len; char buf[BUFSIZE]; char *cp; @@ -129,25 +130,35 @@ char *cgiGetLine (FILE *stream) while (!feof (stream)) { if ((cp = fgets (buf, sizeof (buf), stream)) == NULL) + { + free(line); + line = NULL; return NULL; + } if (strlen(line)+strlen(buf)+1 > size) { if ((cp = (char *)realloc (line, size + BUFSIZE)) == NULL) return line; + cp[size] = '\0'; size += BUFSIZE; line = cp; } strcat (line, buf); - if (line[strlen(line)-1] == '\n') { - line[strlen(line)-1] = '\0'; - if (line[strlen(line)-1] == '\r') - line[strlen(line)-1] = '\0'; + len = strlen(line); + if (len && line[len-1] == '\n') { + line[len-1] = '\0'; + len --; + if (len && line[len-1] == '\r') + line[len-1] = '\0'; cgiDebugOutput (4, "Read line '%s'", line); return line; } } + free(line); + line = NULL; + return NULL; }