curl_get_line: make sure lines end with newline

Verify with test 792 and 793

Reported-by: z2_
Closes #17697
This commit is contained in:
Daniel Stenberg 2025-06-21 22:35:41 +02:00
parent e29f11f2d6
commit 52f58ebb10
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
5 changed files with 123 additions and 11 deletions

View file

@ -32,6 +32,15 @@
/* The last #include file should be: */
#include "memdebug.h"
static int appendnl(struct dynbuf *buf)
{
CURLcode result = curlx_dyn_addn(buf, "\n", 1);
if(result)
/* too long line or out of memory */
return 0; /* error */
return 1; /* all good */
}
/*
* Curl_get_line() makes sure to only return complete whole lines that end
* newlines.
@ -43,9 +52,10 @@ int Curl_get_line(struct dynbuf *buf, FILE *input)
curlx_dyn_reset(buf);
while(1) {
char *b = fgets(buffer, sizeof(buffer), input);
size_t rlen;
if(b) {
size_t rlen = strlen(b);
rlen = strlen(b);
if(!rlen)
break;
@ -59,19 +69,24 @@ int Curl_get_line(struct dynbuf *buf, FILE *input)
/* end of the line */
return 1; /* all good */
else if(feof(input)) {
else if(feof(input))
/* append a newline */
result = curlx_dyn_addn(buf, "\n", 1);
if(result)
/* too long line or out of memory */
return 0; /* error */
return appendnl(buf);
}
else {
rlen = curlx_dyn_len(buf);
if(rlen) {
b = curlx_dyn_ptr(buf);
if(b[rlen-1] != '\n')
/* append a newline */
return appendnl(buf);
return 1; /* all good */
}
else
break;
}
else if(curlx_dyn_len(buf))
return 1; /* all good */
else
break;
}
return 0;
}