bug-wget
[Top][All Lists]
Advanced

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

[Bug-wget] [PATCH 04/14] Drop usage of strncpy


From: gscrivano
Subject: [Bug-wget] [PATCH 04/14] Drop usage of strncpy
Date: Wed, 11 Jun 2014 17:05:15 +0200

From: Giuseppe Scrivano <address@hidden>

---
 src/ChangeLog   | 15 +++++++++++++++
 src/css-url.c   | 25 +++----------------------
 src/ftp-basic.c | 10 +++++-----
 src/ftp.c       | 14 +++++++++-----
 src/http.c      |  8 +++++---
 src/init.c      |  9 ++-------
 src/mswindows.c |  7 +++----
 src/retr.c      | 20 ++++++++++----------
 src/vms.c       |  5 ++---
 9 files changed, 54 insertions(+), 59 deletions(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index 1cdcff5..3a883aa 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,5 +1,20 @@
 2014-06-10  Giuseppe Scrivano  <address@hidden>
 
+       * retr.c (getproxy): Return a dinamically allocated string and...
+       (retrieve_from_file, retrieve_url, url_uses_proxy): ...fix the caller
+       to handle it.
+       * init.c (home_dir): Replace strncpy with strdup.
+       * mswindows.c (struct fake_fork_info): Make lfilename a pointer.
+       (fake_fork_child): Replace strncpy with strdup.
+       * http.c (ensure_extension): Replace strncpy with memcpy, not much
+       better but make "make syntax-check" happy and we know the size.
+       * ftp.c (getftp): Add parameter last_expected_bytes.
+       (ftp_loop_internal): Pass parameter last_expected_bytes to getftp.
+       * ftp-basic.c: Remove declaration of ftp_last_respline.
+       (ftp_response): Do not set ftp_last_respline.
+       * css-url.c (get_uri_string): Replace strncpy with strdup.
+       * vms.c (set_vms_name): Replace strncpy with strdup.
+
        * exits.c: Move WGET_EXIT_* definitions to...
        * exits.h: ...here.  Add WGET_EXIT_GENERIC_ERROR, WGET_EXIT_PARSE_ERROR.
        Remove WGET_EXIT_MINIMUM.
diff --git a/src/css-url.c b/src/css-url.c
index e8c4989..da78673 100644
--- a/src/css-url.c
+++ b/src/css-url.c
@@ -1,5 +1,5 @@
 /* Collect URLs from CSS source.
-   Copyright (C) 1998, 2000, 2001, 2002, 2003, 2009, 2010, 2011 Free
+   Copyright (C) 1998, 2000, 2001, 2002, 2003, 2009, 2010, 2011, 2014 Free
    Software Foundation, Inc.
 
 This file is part of GNU Wget.
@@ -76,12 +76,6 @@ extern int yylex (void);
 static char *
 get_uri_string (const char *at, int *pos, int *length)
 {
-  char *uri;
-  /*char buf[1024];
-  strncpy(buf,at + *pos, *length);
-  buf[*length] = '\0';
-  DEBUGP (("get_uri_string: \"%s\"\n", buf));*/
-
   if (0 != strncasecmp (at + *pos, "url(", 4))
     return NULL;
 
@@ -107,14 +101,7 @@ get_uri_string (const char *at, int *pos, int *length)
       *length -= 2;
     }
 
-  uri = xmalloc (*length + 1);
-  if (uri)
-    {
-      strncpy (uri, at + *pos, *length);
-      uri[*length] = '\0';
-    }
-
-  return uri;
+  return xstrdup (at + *pos);
 }
 
 void
@@ -126,12 +113,6 @@ get_urls_css (struct map_context *ctx, int offset, int 
buf_length)
   int pos, length;
   char *uri;
 
-  /*
-  strncpy(tmp,ctx->text + offset, buf_length);
-  tmp[buf_length] = '\0';
-  DEBUGP (("get_urls_css: \"%s\"\n", tmp));
-  */
-
   /* tell flex to scan from this buffer */
   yy_scan_bytes (ctx->text + offset, buf_length);
 
