]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
tool_paramhlp: remove a malloc+free from proto2num()
authorDaniel Stenberg <daniel@haxx.se>
Tue, 30 Dec 2025 23:13:24 +0000 (00:13 +0100)
committerDaniel Stenberg <daniel@haxx.se>
Wed, 31 Dec 2025 16:59:30 +0000 (17:59 +0100)
Closes #20120

src/tool_paramhlp.c

index 43315d5f732f0bb3a4978f3bd2dcd8887e790157..3f8213835a1505fa676004ec9316f1163eba0298 100644 (file)
@@ -396,21 +396,23 @@ static void protoset_clear(const char **protoset, const char *proto)
  * data.
  */
 
-#define MAX_PROTOSTRING (64 * 11)  /* Room for 64 10-chars proto names. */
+#define MAX_PROTOS 34
+#define MAX_PROTOSTRING (MAX_PROTOS * 11)  /* Room for MAX_PROTOS number of
+                                              10-chars proto names. */
 
 ParameterError proto2num(const char * const *val, char **ostr, const char *str)
 {
-  const char **protoset;
   struct dynbuf obuf;
   size_t proto;
   CURLcode result = CURLE_OK;
+  const char *protoset[MAX_PROTOS + 1];
 
-  curlx_dyn_init(&obuf, MAX_PROTOSTRING);
-
-  protoset = curlx_malloc((proto_count + 1) * sizeof(*protoset));
-  if(!protoset)
+  DEBUGASSERT(proto_count <= MAX_PROTOS);
+  if(proto_count > MAX_PROTOS) /* if case of surprises */
     return PARAM_NO_MEM;
 
+  curlx_dyn_init(&obuf, MAX_PROTOSTRING);
+
   /* Preset protocol set with default values. */
   protoset[0] = NULL;
   for(; *val; val++) {
@@ -505,7 +507,6 @@ ParameterError proto2num(const char * const *val, char **ostr, const char *str)
   for(proto = 0; protoset[proto] && !result; proto++)
     result = curlx_dyn_addf(&obuf, "%s%s", curlx_dyn_len(&obuf) ? "," : "",
                             protoset[proto]);
-  curlx_free((char *)protoset);
   if(result)
     return PARAM_NO_MEM;
   if(!curlx_dyn_len(&obuf)) {