]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
Add INC_ALIGNED_PTR() macro to align pointers like malloc().
authorDave Hart <hart@ntp.org>
Sat, 19 Feb 2011 03:29:55 +0000 (03:29 +0000)
committerDave Hart <hart@ntp.org>
Sat, 19 Feb 2011 03:29:55 +0000 (03:29 +0000)
bk: 4d5f3933QwjljYZjuAIvIlUvvrZerg

ChangeLog
include/ntp_types.h
ntpd/ntp_crypto.c
ntpd/ntp_proto.c
ntpd/refclock_as2201.c
sntp/tests_main.h

index ac7d831aee712a784a0b66939f1f22929a292a68..3728ff209229e0306a7f889672fa5adeaf5c1cd8 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,4 @@
+* Add INC_ALIGNED_PTR() macro to align pointers like malloc().
 (4.2.7p130) 2011/02/12 Released by Harlan Stenn <stenn@ntp.org>
 * [Bug 1811] Update the download location in WHERE-TO-START.
 (4.2.7p129) 2011/02/09 Released by Harlan Stenn <stenn@ntp.org>
index 5d28bac668854083da981a52e48257d3cdfda1ca..6d35e18ff94f676acdd226946658216677688db9 100644 (file)
@@ -185,6 +185,32 @@ typedef u_int32 keyid_t;   /* cryptographic key ID */
 #define KEYID_T_MAX    (0xffffffff)
 typedef u_int32 tstamp_t;      /* NTP seconds timestamp */
 
+/*
+ * Cloning malloc()'s behavior of always returning pointers suitably
+ * aligned for the strictest alignment requirement of any type is not
+ * easy to do portably, as the maximum alignment required is not
+ * exposed.  Use the size of a union of the types known to represent the
+ * strictest alignment on some platform.
+ */
+typedef union max_alignment_tag {
+       double          d;
+} max_alignment;
+
+#define MAXALIGN               sizeof(max_alignment)
+#define ALIGN_UNITS(sz)                (((sz) + MAXALIGN - 1) / MAXALIGN)
+#define ALIGNED_SIZE(sz)       (MAXALIGN * ALIGN_UNITS(sz))
+#define INC_ALIGNED_PTR(b, m)  ((void *)aligned_ptr((void *)(b), m))
+
+static inline
+max_alignment *
+aligned_ptr(
+       max_alignment * base,
+       size_t          minsize
+       )
+{
+       return base + ALIGN_UNITS((minsize < 1) ? 1 : minsize);
+}
+
 /*
  * On Unix struct sock_timeval is equivalent to struct timeval.
  * On Windows built with 64-bit time_t, sock_timeval.tv_sec is a long
index 7ce51276fc193189c75771a7456713646d9c4470..80f63c8e66f8e7b412f8e4d90872682d8f17bbc5 100644 (file)
@@ -3781,11 +3781,12 @@ crypto_setup(void)
                    filename);
                exit (-1);
        }
-       hostval.ptr = strdup(cinfo->subject);
+       hostval.ptr = estrdup(cinfo->subject);
        hostval.vallen = htonl(strlen(cinfo->subject));
        sys_hostname = hostval.ptr;
-       if ((ptr = strchr(sys_hostname, (int)'@')) != NULL)
-               sys_groupname = strdup(++ptr);
+       ptr = (u_char *)strchr(sys_hostname, '@');
+       if (ptr != NULL)
+               sys_groupname = estrdup((char *)++ptr);
        if (ident_filename != NULL)
                strcpy(hostname, ident_filename);
 
@@ -3855,30 +3856,36 @@ crypto_config(
         * Set host name (host).
         */
        case CRYPTO_CONF_PRIV:
-               host_filename = strdup(cp);
+               if (NULL != host_filename)
+                       free(host_filename);
+               host_filename = estrdup(cp);
                break;
 
        /*
         * Set group name (ident).
         */
        case CRYPTO_CONF_IDENT:
-               ident_filename = strdup(cp);
+               if (NULL != ident_filename)
+                       free(ident_filename);
+               ident_filename = estrdup(cp);
                break;
 
        /*
         * Set private key password (pw).
         */
        case CRYPTO_CONF_PW:
-               passwd = emalloc(strlen(cp) + 1);
-               strcpy(passwd, cp);
+               if (NULL != passwd)
+                       free(passwd);
+               passwd = estrdup(cp);
                break;
 
        /*
         * Set random seed file name (randfile).
         */
        case CRYPTO_CONF_RAND:
