jim         97/12/30 07:10:54

  Modified:    .        STATUS
               src      CHANGES
               src/ap   Makefile.tmpl
               src/main alloc.c http_config.c http_config.h http_core.c
                        http_main.c httpd.h util.c
               src/modules/proxy proxy_ftp.c proxy_util.c
  Added:       src/ap   ap_cpystrn.c
  Log:
  Submitted by: Jim Jagielski
  Reviewed by:  Dean Gaudet, Dirk-Willem van Gulik, Ken Coar
  Idea by:        Martin Kraemer
  The move from strncpy() to ap_cpystrn(), which is home-brewed and is
  located in ./src/ap
  
  Revision  Changes    Path
  1.40      +1 -4      apachen/STATUS
  
  Index: STATUS
  ===================================================================
  RCS file: /export/home/cvs/apachen/STATUS,v
  retrieving revision 1.39
  retrieving revision 1.40
  diff -u -r1.39 -r1.40
  --- STATUS    1997/12/29 15:09:47     1.39
  +++ STATUS    1997/12/30 15:10:39     1.40
  @@ -60,12 +60,9 @@
       * Ben Hyde's [PATCH] Serialize the update to pool.sub_* in destroy_pool
         (take 2)
       * Ken's [PATCH] for PR#1195 (" in realm names)
  +    * Jim's [PATCH] ap_cpystrn() function (replace strncpy) Take II
   
   Available Patches:
  -
  -    * Jim's [PATCH] ap_cpystrn() function (replace strncpy) Take II
  -     <[EMAIL PROTECTED]>
  -     Status: Jim +1, Dirk +1, Marc wants to think about the name, Dean +1
   
       * [PATCH] mod_digest/1599: proxy authentication using the digest auth
         scheme never succeeds (fwd)
  
  
  
  1.552     +4 -0      apachen/src/CHANGES
  
  Index: CHANGES
  ===================================================================
  RCS file: /export/home/cvs/apachen/src/CHANGES,v
  retrieving revision 1.551
  retrieving revision 1.552
  diff -u -r1.551 -r1.552
  --- CHANGES   1997/12/29 17:51:51     1.551
  +++ CHANGES   1997/12/30 15:10:41     1.552
  @@ -1,5 +1,9 @@
   Changes with Apache 1.3b4
   
  +  *) migration from strncpy() to our "enhanced" version called
  +     ap_cpystrn() for performance and functionality reasons.
  +     Located in libap.a.  [Jim Jagielski]
  +
     *) table_set() and table_unset() did not deal correctly with
        multiple occurrences of the same key. [Stephen Scheck
         <[EMAIL PROTECTED]>, Ben Laurie] PR#1604
  
  
  
  1.6       +2 -1      apachen/src/ap/Makefile.tmpl
  
  Index: Makefile.tmpl
  ===================================================================
  RCS file: /export/home/cvs/apachen/src/ap/Makefile.tmpl,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- Makefile.tmpl     1997/12/28 04:51:01     1.5
  +++ Makefile.tmpl     1997/12/30 15:10:42     1.6
  @@ -6,7 +6,7 @@
   
   LIB=libap.a
   
  -OBJS=ap_signal.o ap_slack.o ap_snprintf.o ap_strings.o
  +OBJS=ap_signal.o ap_slack.o ap_snprintf.o ap_strings.o ap_cpystrn.o
   
   .c.o:
        $(CC) -c $(INCLUDES) $(CFLAGS) $(SPACER) $<
  @@ -28,3 +28,4 @@
   ap_slack.o: $(INCDIR)/httpd.h $(INCDIR)/http_log.h
   ap_snprintf.o: $(INCDIR)/conf.h
   ap_strings.o: $(INCDIR)/httpd.h
  +ap_cpystrn.o: $(INCDIR)/httpd.h
  
  
  
  1.1                  apachen/src/ap/ap_cpystrn.c
  
  Index: ap_cpystrn.c
  ===================================================================
  /* ====================================================================
   * Copyright (c) 1995-1997 The Apache Group.  All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer. 
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. All advertising materials mentioning features or use of this
   *    software must display the following acknowledgment:
   *    "This product includes software developed by the Apache Group
   *    for use in the Apache HTTP server project (http://www.apache.org/)."
   *
   * 4. The names "Apache Server" and "Apache Group" must not be used to
   *    endorse or promote products derived from this software without
   *    prior written permission. For written permission, please contact
   *    [EMAIL PROTECTED]
   *
   * 5. Redistributions of any form whatsoever must retain the following
   *    acknowledgment:
   *    "This product includes software developed by the Apache Group
   *    for use in the Apache HTTP server project (http://www.apache.org/)."
   *
   * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
   * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
   * OF THE POSSIBILITY OF SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Group and was originally based
   * on public domain software written at the National Center for
   * Supercomputing Applications, University of Illinois, Urbana-Champaign.
   * For more information on the Apache Group and the Apache HTTP server
   * project, please see <http://www.apache.org/>.
   *
   */
  
  #include "httpd.h"
  
  /*
   * Apache's "replacement" for the strncpy() function. We roll our
   * own to implement these specific changes:
   *   (1) strncpy() doesn't always null terminate and we want it to.
   *   (2) strncpy() null fills, which is bogus, esp. when copy 8byte
   *       strings into 8k blocks.
   *   (3) Instead of returning the pointer to the beginning of
   *       the destination string, we return the end so we can
   *       "check" for truncation
   *
   * ap_cpystrn() follows the same call structure as strncpy().
   */
  
  char *ap_cpystrn(char *dst, const char *src, size_t dst_size)
  {
  
      char *d, *end;
  
      if (!dst_size)
          return (dst);
  
      d = dst;
      end = dst + dst_size - 1;
  
      while ((d < end) && (*d++ = *src++))
          ;     /* nop, the while does it all */
  
      *d = '\0';        /* always null terminate */
  
      return (d);
  }
  
  
  
  1.64      +1 -2      apachen/src/main/alloc.c
  
  Index: alloc.c
  ===================================================================
  RCS file: /export/home/cvs/apachen/src/main/alloc.c,v
  retrieving revision 1.63
  retrieving revision 1.64
  diff -u -r1.63 -r1.64
  --- alloc.c   1997/12/29 17:51:57     1.63
  +++ alloc.c   1997/12/30 15:10:44     1.64
  @@ -536,8 +536,7 @@
       if (s == NULL)
        return NULL;
       res = palloc(a, n + 1);
  -    strncpy(res, s, n);
  -    res[n] = '\0';
  +    ap_cpystrn(res, s, n);
       return res;
   }
   
  
  
  
  1.90      +1 -2      apachen/src/main/http_config.c
  
  Index: http_config.c
  ===================================================================
  RCS file: /export/home/cvs/apachen/src/main/http_config.c,v
  retrieving revision 1.89
  retrieving revision 1.90
  diff -u -r1.89 -r1.90
  --- http_config.c     1997/12/16 07:36:30     1.89
  +++ http_config.c     1997/12/30 15:10:45     1.90
  @@ -1134,8 +1134,7 @@
       /* Global virtual host hash bucket pointers.  Init to null. */
       init_vhost_config(p);
   
  -    strncpy(coredump_dir, server_root, sizeof(coredump_dir) - 1);
  -    coredump_dir[sizeof(coredump_dir) - 1] = '\0';
  +    ap_cpystrn(coredump_dir, server_root, sizeof(coredump_dir));
   }
   
   server_rec *init_server_config(pool *p)
  
  
  
  1.58      +1 -1      apachen/src/main/http_config.h
  
  Index: http_config.h
  ===================================================================
  RCS file: /export/home/cvs/apachen/src/main/http_config.h,v
  retrieving revision 1.57
  retrieving revision 1.58
  diff -u -r1.57 -r1.58
  --- http_config.h     1997/12/01 12:10:15     1.57
  +++ http_config.h     1997/12/30 15:10:45     1.58
  @@ -250,7 +250,7 @@
    * handle it back-compatibly, or at least signal an error).
    */
   
  -#define MODULE_MAGIC_NUMBER 19971026
  +#define MODULE_MAGIC_NUMBER 19971226
   #define STANDARD_MODULE_STUFF MODULE_MAGIC_NUMBER, -1, __FILE__, NULL
   
   /* Generic accessors for other modules to get at their own module-specific
  
  
  
  1.144     +2 -4      apachen/src/main/http_core.c
  
  Index: http_core.c
  ===================================================================
  RCS file: /export/home/cvs/apachen/src/main/http_core.c,v
  retrieving revision 1.143
  retrieving revision 1.144
  diff -u -r1.143 -r1.144
  --- http_core.c       1997/12/28 04:51:02     1.143
  +++ http_core.c       1997/12/30 15:10:46     1.144
  @@ -1233,8 +1233,7 @@
       if (err != NULL) return err;
   
       if (!is_directory (arg)) return "ServerRoot must be a valid directory";
  -    strncpy (server_root, arg, sizeof(server_root)-1);
  -    server_root[sizeof(server_root)-1] = '\0';
  +    ap_cpystrn (server_root, arg, sizeof(server_root));
       return NULL;
   }
   
  @@ -1571,8 +1570,7 @@
        return pstrcat(cmd->pool, "CoreDumpDirectory ", arg, 
            " does not exist or is not a directory", NULL);
       }
  -    strncpy(coredump_dir, arg, sizeof(coredump_dir)-1);
  -    coredump_dir[sizeof(coredump_dir)-1] = '\0';
  +    ap_cpystrn(coredump_dir, arg, sizeof(coredump_dir));
       return NULL;
   }
   
  
  
  
  1.261     +13 -28    apachen/src/main/http_main.c
  
  Index: http_main.c
  ===================================================================
  RCS file: /export/home/cvs/apachen/src/main/http_main.c,v
  retrieving revision 1.260
  retrieving revision 1.261
  diff -u -r1.260 -r1.261
  --- http_main.c       1997/12/26 18:16:17     1.260
  +++ http_main.c       1997/12/30 15:10:47     1.261
  @@ -1681,19 +1681,12 @@
        ss->conn_bytes = (unsigned long) 0;
       }
       if (r) {
  -     int slot_size;
        conn_rec *c = r->connection;
  -     slot_size = sizeof(ss->client) - 1;
  -     strncpy(ss->client, get_remote_host(c, r->per_dir_config,
  -                                         REMOTE_NOLOOKUP), slot_size);
  -     ss->client[slot_size] = '\0';
  -     slot_size = sizeof(ss->request) - 1;
  -     strncpy(ss->request, (r->the_request ? r->the_request :
  -                           "NULL"), slot_size);
  -     ss->request[slot_size] = '\0';
  -     slot_size = sizeof(ss->vhost) - 1;
  -     strncpy(ss->vhost, r->server->server_hostname, slot_size);
  -     ss->vhost[slot_size] = '\0';
  +     ap_cpystrn(ss->client, get_remote_host(c, r->per_dir_config,
  +                           REMOTE_NOLOOKUP), sizeof(ss->client));
  +     ap_cpystrn(ss->request, (r->the_request ? r->the_request :
  +                           "NULL"), sizeof(ss->request));
  +     ap_cpystrn(ss->vhost, r->server->server_hostname, sizeof(ss->vhost));
       }
   #endif
   
  @@ -3513,22 +3506,18 @@
       ptrans = make_sub_pool(pconf);
   
       server_argv0 = argv[0];
  -    strncpy(server_root, HTTPD_ROOT, sizeof(server_root) - 1);
  -    server_root[sizeof(server_root) - 1] = '\0';
  -    strncpy(server_confname, SERVER_CONFIG_FILE, sizeof(server_root) - 1);
  -    server_confname[sizeof(server_confname) - 1] = '\0';
  +    ap_cpystrn(server_root, HTTPD_ROOT, sizeof(server_root));
  +    ap_cpystrn(server_confname, SERVER_CONFIG_FILE, sizeof(server_confname));
   
       setup_prelinked_modules();
   
       while ((c = getopt(argc, argv, "Xd:f:vVhlZ:")) != -1) {
        switch (c) {
        case 'd':
  -         strncpy(server_root, optarg, sizeof(server_root) - 1);
  -         server_root[sizeof(server_root) - 1] = '\0';
  +         ap_cpystrn(server_root, optarg, sizeof(server_root));
            break;
        case 'f':
  -         strncpy(server_confname, optarg, sizeof(server_confname) - 1);
  -         server_confname[sizeof(server_confname) - 1] = '\0';
  +         ap_cpystrn(server_confname, optarg, sizeof(server_confname));
            break;
        case 'v':
            printf("Server version %s.\n", SERVER_VERSION);
  @@ -4315,10 +4304,8 @@
       ptrans = make_sub_pool(pconf);
   
       server_argv0 = argv[0];
  -    strncpy(server_root, HTTPD_ROOT, sizeof(server_root) - 1);
  -    server_root[sizeof(server_root) - 1] = '\0';
  -    strncpy(server_confname, SERVER_CONFIG_FILE, sizeof(server_root) - 1);
  -    server_confname[sizeof(server_confname) - 1] = '\0';
  +    ap_cpystrn(server_root, HTTPD_ROOT, sizeof(server_root));
  +    ap_cpystrn(server_confname, SERVER_CONFIG_FILE, sizeof(server_confname));
   
       setup_prelinked_modules();
   
  @@ -4344,12 +4331,10 @@
            break;
   #endif /* WIN32 */
        case 'd':
  -         strncpy(server_root, optarg, sizeof(server_root) - 1);
  -         server_root[sizeof(server_root) - 1] = '\0';
  +         ap_cpystrn(server_root, optarg, sizeof(server_root));
            break;
        case 'f':
  -         strncpy(server_confname, optarg, sizeof(server_confname) - 1);
  -         server_confname[sizeof(server_confname) - 1] = '\0';
  +         ap_cpystrn(server_confname, optarg, sizeof(server_confname));
            break;
        case 'v':
            printf("Server version %s.\n", SERVER_VERSION);
  
  
  
  1.170     +4 -0      apachen/src/main/httpd.h
  
  Index: httpd.h
  ===================================================================
  RCS file: /export/home/cvs/apachen/src/main/httpd.h,v
  retrieving revision 1.169
  retrieving revision 1.170
  diff -u -r1.169 -r1.170
  --- httpd.h   1997/12/28 11:52:02     1.169
  +++ httpd.h   1997/12/30 15:10:48     1.170
  @@ -925,3 +925,7 @@
   #else
   #define RAISE_SIGSTOP(x)
   #endif
  +
  +/* Our own home-brewed strncpy replacement */
  +API_EXPORT(char *) ap_cpystrn(char *dst, const char *src, size_t dst_size);
  +
  
  
  
  1.79      +8 -11     apachen/src/main/util.c
  
  Index: util.c
  ===================================================================
  RCS file: /export/home/cvs/apachen/src/main/util.c,v
  retrieving revision 1.78
  retrieving revision 1.79
  diff -u -r1.78 -r1.79
  --- util.c    1997/12/19 14:30:12     1.78
  +++ util.c    1997/12/30 15:10:49     1.79
  @@ -287,9 +287,10 @@
        }
        else if (no < nmatch && pmatch[no].rm_so < pmatch[no].rm_eo) {
            len = pmatch[no].rm_eo - pmatch[no].rm_so;
  -         strncpy(dst, source + pmatch[no].rm_so, len);
  +         ap_cpystrn(dst, source + pmatch[no].rm_so, len);
            dst += len;
  -         if (*(dst - 1) == '\0')     /* strncpy hit NULL. */
  +         /* is this still valid? jj 12/26/97 */
  +         if (*(dst - 1) == '\0')     /* ap_cpystrn hit NULL. */
                return NULL;
        }
   
  @@ -440,7 +441,7 @@
        if (s[x] == '/')
            if ((++f) == n) {
                res = palloc(p, x + 2);
  -             strncpy(res, s, x);
  +             ap_cpystrn(res, s, x);
                res[x] = '/';
                res[x + 1] = '\0';
                return res;
  @@ -499,8 +500,7 @@
       }
   
       res = palloc(atrans, pos + 1);
  -    strncpy(res, *line, pos);
  -    res[pos] = '\0';
  +    ap_cpystrn(res, *line, pos);
   
       while ((*line)[pos] == stop)
        ++pos;
  @@ -534,8 +534,7 @@
       }
   
       res = palloc(atrans, pos + 1);
  -    strncpy(res, *line, pos);
  -    res[pos] = '\0';
  +    ap_cpystrn(res, *line, pos);
   
       while (isspace((*line)[pos]))
        ++pos;
  @@ -562,8 +561,7 @@
       }
   
       res = palloc(atrans, pos + 1);
  -    strncpy(res, *line, pos);
  -    res[pos] = '\0';
  +    ap_cpystrn(res, *line, pos);
   
       ++pos;
   
  @@ -846,8 +844,7 @@
   
       tok_len = ptr - tok_start;
       token = palloc(p, tok_len + 1);
  -    strncpy(token, tok_start, tok_len);
  -    token[tok_len] = '\0';
  +    ap_cpystrn(token, tok_start, tok_len);
   
       /* Advance accept_line pointer to the next non-white byte */
   
  
  
  
  1.44      +2 -4      apachen/src/modules/proxy/proxy_ftp.c
  
  Index: proxy_ftp.c
  ===================================================================
  RCS file: /export/home/cvs/apachen/src/modules/proxy/proxy_ftp.c,v
  retrieving revision 1.43
  retrieving revision 1.44
  diff -u -r1.43 -r1.44
  --- proxy_ftp.c       1997/11/27 13:30:22     1.43
  +++ proxy_ftp.c       1997/12/30 15:10:52     1.44
  @@ -343,8 +343,7 @@
              link_ptr[n - 1] = '\0';
            ap_snprintf(urlptr, sizeof(urlptr), "%s%s%s", url+hostlen, 
(url[strlen(url) - 1] == '/' ? "" : "/"), filename);
            ap_snprintf(buf2, sizeof(buf2), "%s <A HREF=\"%s\">%s %s</A>\n", 
buf, filename, filename, link_ptr);
  -         strncpy(buf, buf2, sizeof(buf) - 1);
  -         buf[sizeof(buf) - 1] = '\0';
  +         ap_cpystrn(buf, buf2, sizeof(buf));
            n = strlen(buf);
        }
        else if (buf[0] == 'd' || buf[0] == '-' || buf[0] == 'l' || 
isdigit(buf[0])) {
  @@ -380,8 +379,7 @@
            else {
                ap_snprintf(buf2, sizeof(buf2), "%s <A HREF=\"%s\">%s</A>\n", 
buf, filename, filename);
            }
  -         strncpy(buf, buf2, sizeof(buf));
  -         buf[sizeof(buf) - 1] = '\0';
  +         ap_cpystrn(buf, buf2, sizeof(buf));
            n = strlen(buf);
        }
   
  
  
  
  1.38      +2 -2      apachen/src/modules/proxy/proxy_util.c
  
  Index: proxy_util.c
  ===================================================================
  RCS file: /export/home/cvs/apachen/src/modules/proxy/proxy_util.c,v
  retrieving revision 1.37
  retrieving revision 1.38
  diff -u -r1.37 -r1.38
  --- proxy_util.c      1997/11/27 13:46:30     1.37
  +++ proxy_util.c      1997/12/30 15:10:53     1.38
  @@ -639,7 +639,7 @@
       /* now split into directory levels */
   
       for (i = k = d = 0; d < ndepth; ++d) {
  -     strncpy(&val[i], &tmp[k], nlength);
  +     ap_cpystrn(&val[i], &tmp[k], nlength);
        k += nlength;
        val[i + nlength] = '/';
        i += nlength + 1;
  @@ -689,7 +689,7 @@
       /* now split into directory levels */
   
       for (i = k = d = 0; d < ndepth; ++d) {
  -     strncpy(&val[i], &tmp[k], nlength);
  +     ap_cpystrn(&val[i], &tmp[k], nlength);
        k += nlength;
        val[i + nlength] = '/';
        i += nlength + 1;
  
  
  

Reply via email to