+* 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>
#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
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);
* 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;
/*
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
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;
}
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);
*/
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);
#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);