--- ../../spamdyke-3.1.5/spamdyke/spamdyke.c	2008-01-22 09:41:00.000000000 -0600
+++ spamdyke.c	2008-02-07 00:22:12.000000000 -0600
@@ -1497,7 +1497,9 @@
 
   strlen_buf_input = 0;
   usable_buf_input = 0;
+  buf_input[0] = '\0';
   start_buf_input = NULL;
+  buf_childinput[0] = '\0';
   start_buf_childinput = NULL;
   strlen_buf_childinput = 0;
   filter_return = FILTER_FLAG_PASS;
@@ -1885,6 +1887,7 @@
                 strlen_buf_childinput = 0;
                 child_response_needed = 0;
                 line_terminator = '\0';
+                buf_childinput[strlen_buf_childinput] = '\0';
                 }
               else
                 {
@@ -1953,6 +1956,8 @@
           start_buf_childinput = NULL;
           strlen_buf_childinput = 0;
           }
+
+        buf_childinput[strlen_buf_childinput] = '\0';
         }
 
       next_terminator = NULL;
@@ -1966,6 +1971,7 @@
         if ((read_result = NETWORK_READ(current_settings, STDIN_FD, buf_input + strlen_buf_input, MAX_BUF - strlen_buf_input)) > 0)
           {
           strlen_buf_input += read_result;
+          buf_input[strlen_buf_input] = '\0';
           usable_buf_input = 1;
 
           if (current_settings->tls_state != TLS_STATE_ACTIVE_PASSTHROUGH)
@@ -1973,7 +1979,6 @@
             while ((strlen_buf_input > 0) &&
                    (buf_input[strlen_buf_input - 1] == '\0'))
               strlen_buf_input--;
-            buf_input[strlen_buf_input] = '\0';
 
             if ((strlen_buf_input > 0) &&
                 (start_buf_input == NULL))
@@ -1989,10 +1994,17 @@
           {
           if (*outbound_fd >= 0)
             {
-            if (strlen_buf_input > 0)
+            if ((start_buf_input == NULL) &&
+                (strlen_buf_input > 0))
+              start_buf_input = buf_input;
+
+            if ((start_buf_input != NULL) &&
+                ((strlen_buf_input - (start_buf_input - buf_input)) > 0))
               {
-              output_writeln(current_settings, LOG_ACTION_REMOTE_FROM, *outbound_fd, buf_input, strlen_buf_input);
+              output_writeln(current_settings, LOG_ACTION_REMOTE_FROM, *outbound_fd, start_buf_input, strlen_buf_input - (start_buf_input - buf_input));
               strlen_buf_input = 0;
+              buf_input[strlen_buf_input] = '\0';
+              start_buf_input = NULL;
               }
 
             close(*outbound_fd);
@@ -2014,7 +2026,7 @@
             start_buf_input = buf_input;
 
           if (((next_terminator = strchr(start_buf_input, line_terminator)) != NULL) ||
-              ((strlen_buf_input - (start_buf_input - buf_input)) == MAX_BUF))
+              (strlen_buf_input == MAX_BUF))
             {
             if (next_terminator != NULL)
               {
@@ -2028,7 +2040,7 @@
               }
             else
               {
-              next_terminator = start_buf_input + strlen_buf_input;
+              next_terminator = buf_input + strlen_buf_input;
               strlen_buf_trim = strlen_buf_input;
               }
 
@@ -2096,7 +2108,6 @@
             /* Buffer is not full and no terminator found, move data */
             strlen_buf_input -= start_buf_input - buf_input;
             memmove(buf_input, start_buf_input, strlen_buf_input);
-            buf_input[strlen_buf_input] = '\0';
             start_buf_input = NULL;
             }
           }
@@ -2106,6 +2117,8 @@
           start_buf_input = NULL;
           strlen_buf_input = 0;
           }
+
+        buf_input[strlen_buf_input] = '\0';
         }
 
       if (strlen_buf_input == 0)
