From: Daniel Stenberg Date: Thu, 6 Feb 2025 09:51:01 +0000 (+0100) Subject: tool_ssls: switch to tool-specific get_line function X-Git-Tag: curl-8_12_1~74 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d8618f4d;p=thirdparty%2Fcurl.git tool_ssls: switch to tool-specific get_line function Instead of picky-backing on the libcurl one using the curlx shortcut, which is fragile since the libcurl one is not present in all builds. Reported-by: mschroeder-fzj on github Fixes #16201 Closes #16203 --- diff --git a/src/Makefile.inc b/src/Makefile.inc index 1290387a01..404e1aae39 100644 --- a/src/Makefile.inc +++ b/src/Makefile.inc @@ -32,14 +32,12 @@ # libcurl sources to include in curltool lib we use for test binaries CURLTOOL_LIBCURL_CFILES = \ ../lib/base64.c \ - ../lib/dynbuf.c \ - ../lib/curl_get_line.c + ../lib/dynbuf.c # libcurl has sources that provide functions named curlx_* that are not part of # the official API, but we reuse the code here to avoid duplication. CURLX_CFILES = \ ../lib/base64.c \ - ../lib/curl_get_line.c \ ../lib/curl_multibyte.c \ ../lib/dynbuf.c \ ../lib/nonblock.c \ @@ -50,7 +48,6 @@ CURLX_CFILES = \ CURLX_HFILES = \ ../lib/curl_ctype.h \ - ../lib/curl_get_line.h \ ../lib/curl_multibyte.h \ ../lib/curl_setup.h \ ../lib/dynbuf.h \ diff --git a/src/tool_parsecfg.c b/src/tool_parsecfg.c index d79e869f02..651ec8e9f4 100644 --- a/src/tool_parsecfg.c +++ b/src/tool_parsecfg.c @@ -43,8 +43,6 @@ static const char *unslashquote(const char *line, char *param); #define MAX_CONFIG_LINE_LENGTH (10*1024*1024) -static bool my_get_line(FILE *fp, struct curlx_dynbuf *, bool *error); - /* return 0 on everything-is-fine, and non-zero otherwise */ int parseconfig(const char *filename, struct GlobalConfig *global) @@ -241,8 +239,6 @@ int parseconfig(const char *filename, struct GlobalConfig *global) if(alloced_param) Curl_safefree(param); - - curlx_dyn_reset(&buf); } curlx_dyn_free(&buf); if(file != stdin) @@ -301,22 +297,45 @@ static const char *unslashquote(const char *line, char *param) /* * Reads a line from the given file, ensuring is NUL terminated. */ -static bool my_get_line(FILE *fp, struct curlx_dynbuf *db, - bool *error) + +bool my_get_line(FILE *input, struct dynbuf *buf, bool *error) { - char buf[4096]; - *error = FALSE; - do { - /* fgets() returns s on success, and NULL on error or when end of file - occurs while no characters have been read. */ - if(!fgets(buf, sizeof(buf), fp)) - /* only if there is data in the line, return TRUE */ - return curlx_dyn_len(db); - if(curlx_dyn_add(db, buf)) { - *error = TRUE; /* error */ - return FALSE; /* stop reading */ - } - } while(!strchr(buf, '\n')); + CURLcode result; + char buffer[128]; + curlx_dyn_reset(buf); + while(1) { + char *b = fgets(buffer, sizeof(buffer), input); + + if(b) { + size_t rlen = strlen(b); + + if(!rlen) + break; - return TRUE; /* continue */ + result = curlx_dyn_addn(buf, b, rlen); + if(result) { + /* too long line or out of memory */ + *error = TRUE; + return FALSE; /* error */ + } + + else if(b[rlen-1] == '\n') + /* end of the line */ + return TRUE; /* all good */ + + else if(feof(input)) { + /* append a newline */ + result = curlx_dyn_addn(buf, "\n", 1); + if(result) { + /* too long line or out of memory */ + *error = TRUE; + return FALSE; /* error */ + } + return TRUE; /* all good */ + } + } + else + break; + } + return FALSE; } diff --git a/src/tool_parsecfg.h b/src/tool_parsecfg.h index 75f9127ee7..b8a18ef151 100644 --- a/src/tool_parsecfg.h +++ b/src/tool_parsecfg.h @@ -27,4 +27,6 @@ int parseconfig(const char *filename, struct GlobalConfig *config); +bool my_get_line(FILE *fp, struct curlx_dynbuf *db, bool *error); + #endif /* HEADER_CURL_TOOL_PARSECFG_H */ diff --git a/src/tool_ssls.c b/src/tool_ssls.c index 55e1b56447..b7525c0e74 100644 --- a/src/tool_ssls.c +++ b/src/tool_ssls.c @@ -31,7 +31,7 @@ #include "tool_ssls.h" #include "dynbuf.h" #include "curl_base64.h" -#include "curl_get_line.h" +#include "tool_parsecfg.h" /* The maximum line length for an ecoded session ticket */ #define MAX_SSLS_LINE (64 * 1024) @@ -68,6 +68,7 @@ CURLcode tool_ssls_load(struct GlobalConfig *global, size_t shmac_len, sdata_len; CURLcode r = CURLE_OK; int i, imported; + bool error = FALSE; curlx_dyn_init(&buf, MAX_SSLS_LINE); fp = fopen(filename, FOPEN_READTEXT); @@ -81,7 +82,7 @@ CURLcode tool_ssls_load(struct GlobalConfig *global, goto out; i = imported = 0; - while(Curl_get_line(&buf, fp)) { + while(my_get_line(fp, &buf, &error)) { ++i; curl_free(shmac); curl_free(sdata); @@ -123,7 +124,10 @@ CURLcode tool_ssls_load(struct GlobalConfig *global, } ++imported; } - r = CURLE_OK; + if(error) + r = CURLE_FAILED_INIT; + else + r = CURLE_OK; out: if(easy)