@@ -165,7 +146,7 @@ get_urls_css (struct map_context *ctx, int offset, int 
buf_length)
                   pos++;
                   length -= 2;
                   uri = xmalloc (length + 1);
-                  strncpy (uri, yytext + 1, length);
+                  memcpy (uri, yytext + 1, length);
                   uri[length] = '\0';
                 }
 
diff --git a/src/ftp-basic.c b/src/ftp-basic.c
index 7a512c6..b6e67e2 100644
--- a/src/ftp-basic.c
+++ b/src/ftp-basic.c
@@ -1,6 +1,6 @@
 /* Basic FTP routines.
    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
-   2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation,
+   2005, 2006, 2007, 2008, 2009, 2010, 2011, 2014 Free Software Foundation,
    Inc.
 
 This file is part of GNU Wget.
@@ -44,8 +44,6 @@ as that of the covered work.  */
 #include "ftp.h"
 #include "retr.h"
 
-char ftp_last_respline[128];
-
 
 /* Get the response of FTP server and allocate enough room to handle
    it.  <CR> and <LF> characters are stripped from the line, and the
@@ -84,8 +82,6 @@ ftp_response (int fd, char **ret_line)
       if (c_isdigit (line[0]) && c_isdigit (line[1]) && c_isdigit (line[2])
           && line[3] == ' ')
         {
-          strncpy (ftp_last_respline, line, sizeof (ftp_last_respline));
-          ftp_last_respline[sizeof (ftp_last_respline) - 1] = '\0';
           *ret_line = line;
           return FTPOK;
         }
@@ -1037,6 +1033,7 @@ ftp_syst (int csock, enum stype *server_type, enum ustype 
*unix_type)
   char *request, *respline;
   int nwritten;
   uerr_t err;
+  char *ftp_last_respline;
 
   /* Send SYST request.  */
   request = ftp_request ("SYST", NULL);
@@ -1058,6 +1055,8 @@ ftp_syst (int csock, enum stype *server_type, enum ustype 
*unix_type)
       return FTPSRVERR;
     }
 
+  ftp_last_respline = strdup (respline);
+
   /* Skip the number (215, but 200 (!!!) in case of VMS) */
   strtok (respline, " ");
 
@@ -1092,6 +1091,7 @@ ftp_syst (int csock, enum stype *server_type, enum ustype 
*unix_type)
   else
     *server_type = ST_OTHER;
 
+  xfree (ftp_last_respline);
   xfree (respline);
   /* All OK.  */
   return FTPOK;
diff --git a/src/ftp.c b/src/ftp.c
index e63d98d..5f4ef03 100644
--- a/src/ftp.c
+++ b/src/ftp.c
@@ -1,6 +1,6 @@
 /* File Transfer Protocol support.
    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
-   2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation,
+   2005, 2006, 2007, 2008, 2009, 2010, 2011, 2014 Free Software Foundation,
    Inc.
 
 This file is part of GNU Wget.
@@ -243,7 +243,8 @@ static uerr_t ftp_get_listing (struct url *, ccon *, struct 
fileinfo **);
    is non-NULL, the downloaded data will be written there as well.  */
 static uerr_t
 getftp (struct url *u, wgint passed_expected_bytes, wgint *qtyread,
-        wgint restval, ccon *con, int count, FILE *warc_tmp)
+        wgint restval, ccon *con, int count, wgint *last_expected_bytes,
+        FILE *warc_tmp)
 {
   int csock, dtsock, local_sock, res;
   uerr_t err = RETROK;          /* appease the compiler */
@@ -1078,7 +1079,7 @@ Error in server response, closing control 
connection.\n"));
         logputs (LOG_VERBOSE, _("done.\n"));
 
       if (! got_expected_bytes)
-        expected_bytes = ftp_expected_bytes (ftp_last_respline);
+        expected_bytes = *last_expected_bytes;
     } /* do retrieve */
 
   if (cmd & DO_LIST)
