]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
tool_ssls: switch to tool-specific get_line function
authorDaniel Stenberg <daniel@haxx.se>
Thu, 6 Feb 2025 09:51:01 +0000 (10:51 +0100)
committerDaniel Stenberg <daniel@haxx.se>
Thu, 6 Feb 2025 21:25:35 +0000 (22:25 +0100)
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

src/Makefile.inc
src/tool_parsecfg.c
src/tool_parsecfg.h
src/tool_ssls.c

index 1290387a014f01bb0384dae69d247e181c5c782a..404e1aae3990c6cd1d7c63fac3c294dfaddd7214 100644 (file)
 # 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 \
index d79e869f023b85acc65634ae8d5595ad9a553d66..651ec8e9f401f4f93bb4315e72534650160b68f2 100644 (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)
+
+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;
 }
index 75f9127ee750c416fe6e3140a9949bfe527c5db4..b8a18ef1512804467746073dfe5a49f1b1f5e1be 100644 (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 */
index 55e1b56447e613badebca77f4e72a577eaa52e46..b7525c0e74ba75d830a414dda72b8bb258c37183 100644 (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)