-               rand_file = emalloc(strlen(cp) + 1);
-               strcpy(rand_file, cp);
+               if (NULL != rand_file)
+                       free(rand_file);
+               rand_file = estrdup(cp);
                break;
 
        /*
index d881106a110d525bedba32660025b4419576f5ee..77e1ef43347fc656f4a75b4dd6c3e8141059ed3f 100644 (file)
@@ -2387,17 +2387,17 @@ clock_select(void)
        for (peer = peer_list; peer != NULL; peer = peer->p_link)
                nlist++;
        endpoint_size = nlist * 2 * sizeof(struct endpoint);
-       synch_size = nlist * sizeof(double);
-       error_size = nlist * sizeof(double);
-       peers_size = nlist * sizeof(struct peer *);
-       indx_size = nlist * 2 * sizeof(int);
+       synch_size = ALIGNED_SIZE(nlist * sizeof(double));
+       error_size = ALIGNED_SIZE(nlist * sizeof(double));
+       peers_size = ALIGNED_SIZE(nlist * sizeof(struct peer *));
+       indx_size = ALIGNED_SIZE(nlist * 2 * sizeof(int));
        octets = endpoint_size + indx_size + peers_size + synch_size +
            error_size;
        endpoint = erealloc(endpoint, octets);
-       synch = (double *)((char *)endpoint + endpoint_size);
-       error = (double *)((char *)synch + synch_size);
-       peers = (struct peer **)((char *)error + error_size);
-       indx = (int *)((char *)peers + peers_size);
+       synch = INC_ALIGNED_PTR(endpoint, endpoint_size);
+       error = INC_ALIGNED_PTR(synch, synch_size);
+       peers = INC_ALIGNED_PTR(error, error_size);
+       indx = INC_ALIGNED_PTR(peers, peers_size);
 
        /*
         * Initially, we populate the island with all the rifraff peers
@@ -3499,7 +3499,7 @@ pool_xmit(
                        free(pool->addrs);
                        pool->addrs = NULL;
                }
-               memset(&hints, 0, sizeof(hints));
+               ZERO(hints);
                hints.ai_family = AF(&pool->srcadr);
                hints.ai_socktype = SOCK_DGRAM;
                hints.ai_protocol = IPPROTO_UDP;
@@ -3521,7 +3521,8 @@ pool_xmit(
        }
 
        do {
-               rmtadr = (sockaddr_u *)pool->ai->ai_addr;
+               /* copy_addrinfo_list ai_addr points to a sockaddr_u */
+               rmtadr = (sockaddr_u *)(void *)pool->ai->ai_addr;
                pool->ai = pool->ai->ai_next;
                p = findexistingpeer(rmtadr, NULL, NULL, MODE_CLIENT);
        } while (p != NULL && pool->ai != NULL);
index af59073919a85afec329c686fc7c60e5b36877ea..ef390a0e5b1d6312af7195a813f10a8ed1af41f6 100644 (file)
@@ -332,11 +332,11 @@ as2201_receive(
         */
        if ((int)(up->lastptr - up->stats + pp->lencode) > SMAX - 2)
            return;
-       (void)strcpy(up->lastptr, pp->a_lastcode);
+       memcpy(up->lastptr, pp->a_lastcode, pp->lencode);
        up->lastptr += pp->lencode;
        if (pp->sloppyclockflag & CLK_FLAG4) {
                octets = strlen(stat_command[up->index]);
-               if ((int)(up->lastptr - up->stats + octets) > SMAX - 2)
+               if ((int)(up->lastptr - up->stats + 1 + octets) > SMAX - 2)
                    return;
                *up->lastptr++ = ' ';
                memcpy(up->lastptr, stat_command[up->index], octets);
index 3da05241c6291fc148acf2ff7707df4cbb20a533..d280da80a7a657ad7b2a2cb8893823441bb699f8 100644 (file)
@@ -1,13 +1,14 @@
 #ifndef TESTS_MAIN_H
 #define TESTS_MAIN_H
 
-#include <gtest/gtest.h>
-
 #include "config.h"
 
 #include <string>
 #include <vector>
 
+#include <gtest/gtest.h>
+
+
 class ntptest : public ::testing::Test {
 public:
        static void SetExtraParams(int start, int count, char** argv);