@@ -1127,7 +1128,7 @@ Error in server response, closing control 
connection.\n"));
         logputs (LOG_VERBOSE, _("done.\n"));
 
       if (! got_expected_bytes)
-        expected_bytes = ftp_expected_bytes (ftp_last_respline);
+        expected_bytes = *last_expected_bytes;
     } /* cmd & DO_LIST */
 
   if (!(cmd & (DO_LIST | DO_RETR)) || (opt.spider && !(cmd & DO_LIST)))
@@ -1345,6 +1346,7 @@ Error in server response, closing control 
connection.\n"));
 
   /* Get the server to tell us if everything is retrieved.  */
   err = ftp_response (csock, &respline);
+  *last_expected_bytes = ftp_expected_bytes (respline);
   if (err != FTPOK)
     {
       /* The control connection is decidedly closed.  Print the time
@@ -1547,6 +1549,7 @@ ftp_loop_internal (struct url *u, struct fileinfo *f, 
ccon *con, char **local_fi
   bool warc_enabled = (opt.warc_filename != NULL);
   FILE *warc_tmp = NULL;
   ip_address *warc_ip = NULL;
+  wgint last_expected_bytes = 0;
 
   /* Get the target, and set the name for the message accordingly. */
   if ((f == NULL) && (con->target))
@@ -1673,7 +1676,8 @@ ftp_loop_internal (struct url *u, struct fileinfo *f, 
ccon *con, char **local_fi
 
       /* If we are working on a WARC record, getftp should also write
          to the warc_tmp file. */
-      err = getftp (u, len, &qtyread, restval, con, count, warc_tmp);
+      err = getftp (u, len, &qtyread, restval, con, count, 
&last_expected_bytes,
+                    warc_tmp);
 
       if (con->csock == -1)
         con->st &= ~DONE_CWD;
diff --git a/src/http.c b/src/http.c
index 9a28b37..f119e25 100644
--- a/src/http.c
+++ b/src/http.c
@@ -1,6 +1,6 @@
 /* HTTP support.
    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
-   2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation,
+   2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2014 Free Software 
Foundation,
    Inc.
 
 This file is part of GNU Wget.
@@ -4033,10 +4033,12 @@ ensure_extension (struct http_stat *hs, const char 
*ext, int *dt)
 {
   char *last_period_in_local_filename = strrchr (hs->local_file, '.');
   char shortext[8];
-  int len = strlen (ext);
+  int len;
+  shortext[0] = '\0';
+  len = strlen (ext);
   if (len == 5)
     {
-      strncpy (shortext, ext, len - 1);
+      memcpy (shortext, ext, len - 1);
       shortext[len - 1] = '\0';
     }
 
diff --git a/src/init.c b/src/init.c
index 4c7bc8e..05b0c8f 100644
--- a/src/init.c
+++ b/src/init.c
@@ -1,6 +1,6 @@
 /* Reading/parsing the initialization file.
    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
-   2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation,
+   2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2014 Free Software 
Foundation,
    Inc.
 
 This file is part of GNU Wget.
@@ -445,12 +445,7 @@ home_dir (void)
           assert (p);
 
           len = p - buff + 1;
-          buff = malloc (len + 1);
-          if (buff == NULL)
-            return NULL;
-
-          strncpy (buff, _w32_get_argv0 (), len);
-          buff[len] = '\0';
+          buff = strdup (_w32_get_argv0 ());
 
           home = buf;
 #elif !defined(WINDOWS)
diff --git a/src/mswindows.c b/src/mswindows.c
index c0d9be6..d479268 100644
--- a/src/mswindows.c
+++ b/src/mswindows.c
@@ -1,6 +1,6 @@
 /* mswindows.c -- Windows-specific support
    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
-   2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation,
+   2005, 2006, 2007, 2008, 2009, 2010, 2011, 2014 Free Software Foundation,
    Inc.
 
 This file is part of GNU Wget.
@@ -122,7 +122,7 @@ struct fake_fork_info
 {
   HANDLE event;
   bool logfile_changed;
-  char lfilename[MAX_PATH + 1];
+  char *lfilename;
 };
 
 /* Determines if we are the child and if so performs the child logic.
@@ -164,8 +164,7 @@ fake_fork_child (void)
       if (new_log_fp)
         {
           info->logfile_changed = true;
-          strncpy (info->lfilename, opt.lfilename, sizeof (info->lfilename));
-          info->lfilename[sizeof (info->lfilename) - 1] = '\0';
+          info->lfilename = strdup (opt.lfilename);
           fclose (new_log_fp);
         }
     }
diff --git a/src/retr.c b/src/retr.c
index ff125a6..461b17a 100644
--- a/src/retr.c
+++ b/src/retr.c
@@ -1,6 +1,6 @@
 /* File retrieval.
    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
-   2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation,
+   2005, 2006, 2007, 2008, 2009, 2010, 2011, 2014 Free Software Foundation,
    Inc.
 
 This file is part of GNU Wget.
@@ -782,6 +782,7 @@ retrieve_url (struct url * orig_parsed, const char 
*origurl, char **file,
           result = PROXERR;
           goto bail;
         }
+      free (proxy);
     }
 
   if (u->scheme == SCHEME_HTTP
@@ -1065,8 +1066,9 @@ retrieve_from_file (const char *file, bool html, int 
*count)
 
       parsed_url = url_parse (cur_url->url->url, NULL, tmpiri, true);
 
+      char *proxy = getproxy (cur_url->url);
       if ((opt.recursive || opt.page_requisites)
-          && (cur_url->url->scheme != SCHEME_FTP || getproxy (cur_url->url)))
+          && (cur_url->url->scheme != SCHEME_FTP || proxy))
         {
           int old_follow_ftp = opt.follow_ftp;
 
@@ -1084,6 +1086,7 @@ retrieve_from_file (const char *file, bool html, int 
*count)
                                cur_url->url->url, &filename,
                                &new_file, NULL, &dt, opt.recursive, tmpiri,
                                true);
+      free(proxy);
 
       if (parsed_url)
           url_free (parsed_url);
@@ -1236,7 +1239,6 @@ getproxy (struct url *u)
 {
   char *proxy = NULL;
   char *rewritten_url;
-  static char rewritten_storage[1024];
 
   if (!opt.use_proxy)
     return NULL;
@@ -1266,13 +1268,9 @@ getproxy (struct url *u)
      getproxy() to return static storage. */
   rewritten_url = rewrite_shorthand_url (proxy);
   if (rewritten_url)
-    {
-      strncpy (rewritten_storage, rewritten_url, sizeof (rewritten_storage));
-      rewritten_storage[sizeof (rewritten_storage) - 1] = '\0';
-      proxy = rewritten_storage;
-    }
+    return rewritten_url;
 
-  return proxy;
+  return strdup(proxy);
 }
 
 /* Returns true if URL would be downloaded through a proxy. */
@@ -1283,7 +1281,9 @@ url_uses_proxy (struct url * u)
   bool ret;
   if (!u)
     return false;
-  ret = getproxy (u) != NULL;
+  char *proxy = getproxy (u);
+  ret = proxy != NULL;
+  free(proxy);
   return ret;
 }
 
diff --git a/src/vms.c b/src/vms.c
index 5183c59..dd02f0b 100644
--- a/src/vms.c
+++ b/src/vms.c
@@ -841,12 +841,11 @@ else
 
 /* Action routine for decc$to_vms(), in utime(). */
 
-char vms_path[ NAMX$C_MAXRSS+ 1];
+char *vms_path;
 
 int set_vms_name( char *name, int type)
 {
-   strncpy( vms_path, name, NAMX$C_MAXRSS);
-   vms_path[ NAMX$C_MAXRSS] = '\0';
+   vms_path = strdup(name);
    return 1;
 }
 
-- 
1.9.3




reply via email to

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