commit-inetutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[SCM] GNU Inetutils branch, master, updated. inetutils-1_9_1-336-ga8ef9


From: Mats Erik Andersson
Subject: [SCM] GNU Inetutils branch, master, updated. inetutils-1_9_1-336-ga8ef961
Date: Mon, 09 Sep 2013 20:31:02 +0000

This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "GNU Inetutils ".

The branch, master has been updated
       via  a8ef961c8078d28dbbe0121b89a3deff2ff539bd (commit)
      from  0e8582e3043a015a1d770056b58f0a8d80ab139b (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
http://git.savannah.gnu.org/cgit/inetutils.git/commit/?id=a8ef961c8078d28dbbe0121b89a3deff2ff539bd


commit a8ef961c8078d28dbbe0121b89a3deff2ff539bd
Author: Mats Erik Andersson <address@hidden>
Date:   Thu Sep 5 15:28:24 2013 +0200

    ftp: Minor allocation issues.

diff --git a/ChangeLog b/ChangeLog
index 03c727c..a205b64 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,28 @@
 2013-09-05  Mats Erik Andersson  <address@hidden>
 
+       ftp: Minor allocation issues.
+       Fix issues related to the global variable `line'.
+
+       * ftp/cmds.c (another): New variable NEW.  Assign
+       outcome of realloc() to NEW, to better avoid memory
+       leakage.  Determine new size with `linelen'.
+       (confirm): Rename LINE to INPUT, which does not
+       shadow the global variable `line'.
+
+       * ftp/domacro.c: Include <stdlib.h>.
+       (domacro): Change LINE2 to a string pointer.
+       Assign LINE2 using strdup(), freeing it later.
+
+       * ftp/ftp.c (login) <execution of macro `init'>:
+       Allocate MAXLINE bytes; set `linelen'.
+
+       * ftp/ftp_var.h (linelen): New variable.
+
+       * ftp/main.c (main): Reset `line', `linelen'.
+       (cmdscanner): Remove LEN, using `linelen' instead.
+
+2013-09-05  Mats Erik Andersson  <address@hidden>
+
        ftp: Macro execution segfaults.
        Abort macro execution when command is ambiguous,
        is invalid, or if the control connection has been
diff --git a/ftp/cmds.c b/ftp/cmds.c
index 10b8d60..a89ecbe 100644
--- a/ftp/cmds.c
+++ b/ftp/cmds.c
@@ -150,7 +150,7 @@ int
 another (int *pargc, char ***pargv, const char *prompt)
 {
   char *arg = NULL;
-  char *buffer;
+  char *buffer, *new;
   size_t size = 0, len = strlen (line);
   int ret;
 
@@ -198,13 +198,17 @@ another (int *pargc, char ***pargv, const char *prompt)
       return 0;
     }
 
-  line = realloc (line, sizeof (char) * (len + strlen (arg) + 2));
-  if (!line)
+  new = realloc (line, sizeof (char) *
+                      ((linelen ? linelen : len) + strlen (arg) + 2));
+  if (!new)
     {
       free (arg);
       intr (0);
     }
 
+  line = new;
+  linelen = sizeof (char) *
+           ((linelen ? linelen : len) + strlen (arg) + 2);
   line[len++] = ' ';
   strcpy (&line[len], arg);
   free (arg);
@@ -1943,15 +1947,15 @@ disconnect (int argc _GL_UNUSED_PARAMETER, char **argv 
_GL_UNUSED_PARAMETER)
 int
 confirm (char *cmd, char *file)
 {
-  char line[BUFSIZ];
+  char input[BUFSIZ];
 
   if (!interactive)
     return (1);
   printf ("%s %s? ", cmd, file);
   fflush (stdout);
-  if (fgets (line, sizeof line, stdin) == NULL)
+  if (fgets (input, sizeof input, stdin) == NULL)
     return (0);
-  return (*line != 'n' && *line != 'N');
+  return (*input != 'n' && *input != 'N');
 }
 
 void
diff --git a/ftp/domacro.c b/ftp/domacro.c
index db7d9b3..3a01796 100644
--- a/ftp/domacro.c
+++ b/ftp/domacro.c
@@ -51,6 +51,7 @@
 
 #include <ctype.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 
 #include "ftp_var.h"
@@ -59,7 +60,8 @@ void
 domacro (int argc, char *argv[])
 {
   int i, j, count = 2, loopflg = 0;
-  char *cp1, *cp2, line2[200];
+  char *cp1, *cp2;
+  char *line2;         /* Allocated copy of `line'.  */
   struct cmd *c;
 
   if (argc < 2 && !another (&argc, &argv, "macro name"))
@@ -82,28 +84,42 @@ domacro (int argc, char *argv[])
       code = -1;
       return;
     }
