]> Pileus Git - ~andy/fetchmail/commitdiff
Call strlen() only once when removing CRLF from a line
authorSunil Shetye <sunilshetye@rocketmail.com>
Sun, 1 May 2011 19:05:00 +0000 (00:35 +0530)
committerMatthias Andree <matthias.andree@gmx.de>
Tue, 3 May 2011 09:37:18 +0000 (11:37 +0200)
NEWS
smtp.c
transact.c

diff --git a/NEWS b/NEWS
index 221bfcfbacc91a25c83f1915f781af1519ebf951..b226dfd5305174034b575a8fe2fe4005d22fd3b2 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -65,6 +65,9 @@ fetchmail-6.3.20 (not yet released):
   variants are too diverse, and we've been bitten before -- and configure
   complains noisily on Cyrus-SASL's RFC1321 md5.h.
 
+# BUG FIXES
+* Call strlen() only once when removing CRLF from a line. (Sunil Shetye)
+
 # TRANSLATION UPDATES
   [ja]    Japanese (Takeshi Hamasaki)
 
diff --git a/smtp.c b/smtp.c
index 611bad075dff7986ffe98a23d43768e005e6fed2..1c99c69675917cbbda7c2e1f6ebf9e91faf8946c 100644 (file)
--- a/smtp.c
+++ b/smtp.c
@@ -185,15 +185,16 @@ int SMTP_ehlo(int sock, char smtp_mode, const char *host, char *name, char *pass
   *opt = 0;
   while ((SockRead(sock, smtp_response, sizeof(smtp_response)-1)) != -1)
   {
-      int  n = strlen(smtp_response);
+      size_t n;
 
       set_timeout(0);
       (void)set_signal_handler(SIGALRM, alrmsave);
 
-      if (smtp_response[strlen(smtp_response)-1] == '\n')
-         smtp_response[strlen(smtp_response)-1] = '\0';
-      if (smtp_response[strlen(smtp_response)-1] == '\r')
-         smtp_response[strlen(smtp_response)-1] = '\0';
+      n = strlen(smtp_response);
+      if (n > 0 && smtp_response[n-1] == '\n')
+         smtp_response[--n] = '\0';
+      if (n > 0 && smtp_response[n-1] == '\r')
+         smtp_response[--n] = '\0';
       if (n < 4)
          return SM_ERROR;
       smtp_response[n] = '\0';
@@ -329,10 +330,9 @@ int SMTP_ok(int sock, char smtp_mode, int mintimeout)
 
        n = strlen(reply);
        if (n > 0 && reply[n-1] == '\n')
-           n--;
+           reply[--n] = '\0';
        if (n > 0 && reply[n-1] == '\r')
-           n--;
-       reply[n] = '\0';
+           reply[--n] = '\0';
 
        /* stomp over control characters */
        for (i = reply; *i; i++)
index 55d251684179f0ea1dbbb381ad91760c2efd754b..758e5aaf58ada4cfb58d877770ca3a66d36c5c56 100644 (file)
@@ -1554,6 +1554,7 @@ int gen_recv(int sock  /** socket to which server is connected */,
             char *buf /* buffer to receive input */,
             int size  /* length of buffer */)
 {
+    size_t n;
     int oldphase = phase;      /* we don't have to be re-entrant */
 
     phase = SERVER_WAIT;
@@ -1573,10 +1574,11 @@ int gen_recv(int sock  /** socket to which server is connected */,
     else
     {
        set_timeout(0);
-       if (buf[strlen(buf)-1] == '\n')
-           buf[strlen(buf)-1] = '\0';
-       if (buf[strlen(buf)-1] == '\r')
-           buf[strlen(buf)-1] = '\0';
+       n = strlen(buf);
+       if (n > 0 && buf[n-1] == '\n')
+           buf[--n] = '\0';
+       if (n > 0 && buf[n-1] == '\r')
+           buf[--n] = '\0';
        if (outlevel >= O_MONITOR)
            report(stdout, "%s< %s\n", protocol->name, buf);
        phase = oldphase;