From: Dave Hart Date: Sat, 19 Feb 2011 03:29:55 +0000 (+0000) Subject: Add INC_ALIGNED_PTR() macro to align pointers like malloc(). X-Git-Tag: NTP_4_2_7P134~3^2~1^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ce3a9430fe859ba5d49c6107cb8034b91b7d7d23;p=thirdparty%2Fntp.git Add INC_ALIGNED_PTR() macro to align pointers like malloc(). bk: 4d5f3933QwjljYZjuAIvIlUvvrZerg --- diff --git a/ChangeLog b/ChangeLog index ac7d831ae..3728ff209 100644 --- 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 * [Bug 1811] Update the download location in WHERE-TO-START. (4.2.7p129) 2011/02/09 Released by Harlan Stenn diff --git a/include/ntp_types.h b/include/ntp_types.h index 5d28bac66..6d35e18ff 100644 --- a/include/ntp_types.h +++ b/include/ntp_types.h @@ -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 diff --git a/ntpd/ntp_crypto.c b/ntpd/ntp_crypto.c index 7ce51276f..80f63c8e6 100644 --- a/ntpd/ntp_crypto.c +++ b/ntpd/ntp_crypto.c @@ -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; /* diff --git a/ntpd/ntp_proto.c b/ntpd/ntp_proto.c index d881106a1..77e1ef433 100644 --- a/ntpd/ntp_proto.c +++ b/ntpd/ntp_proto.c @@ -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); diff --git a/ntpd/refclock_as2201.c b/ntpd/refclock_as2201.c index af5907391..ef390a0e5 100644 --- a/ntpd/refclock_as2201.c +++ b/ntpd/refclock_as2201.c @@ -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); diff --git a/sntp/tests_main.h b/sntp/tests_main.h index 3da05241c..d280da80a 100644 --- a/sntp/tests_main.h +++ b/sntp/tests_main.h @@ -1,13 +1,14 @@ #ifndef TESTS_MAIN_H #define TESTS_MAIN_H -#include - #include "config.h" #include #include +#include + + class ntptest : public ::testing::Test { public: static void SetExtraParams(int start, int count, char** argv);