From d9bf72de0c074bfe84e4d2b191f86f2e39a0abaf Mon Sep 17 00:00:00 2001
From: Per Cederqvist <ceder@lysator.liu.se>
Date: Thu, 13 Jun 1991 03:41:20 +0000
Subject: [PATCH] Fixed included files. Fixed possible memory leak in
 prot_a_parse_string. Needs more work.

---
 src/server/prot-a-parse.c | 45 ++++++++++++++++++++++++---------------
 1 file changed, 28 insertions(+), 17 deletions(-)

diff --git a/src/server/prot-a-parse.c b/src/server/prot-a-parse.c
index 34a0f4506..49161402e 100644
--- a/src/server/prot-a-parse.c
+++ b/src/server/prot-a-parse.c
@@ -7,8 +7,8 @@
 #include <setjmp.h>
 #include <string.h>
 
-#include <kom-types.h>
 #include "s-string.h"
+#include <kom-types.h>
 #include "lyskomd.h"
 #include "com.h"
 #include "connections.h"
@@ -18,6 +18,7 @@
 #include "minmax.h"
 #include "prot-a.h"
 #include "config.h"
+#include "log.h"
 
 long
 prot_a_parse_long(Connection *client)
@@ -83,27 +84,30 @@ prot_a_parse_conf_type(Connection *client,
 /*
  * Parse a string. At most 'maxlen' characters are allowed. If the
  * client sends a longer string only the first 'maxlen+1' characters
- * are read. The following characters are discarded.
+ * are read. Any remaining characters are discarded.
+ */
+/*
+ * +++ This needs cleaning up. See comments in and above mux_parse_string.
  */
 void
 prot_a_parse_string(Connection  *client,
-		    String	    *result,
-		    int		     maxlen)
+		    String	*result,
+		    int		 maxlen)
 {
     String_size hptr;		/* Pointer to 'H' */
     String_size client_len;	/* The len the client is sending. */
     String_size truncated_len;	/* How much the server will receive. */
     String_size to_skip;
-    String tmp;
+    static u_long err_cnt = 0;
 
     switch ( client->string_parse_pos )
     {
     case 0:
 	/* Get number and discard trailing 'H' */
-	result->len = s_strtol(s_fsubstr(client->unparsed,
-					 client->first_to_parse,
-					 END_OF_STRING),
-			       &hptr, PROTOCOL_NUMBER_BASE);
+	client_len = s_strtol(s_fsubstr(client->unparsed,
+					client->first_to_parse,
+					END_OF_STRING),
+			      &hptr, PROTOCOL_NUMBER_BASE);
 
 	if ( hptr == -1
 	    || client->first_to_parse + hptr
@@ -130,8 +134,7 @@ prot_a_parse_string(Connection  *client,
 	/* Check that the entire string is transmitted. */
 	/* (Don't care about the trailing part that will be skipped if the
 	 *  string is longer than maxlen) */
-	truncated_len = min(maxlen + 1, result->len);
-	client_len = result->len;
+	truncated_len = min(maxlen + 1, client_len);
 	
 	if ( client->first_to_parse + truncated_len
 	    > s_strlen(client->unparsed) )
@@ -139,12 +142,20 @@ prot_a_parse_string(Connection  *client,
 	    longjmp(parse_env, ISC_MSG_INCOMPLETE);
 	}
 
-	result->string = client->unparsed.string + client->first_to_parse;
-	result->len = truncated_len;
-	tmp = EMPTY_STRING;
-	s_strcpy(&tmp, *result); /* Copy the string. */
-	*result = tmp;
-	result->len = client_len;
+	if ( (result->len != 0 || result->string != NULL) && err_cnt++ < 20 )
+	{
+	    log ("prot_a_parse_string(): result->len == %lu, "
+		 "result->string == %lu. This memory will not be free()'d.\n",
+		 (u_long)result->len, (u_long)result->string);
+	    *result = EMPTY_STRING;
+	    if ( err_cnt == 20 )
+		log("Won't log the above warning no more.");
+	}
+
+	s_mem_crea_str(result,
+		       client->unparsed.string + client->first_to_parse,
+		       truncated_len);
+	result->len = client_len; /* Ugly! +++ */
 
 	client->first_to_parse += truncated_len;
 	client->string_parse_pos = 2;
-- 
GitLab