-  strcpy (line2, line);
+
+  line2 = strdup (line);
+  if (!line2)
+    {
+      printf ("System refused resources for macro '%s'.\n", argv[1]);
+      code = -1;
+      return;
+    }
 
   do
     {
       cp1 = macros[i].mac_start;
       while (cp1 != macros[i].mac_end)
        {
+         /* Skip initial white space on each line of input.  */
          while (isspace (*cp1))
            {
              cp1++;
            }
+         /* Translate a line of text from macro definition
+          * and put it in `line'.  This global variable is
+          * referenced by some parsing functions, so the
+          * translation target cannot be changed easily.
+          */
          cp2 = line;
          while (*cp1 != '\0')
            {
              switch (*cp1)
                {
-               case '\\':
+               case '\\':              /* Escaping character.  */
                  *cp2++ = *++cp1;
                  break;
-               case '$':
+               case '$':               /* Substitution.  */
                  if (isdigit (*(cp1 + 1)))
                    {
+                     /* Argument expansion.  */
                      j = 0;
                      while (isdigit (*++cp1))
                        j = 10 * j + *cp1 - '0';
@@ -117,8 +133,9 @@ domacro (int argc, char *argv[])
                    }
                  if (*(cp1 + 1) == 'i')
                    {
+                     /* The loop counter "$i" was detected.  */
                      loopflg = 1;
-                     cp1++;
+                     cp1++;            /* Back to last used char.  */
                      if (count < argc)
                        {
                          strcpy (cp2, argv[count]);
@@ -126,11 +143,15 @@ domacro (int argc, char *argv[])
                        }
                      break;
                    }
-                 /* intentional drop through */
+                 /* Intentional fall through, since no acceptable
+                  * use of '$' was detected.  Present input is the
+                  * dollar sign.
+                  */
                default:
-                 *cp2++ = *cp1;
+                 *cp2++ = *cp1;        /* Copy present character.  */
                  break;
                }
+             /* Advance to next usable character.  */
              if (*cp1 != '\0')
                cp1++;
            }
@@ -168,8 +189,13 @@ domacro (int argc, char *argv[])
              (*c->c_handler) (margc, margv);
              if (bell && c->c_bell)
                putchar ('\007');
-             strcpy (line, line2);
-             makeargv ();
+
+             /* The arguments set at the time of invoking
+              * the macro must be recovered, to be used
+              * in parsing next line of macro definition.
+              */
+             strcpy (line, line2);     /* Known to fit.  */
+             makeargv ();              /* Get the arguments.  */
              argc = margc;
              argv = margv;
            }
@@ -178,4 +204,6 @@ domacro (int argc, char *argv[])
        }
     }
   while (loopflg && ++count < argc);
+
+  free (line2);
 }
