From: Daniel Stenberg Date: Sat, 21 Jun 2025 20:35:41 +0000 (+0200) Subject: curl_get_line: make sure lines end with newline X-Git-Tag: curl-8_15_0~192 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=52f58ebb104033a5a06779efd60c608e94729d37;p=thirdparty%2Fcurl.git curl_get_line: make sure lines end with newline Verify with test 792 and 793 Reported-by: z2_ Closes #17697 --- diff --git a/lib/curl_get_line.c b/lib/curl_get_line.c index 2bb57492b3..4b1c6c3e09 100644 --- a/lib/curl_get_line.c +++ b/lib/curl_get_line.c @@ -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; } diff --git a/tests/data/Makefile.am b/tests/data/Makefile.am index 3986eca6f1..8e502220f1 100644 --- a/tests/data/Makefile.am +++ b/tests/data/Makefile.am @@ -110,7 +110,7 @@ test736 test737 test738 test739 test740 test741 test742 test743 test744 \ test745 test746 test747 test748 test749 test750 test751 test752 test753 \ test754 \ test780 test781 test782 test783 test784 test785 test786 test787 test788 \ -test789 test790 test791 \ +test789 test790 test791 test792 test793 \ \ test799 test800 test801 test802 test803 test804 test805 test806 test807 \ test808 test809 test810 test811 test812 test813 test814 test815 test816 \ diff --git a/tests/data/test744 b/tests/data/test744 index 4a22d51ec4..b35d97b048 100644 --- a/tests/data/test744 +++ b/tests/data/test744 @@ -3,6 +3,7 @@ HTTP --netrc-file +netrc diff --git a/tests/data/test792 b/tests/data/test792 new file mode 100644 index 0000000000..a7656e4781 --- /dev/null +++ b/tests/data/test792 @@ -0,0 +1,48 @@ + + + +netrc +FTP + + +# +# Server-side + + +content + + + +# +# Client-side + + +ftp + + +proxy + + +.netrc with embedded NULL byte + + +--netrc --netrc-file %LOGDIR/netrc%TESTNUMBER ftp://%HOSTIP:%FTPPORT/%TESTNUMBER + + +machine %HOSTIP login username password%hex[%00]hex% hello + + + + + +USER username +PASS +PWD +EPSV +TYPE I +SIZE %TESTNUMBER +RETR %TESTNUMBER +QUIT + + + diff --git a/tests/data/test793 b/tests/data/test793 new file mode 100644 index 0000000000..ad68a0f4e1 --- /dev/null +++ b/tests/data/test793 @@ -0,0 +1,48 @@ + + + +netrc +FTP + + +# +# Server-side + + +content + + + +# +# Client-side + + +ftp + + +proxy + + +.netrc with embedded NULL byte, with quoted token + + +--netrc --netrc-file %LOGDIR/netrc%TESTNUMBER ftp://%HOSTIP:%FTPPORT/%TESTNUMBER + + +machine %HOSTIP login username "password"%hex[%00]hex% hello + + + + + +USER username +PASS +PWD +EPSV +TYPE I +SIZE %TESTNUMBER +RETR %TESTNUMBER +QUIT + + +