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
This commit is contained in:
Daniel Stenberg 2025-02-06 10:51:01 +01:00
parent 3814fb5a9f
commit d8618f4d84
No known key found for this signature in database
GPG Key ID: 5CC908FDB71E12C2
4 changed files with 50 additions and 28 deletions

View File

@ -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 \

View File

@ -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)
{
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'));
return TRUE; /* continue */
bool my_get_line(FILE *input, struct dynbuf *buf, bool *error)
{
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;
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;
}

View File

@ -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 */

View File

@ -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)