diff --git a/ftp/ftp.c b/ftp/ftp.c
index 682bcbf..318a2f6 100644
--- a/ftp/ftp.c
+++ b/ftp/ftp.c
@@ -358,10 +358,12 @@ login (char *host)
       if (!strcmp ("init", macros[n].mac_name))
        {
          free (line);
-         line = calloc (200, sizeof (*line));
+         line = calloc (MAXLINE, sizeof (*line));
+         linelen = MAXLINE;
          if (!line)
            quit (0, 0);
 
+         /* Simulate input of the macro 'init'.  */
          strcpy (line, "$init");
          makeargv ();
          domacro (margc, margv);
diff --git a/ftp/ftp_var.h b/ftp/ftp_var.h
index 499ad36..29247d1 100644
--- a/ftp/ftp_var.h
+++ b/ftp/ftp_var.h
@@ -113,7 +113,8 @@ FTP_EXTERN int unix_proxy;  /* proxy is unix, can use 
binary for ascii */
 
 FTP_EXTERN jmp_buf toplevel;   /* non-local goto stuff for cmd scanner */
 
-FTP_EXTERN char *line;
+FTP_EXTERN char *line;         /* input produced by readline or getline */
+FTP_EXTERN size_t linelen;     /* allocated length of the same, if known */
 
 FTP_EXTERN char *stringbase;   /* current scan point in line buffer */
 FTP_EXTERN char argbuf[MAXLINE];       /* argument storage buffer */
diff --git a/ftp/main.c b/ftp/main.c
index 4231951..2b847b3 100644
--- a/ftp/main.c
+++ b/ftp/main.c
@@ -73,7 +73,9 @@
 # include <locale.h>
 #endif
 
-/* Define macro to nothing so declarations in ftp_var.h become definitions. */
+/* Define macro to nothing so declarations
+ * in "ftp_var.h" become definitions.
+ */
 #define FTP_EXTERN
 #include "ftp_var.h"
 
@@ -217,6 +219,9 @@ main (int argc, char *argv[])
   usefamily = AF_UNSPEC;       /* allow any address family */
   usereadline = 1;             /* normally using readline */
 
+  line = NULL;                 /* reset global input */
+  linelen = 0;
+
   /* Invoked as `pftp'?  Then set passive mode.  */
   cp = strrchr (argv[0], '/');
   if (cp)
@@ -359,7 +364,6 @@ cmdscanner (int top)
 {
   struct cmd *c;
   ssize_t l;
-  size_t len;
 
   if (!top)
     putchar ('\n');
@@ -369,12 +373,12 @@ cmdscanner (int top)
        {
          free (line);
          line = NULL;
-         len = 0;
        }
+      linelen = 0;
 
 #if HAVE_READLINE
       if (usereadline)
-       line = readline (prompt);
+       line = readline (prompt);       /* malloc'd, no NL */
       else
 #endif /* HAVE_READLINE */
        {
@@ -384,7 +388,8 @@ cmdscanner (int top)
              fflush (stdout);
            }
 
-         l = getline (&line, &len, stdin);
+         /* `linelen' is updated to allocated amount.  */
+         l = getline (&line, &linelen, stdin); /* includes NL */
          if ((l > 0) && line)
            {
              char *nl = strchr (line, '\n');
@@ -394,32 +399,34 @@ cmdscanner (int top)
            }
          else
            {
+             /* Allocation takes place even without input.  */
              free (line);      /* EOF, et cetera */
              line = NULL;
+             linelen = 0;
            }
 
          if (!fromatty && prompt)
            fprintf (stdout, "%s\n", line ? line : "");
-       }
+       } /* !usereadline ends */
 
       if (!line)
        quit (0, 0);
 
       l = strlen (line);
-      if (l >= MAXLINE)
+      if (l >= MAXLINE)                        /* XXX: Relax.  */
        {
          printf ("Line too long.\n");
          break;
        }
 
+      if (l == 0)
+       break;
+
 #if HAVE_READLINE
       if (usereadline && line && *line)
        add_history (line);
 #endif /* HAVE_READLINE */
 
-      if (l == 0)
-       break;
-
       makeargv ();
       if (margc == 0)
        continue;
@@ -440,7 +447,10 @@ cmdscanner (int top)
          printf ("Not connected.\n");
          continue;
        }
+
+      /* Perform the requested action.  */
       (*c->c_handler) (margc, margv);
+
       if (bell && c->c_bell)
        putchar ('\007');
       if (c->c_handler != help)
@@ -463,8 +473,8 @@ makeargv (void)
 
   margc = 0;
   argp = margv;
-  stringbase = line;           /* scan from first of buffer */
-  argbase = argbuf;            /* store from first of buffer */
+  stringbase = line;           /* scan from beginning of buffer */
+  argbase = argbuf;            /* store at beginning of buffer */
   slrflag = 0;
   while ((*argp++ = slurpstring ()))
     margc++;

-----------------------------------------------------------------------

Summary of changes:
 ChangeLog     |   23 +++++++++++++++++++++++
 ftp/cmds.c    |   16 ++++++++++------
 ftp/domacro.c |   46 +++++++++++++++++++++++++++++++++++++---------
 ftp/ftp.c     |    4 +++-
 ftp/ftp_var.h |    3 ++-
 ftp/main.c    |   34 ++++++++++++++++++++++------------
 6 files changed, 97 insertions(+), 29 deletions(-)


hooks/post-receive
-- 
GNU Inetutils 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]