From: Dave Hart Date: Sat, 8 Jan 2011 08:45:17 +0000 (+0000) Subject: Fix for openssl pkg-config detection eval failure. X-Git-Tag: NTP_4_2_7P114~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=16ca6f5390a0c1e956940441fb13de4228c46a61;p=thirdparty%2Fntp.git Fix for openssl pkg-config detection eval failure. (applies to prior cset) Add erealloc_zero(), refactor estrdup(), emalloc(), emalloc_zero() to separate tracking callsite file/line from using debug MS C runtime, and to reduce code duplication. bk: 4d28241dGFY8JNoFYK7Gc4dYOh2-Aw --- diff --git a/ChangeLog b/ChangeLog index 2946d1d78..1794466b9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +* Fix for openssl pkg-config detection eval failure. +* Add erealloc_zero(), refactor estrdup(), emalloc(), emalloc_zero() to + separate tracking callsite file/line from using debug MS C runtime, + and to reduce code duplication. (4.2.7p113) 2011/01/07 Released by Harlan Stenn * [Bug 1776] sntp mishandles -t/--timeout and -a/--authentication. * Default to silent make rules, override with make V=1 or ./configure diff --git a/configure.ac b/configure.ac index 903ddf1b5..e1667f59a 100644 --- a/configure.ac +++ b/configure.ac @@ -5662,49 +5662,7 @@ AC_MSG_RESULT([$ntp_ok]) dnl gtest is needed for our tests subdirs. It would be nice if we could dnl require a C++ compiler only if we will use gtest, but AC_PROG_CXX dnl can't be conditionalized. - -gta=false -AC_ARG_WITH( - [gtest], - [AS_HELP_STRING( - [--with-gtest], - [Use the gtest framework if it's available] - )], - [try_gtest=$withval], - [try_gtest=yes] -) -case "$try_gtest" in - yes) - AC_PATH_PROG([GTEST_CONFIG], [gtest-config]) - case x${GTEST_CONFIG} in - x) ;; - *) - AC_MSG_CHECKING([gtest version]) - gtest_version=`$GTEST_CONFIG --version` - case "$gtest_version" in - 1.5.*) - GTEST_LDFLAGS=`$GTEST_CONFIG --ldflags` - GTEST_LIBS=`$GTEST_CONFIG --libs` - GTEST_CXXFLAGS=`$GTEST_CONFIG --cxxflags` - GTEST_CPPFLAGS=`$GTEST_CONFIG --cppflags` - AC_SUBST([GTEST_LDFLAGS]) - AC_SUBST([GTEST_LIBS]) - AC_SUBST([GTEST_CXXFLAGS]) - AC_SUBST([GTEST_CPPFLAGS]) - gta=true - ;; - esac - case "$gta" in - true) - AC_MSG_RESULT([($gtest_version) ok]) - ;; - *) AC_MSG_RESULT([($gtest_version) not ok]) - ;; - esac - esac -esac -AM_CONDITIONAL([GTEST_AVAILABLE], [$gta]) - +NTP_GOOGLETEST ### diff --git a/include/lib_strbuf.h b/include/lib_strbuf.h index aa4b7f968..6242cc958 100644 --- a/include/lib_strbuf.h +++ b/include/lib_strbuf.h @@ -22,7 +22,7 @@ extern int lib_inited; do { \ if (!lib_inited) \ init_lib(); \ + ZERO(lib_stringbuf[lib_nextbuf]); \ (bufp) = &lib_stringbuf[lib_nextbuf++][0]; \ lib_nextbuf %= COUNTOF(lib_stringbuf); \ - memset((bufp), '\0', sizeof(lib_stringbuf[0])); \ } while (FALSE) diff --git a/include/ntp_control.h b/include/ntp_control.h index 50f20c863..3d2e1a669 100644 --- a/include/ntp_control.h +++ b/include/ntp_control.h @@ -161,7 +161,7 @@ struct ctl_trap { u_char tr_flags; /* trap flags */ u_char tr_version; /* version number of trapper */ }; -extern struct ctl_trap ctl_trap[]; +extern struct ctl_trap ctl_traps[CTL_MAXTRAPS]; /* * Flag bits diff --git a/include/ntp_malloc.h b/include/ntp_malloc.h index 9203c53f0..4cde62e1a 100644 --- a/include/ntp_malloc.h +++ b/include/ntp_malloc.h @@ -41,4 +41,21 @@ void * alloca(size_t); #endif +#ifdef EREALLOC_IMPL +# define EREALLOC_CALLSITE /* preserve __FILE__ and __LINE__ */ +#else +# define EREALLOC_IMPL(ptr, newsz, filenm, loc) \ + realloc(ptr, (newsz)) +#endif + +#ifdef HAVE_STRINGS_H +# include +# define zero_mem(p, s) bzero(p, s) +#endif + +#ifndef zero_mem +# define zero_mem(p, s) memset(p, 0, s) +#endif +#define ZERO(var) zero_mem(&(var), sizeof(var)) + #endif /* NTP_MALLOC_H */ diff --git a/include/ntp_net.h b/include/ntp_net.h index d417cea4d..df207eb6c 100644 --- a/include/ntp_net.h +++ b/include/ntp_net.h @@ -23,6 +23,7 @@ #endif #include "ntp_rfc2553.h" +#include "ntp_malloc.h" typedef union { struct sockaddr sa; @@ -123,7 +124,7 @@ typedef union { : sizeof((psau)->sa6)) #define ZERO_SOCK(psau) \ - memset((psau), 0, sizeof(*(psau))) + ZERO(*(psau)) /* blast a byte value across sockaddr_u v6 address */ #define MEMSET_ADDR6(psau, v) \ diff --git a/include/ntp_rfc2553.h b/include/ntp_rfc2553.h index 632418327..337658720 100644 --- a/include/ntp_rfc2553.h +++ b/include/ntp_rfc2553.h @@ -72,15 +72,19 @@ #include #include "ntp_types.h" +#include "ntp_malloc.h" - -#if !defined(_MSC_VER) || !defined(_DEBUG) -struct addrinfo *copy_addrinfo_list(const struct addrinfo *); +struct addrinfo *copy_addrinfo_list_impl(const struct addrinfo * +#ifdef EREALLOC_CALLSITE /* from ntp_malloc.h */ + , + const char *, int +#endif + ); +#ifdef EREALLOC_CALLSITE +# define copy_addrinfo_list(l) \ + copy_addrinfo_list_impl((l), __FILE__, __LINE__) #else -#define copy_addrinfo_list(ai) \ - debug_copy_addrinfo_list((ai), __FILE__, __LINE__) -struct addrinfo *debug_copy_addrinfo_list(const struct addrinfo *, - const char *, int); +# define copy_addrinfo_list(l) copy_addrinfo_list_impl(l) #endif /* diff --git a/include/ntp_stdlib.h b/include/ntp_stdlib.h index ccc670c28..7b23dff3e 100644 --- a/include/ntp_stdlib.h +++ b/include/ntp_stdlib.h @@ -10,7 +10,7 @@ #endif #include "l_stdlib.h" -#include "ntp_types.h" +#include "ntp_malloc.h" #include "ntp_string.h" #include "ntp_net.h" #include "ntp_syslog.h" @@ -83,19 +83,6 @@ extern u_int32 caldaystart (u_int32 ntptime, const time_t *pivot); extern const char *clockname (int); extern int clocktime (int, int, int, int, int, u_int32, u_long *, u_int32 *); -#if !defined(_MSC_VER) || !defined(_DEBUG) -extern void * emalloc (size_t); -extern void * erealloc (void *, size_t); -extern void * emalloc_zero (size_t); -extern char * estrdup (const char *); -#else -extern void * debug_ereallocz (void *, size_t, int, const char *, int); -#define emalloc(c) debug_ereallocz(NULL, (c), 0, __FILE__, __LINE__) -#define erealloc(p, c) debug_ereallocz((p), (c), 0, __FILE__, __LINE__) -#define emalloc_zero(c) debug_ereallocz(NULL, (c), 1, __FILE__, __LINE__) -extern char * debug_estrdup (const char *, const char *, int); -#define estrdup(s) debug_estrdup((s), __FILE__, __LINE__) -#endif extern int ntp_getopt (int, char **, const char *); extern void init_auth (void); extern void init_lib (void); @@ -109,6 +96,32 @@ extern int MD5authencrypt (int, u_char *, u_int32 *, int); extern void MD5auth_setkey (keyid_t, int, const u_char *, const int); extern u_int32 addr2refid (sockaddr_u *); +/* emalloc.c */ +#ifndef EREALLOC_CALLSITE /* ntp_malloc.h defines */ +extern void * ereallocz (void *, size_t, size_t, int); +#define erealloczsite(p, n, o, z, f, l) ereallocz(p, n, o, (z)) +extern void * emalloc (size_t); +#define emalloc_zero(c) ereallocz(NULL, (c), 0, TRUE) +#define erealloc(p, c) ereallocz(p, (c), 0, FALSE) +#define erealloc_zero(p, n, o) ereallocz(p, n, (o), TRUE) +extern char * estrdup_impl (const char *); +#define estrdup(s) estrdup_impl(s) +#else +extern void * ereallocz (void *, size_t, size_t, int, + const char *, int); +#define erealloczsite ereallocz +#define emalloc(c) ereallocz(NULL, (c), 0, FALSE, \ + __FILE__, __LINE__) +#define emalloc_zero(c) ereallocz(NULL, (c), 0, TRUE, \ + __FILE__, __LINE__) +#define erealloc(p, c) ereallocz(p, (c), 0, FALSE, \ + __FILE__, __LINE__) +#define erealloc_zero(p, n, o) ereallocz(p, n, (o), TRUE, \ + __FILE__, __LINE__) +extern char * estrdup_impl (const char *, const char *, int); +#define estrdup(s) estrdup_impl((s), __FILE__, __LINE__) +#endif + extern int atoint (const char *, long *); extern int atouint (const char *, u_long *); diff --git a/libntp/authkeys.c b/libntp/authkeys.c index b9b76d9e6..94bc87fcf 100644 --- a/libntp/authkeys.c +++ b/libntp/authkeys.c @@ -79,7 +79,7 @@ init_auth(void) /* * Initialize hash table and free list */ - memset((char *)key_hash, 0, sizeof key_hash); + ZERO(key_hash); } @@ -429,7 +429,7 @@ auth_delkeys(void) */ if (sk->flags & KEY_TRUSTED) { skp = &(sk->next); - memset(&sk->k, 0, sizeof(sk->k)); + ZERO(sk->k); sk->lifetime = 0; sk->keylen = 0; sk = sk->next; diff --git a/libntp/decodenetnum.c b/libntp/decodenetnum.c index ce30d9093..9d950c6f8 100644 --- a/libntp/decodenetnum.c +++ b/libntp/decodenetnum.c @@ -67,7 +67,7 @@ decodenetnum( port_str = &cp[2]; cp = name; } - memset(&hints, 0, sizeof(hints)); + ZERO(hints); hints.ai_flags = Z_AI_NUMERICHOST; err = getaddrinfo(cp, "ntp", &hints, &ai); if (err != 0) diff --git a/libntp/dofptoa.c b/libntp/dofptoa.c index 1d60fe939..7a000afbb 100644 --- a/libntp/dofptoa.c +++ b/libntp/dofptoa.c @@ -33,7 +33,7 @@ dofptoa( /* * Zero out the buffer */ - memset((char *)cbuf, 0, sizeof cbuf); + ZERO(cbuf); /* * Set the pointers to point at the first diff --git a/libntp/dolfptoa.c b/libntp/dolfptoa.c index 93f229c78..0beddf749 100644 --- a/libntp/dolfptoa.c +++ b/libntp/dolfptoa.c @@ -31,7 +31,7 @@ dolfptoa( /* * Zero the character buffer */ - memset((char *) cbuf, 0, sizeof(cbuf)); + ZERO(cbuf); /* * Work on the integral part. This should work reasonable on diff --git a/libntp/emalloc.c b/libntp/emalloc.c index 6d627d7a7..49a07b54a 100644 --- a/libntp/emalloc.c +++ b/libntp/emalloc.c @@ -7,81 +7,6 @@ #include "ntp_syslog.h" #include "ntp_stdlib.h" -extern char *progname; - -#if !defined(_MSC_VER) || !defined(_DEBUG) - - -void * -erealloc( - void * prev, - size_t size - ) -{ - void * mem; - - mem = realloc(prev, size ? size : 1); - - if (NULL == mem) { - msyslog(LOG_ERR, - "fatal out of memory (%u bytes)", (u_int)size); - fprintf(stderr, - "%s: fatal out of memory (%u bytes)", progname, - (u_int)size); - exit(1); - } - - return mem; -} - - -void * -emalloc( - size_t size - ) -{ - return erealloc(NULL, size); -} - - -void * -emalloc_zero( - size_t size - ) -{ - void *ptr; - - ptr = erealloc(NULL, size); - if (NULL != ptr) - memset(ptr, 0, size); - - return ptr; -} - - -char * -estrdup( - const char * str - ) -{ - char * copy; - - copy = strdup(str); - - if (NULL == copy) { - msyslog(LOG_ERR, - "fatal out of memory duplicating %u bytes", - (u_int)strlen(str) + 1); - fprintf(stderr, - "%s: fatal out of memory duplicating %u bytes", - progname, (u_int)strlen(str) + 1); - exit(1); - } - - return copy; -} - -#else /* below is _MSC_VER && _DEBUG */ /* * When using the debug MS CRT allocator, each allocation stores the @@ -90,53 +15,82 @@ estrdup( * allocations to show up as coming from emalloc.c, so we preserve the * original callsite's source file and line using macros which pass * __FILE__ and __LINE__ as parameters to these routines. + * Other debug malloc implementations can be used by defining + * EREALLOC_IMPL() as ports/winnt/include/config.h does. */ void * -debug_ereallocz( - void * prev, - size_t size, - int zero_init, - const char * file, /* __FILE__ */ - int line /* __LINE__ */ +ereallocz( + void * ptr, + size_t newsz, + size_t priorsz, + int zero_init +#ifdef EREALLOC_CALLSITE /* ntp_malloc.h */ + , + const char * file, + int line +#endif ) { - void * mem; + char * mem; + size_t allocsz; - mem = _realloc_dbg(prev, size ? size : 1, - _NORMAL_BLOCK, file, line); + if (0 == newsz) + allocsz = 1; + else + allocsz = newsz; - if (mem != NULL) { - if (zero_init) - memset(mem, 0, size); - } else { + mem = EREALLOC_IMPL(ptr, allocsz, file, line); + if (NULL == mem) { + msyslog_term = TRUE; +#ifndef REALLOC_CALLSITE + msyslog(LOG_ERR, "fatal out of memory (%lu bytes)", + (u_long)newsz); +#else msyslog(LOG_ERR, - "fatal: out of memory in %s line %d size %u", - file, line, (u_int)size); - fprintf(stderr, - "%s: fatal: out of memory in %s line %d size %u", - progname, file, line, (u_int)size); + "fatal out of memory %s line %d (%lu bytes)", + file, line, (u_long)newsz); +#endif exit(1); } + if (zero_init && newsz > priorsz) + zero_mem(mem + priorsz, newsz - priorsz); + return mem; } + char * -debug_estrdup( - const char * str, - const char * file, /* __FILE__ */ - int line /* __LINE__ */ +estrdup_impl( + const char * str +#ifdef EREALLOC_CALLSITE + , + const char * file, + int line +#endif ) { char * copy; size_t bytes; bytes = strlen(str) + 1; - copy = debug_ereallocz(NULL, bytes, 0, file, line); + copy = ereallocz(NULL, bytes, 0, FALSE +#ifdef EREALLOC_CALLSITE + , file, line +#endif + ); memcpy(copy, str, bytes); return copy; } -#endif /* _MSC_VER && _DEBUG */ + +#ifndef EREALLOC_CALLSITE +void * +emalloc(size_t newsz) +{ + return ereallocz(NULL, newsz, 0, FALSE); +} +#endif + diff --git a/libntp/netof.c b/libntp/netof.c index edd65c931..c8831c3dd 100644 --- a/libntp/netof.c +++ b/libntp/netof.c @@ -43,7 +43,7 @@ netof( } else if (IS_IPV6(netaddr)) /* assume the typical /64 subnet size */ - memset(&NSRCADR6(netaddr)[8], 0, 8); + zero_mem(&NSRCADR6(netaddr)[8], 8); #ifdef DEBUG else { msyslog(LOG_ERR, "netof unknown AF %d", AF(netaddr)); diff --git a/libntp/ntp_lineedit.c b/libntp/ntp_lineedit.c index 55fdf0430..ed579c986 100644 --- a/libntp/ntp_lineedit.c +++ b/libntp/ntp_lineedit.c @@ -103,7 +103,7 @@ ntp_readline_init( success = 0; } else { - memset(&hev, 0, sizeof(hev)); + ZERO(hev); #ifdef H_SETSIZE history(ntp_hist, &hev, H_SETSIZE, 128); #endif diff --git a/libntp/ntp_rfc2553.c b/libntp/ntp_rfc2553.c index e305a34da..f5fd9b410 100644 --- a/libntp/ntp_rfc2553.c +++ b/libntp/ntp_rfc2553.c @@ -80,7 +80,6 @@ #include "ntpd.h" #include "ntp_malloc.h" -#include "ntp_stdlib.h" #include "ntp_string.h" #include "ntp_debug.h" @@ -107,19 +106,15 @@ * The rest of ntp_rfc2553.c is conditioned on ISC_PLATFORM_HAVEIPV6 * not being defined, copy_addrinfo_list() is an exception. */ -#if !defined(_MSC_VER) || !defined(_DEBUG) struct addrinfo * -copy_addrinfo_list( - const struct addrinfo *src - ) -#else -struct addrinfo * -debug_copy_addrinfo_list( - const struct addrinfo * src, +copy_addrinfo_list_impl( + const struct addrinfo * src +#ifdef EREALLOC_CALLSITE + , const char * caller_file, int caller_line - ) #endif + ) { const struct addrinfo * ai_src; struct addrinfo * ai_cpy; @@ -143,12 +138,8 @@ debug_copy_addrinfo_list( octets = elements * (sizeof(*ai_cpy) + sizeof(*psau)); octets += canons_octets; -#if !defined(_MSC_VER) || !defined(_DEBUG) - dst = emalloc(octets); - memset(dst, 0, octets); -#else - dst = debug_ereallocz(NULL, octets, 1, caller_file, caller_line); -#endif + dst = erealloczsite(NULL, octets, 0, TRUE, caller_file, + caller_line); ai_cpy = dst; psau = (void *)(ai_cpy + elements); pcanon = (void *)(psau + elements); diff --git a/libntp/recvbuff.c b/libntp/recvbuff.c index a78f37434..2a8f046ee 100644 --- a/libntp/recvbuff.c +++ b/libntp/recvbuff.c @@ -72,7 +72,7 @@ lowater_additions(void) static inline void initialise_buffer(recvbuf_t *buff) { - memset(buff, 0, sizeof(*buff)); + ZERO(*buff); } static void @@ -85,7 +85,7 @@ create_buffers(int nbufs) buffer_shortfall = 0; #ifndef DEBUG - bufp = emalloc(abuf * sizeof(*bufp)); + bufp = emalloc_zero(abuf * sizeof(*bufp)); #endif for (i = 0; i < abuf; i++) { @@ -95,9 +95,8 @@ create_buffers(int nbufs) * free()d during ntpd shutdown on DEBUG builds to * keep them out of heap leak reports. */ - bufp = emalloc(sizeof(*bufp)); + bufp = emalloc_zero(sizeof(*bufp)); #endif - memset(bufp, 0, sizeof(*bufp)); LINK_SLIST(free_recv_list, bufp, link.next); bufp++; free_recvbufs++; diff --git a/libntp/socktohost.c b/libntp/socktohost.c index 492521190..5f7925540 100644 --- a/libntp/socktohost.c +++ b/libntp/socktohost.c @@ -48,7 +48,7 @@ socktohost( * Resolve the reversed name and make sure the reversed address * is among the results. */ - memset(&hints, 0, sizeof(hints)); + ZERO(hints); hints.ai_family = AF(sock); hints.ai_protocol = IPPROTO_UDP; hints.ai_socktype = SOCK_DGRAM; diff --git a/libntp/syssignal.c b/libntp/syssignal.c index 983e930ab..cbdb02f2c 100644 --- a/libntp/syssignal.c +++ b/libntp/syssignal.c @@ -75,7 +75,7 @@ signal_no_reset( struct sigvec sv; int n; - memset(&sv, 0, sizeof(sv)); + ZERO(sv); sv.sv_handler = func; n = sigvec(sig, &sv, (struct sigvec *)NULL); if (-1 == n) { diff --git a/libntp/systime.c b/libntp/systime.c index e29f535a1..67176ae92 100644 --- a/libntp/systime.c +++ b/libntp/systime.c @@ -281,10 +281,10 @@ step_systime( #endif #ifdef HAVE_UTMP_H - memset(&ut, 0, sizeof(ut)); + ZERO(ut); #endif #ifdef HAVE_UTMPX_H - memset(&utx, 0, sizeof(utx)); + ZERO(utx); #endif /* UTMP */ diff --git a/m4/ntp_googletest.m4 b/m4/ntp_googletest.m4 new file mode 100644 index 000000000..fd492c896 --- /dev/null +++ b/m4/ntp_googletest.m4 @@ -0,0 +1,52 @@ +dnl ###################################################################### +dnl NTP_GOOGLETEST gtest support shared by top-level and sntp/configure.ac +AC_DEFUN([NTP_GOOGLETEST], [ +gta=false +AC_ARG_WITH( + [gtest], + [AS_HELP_STRING( + [--with-gtest], + [Use the gtest framework if it's available] + )], + [try_gtest=$withval], + [try_gtest=yes] +) +case "$try_gtest" in + yes) + AC_PATH_PROG([GTEST_CONFIG], [gtest-config]) + case x${GTEST_CONFIG} in + x) ;; + *) + AC_MSG_CHECKING([gtest version]) + gtest_version_test=`$GTEST_CONFIG --min-version=1.5 || echo toolow` + case "$gtest_version_test" in + toolow*) + ;; + *) + GTEST_LDFLAGS=`$GTEST_CONFIG --ldflags` + GTEST_LIBS=`$GTEST_CONFIG --libs` + GTEST_CXXFLAGS=`$GTEST_CONFIG --cxxflags` + GTEST_CPPFLAGS=`$GTEST_CONFIG --cppflags` + AC_SUBST([GTEST_LDFLAGS]) + AC_SUBST([GTEST_LIBS]) + AC_SUBST([GTEST_CXXFLAGS]) + AC_SUBST([GTEST_CPPFLAGS]) + gta=true + ;; + esac + gtest_version=`$GTEST_CONFIG --version` + case "$gta" in + true) + AC_MSG_RESULT([($gtest_version) ok]) + ;; + *) AC_MSG_RESULT([($gtest_version) not ok]) + ;; + esac + AS_UNSET([gtest_version_test]) + AS_UNSET([gtest_version]) + esac +esac +AM_CONDITIONAL([GTEST_AVAILABLE], [$gta]) + +]) +dnl ====================================================================== diff --git a/ntpd/keyword-gen.c b/ntpd/keyword-gen.c index cab636f38..03cb32189 100644 --- a/ntpd/keyword-gen.c +++ b/ntpd/keyword-gen.c @@ -558,8 +558,7 @@ create_scan_states( /* relocate so token id is sst[] index */ if (my_state != token) { sst[token] = sst[my_state]; - memset(&sst[my_state], 0, - sizeof(sst[my_state])); + ZERO(sst[my_state]); do sst_highwater--; while (sst[sst_highwater].finishes_token); diff --git a/ntpd/ntp_config.c b/ntpd/ntp_config.c index e6a61f083..1ac8b4374 100644 --- a/ntpd/ntp_config.c +++ b/ntpd/ntp_config.c @@ -381,7 +381,7 @@ init_syntax_tree( config_tree *ptree ) { - memset(ptree, 0, sizeof(*ptree)); + ZERO(*ptree); } @@ -2408,7 +2408,7 @@ config_access( * restrict hosts have been resolved. */ ai_list = NULL; - memset(&hints, 0, sizeof(hints)); + ZERO(hints); hints.ai_protocol = IPPROTO_UDP; hints.ai_socktype = SOCK_DGRAM; hints.ai_family = my_node->addr->type; @@ -3106,7 +3106,7 @@ config_trap( * save context and hand it off * for name resolution. */ - memset(&hints, 0, sizeof(hints)); + ZERO(hints); hints.ai_protocol = IPPROTO_UDP; hints.ai_socktype = SOCK_DGRAM; snprintf(port_text, sizeof(port_text), @@ -3183,7 +3183,7 @@ trap_name_resolved( return; } NTP_INSIST(sizeof(peeraddr) >= res->ai_addrlen); - memset(&peeraddr, 0, sizeof(peeraddr)); + ZERO(peeraddr); memcpy(&peeraddr, res->ai_addr, res->ai_addrlen); localaddr = NULL; if (pstp->ifaddr_nonnull) @@ -3248,7 +3248,7 @@ config_fudge( } /* Parse all the options to the fudge command */ - memset(&clock_stat, 0, sizeof(clock_stat)); + ZERO(clock_stat); curr_opt = HEAD_PFIFO(curr_fudge->options); for (; curr_opt != NULL; curr_opt = curr_opt->link) { switch (curr_opt->attr) { @@ -3614,7 +3614,7 @@ config_peers( ctx->version = NTP_VERSION; ctx->flags = FLAG_IBURST; - memset(&hints, 0, sizeof(hints)); + ZERO(hints); hints.ai_family = (u_short)ctx->family; hints.ai_socktype = SOCK_DGRAM; hints.ai_protocol = IPPROTO_UDP; @@ -3692,7 +3692,7 @@ config_peers( ctx->keyid = curr_peer->peerkey; ctx->group = curr_peer->group; - memset(&hints, 0, sizeof(hints)); + ZERO(hints); hints.ai_family = ctx->family; hints.ai_socktype = SOCK_DGRAM; hints.ai_protocol = IPPROTO_UDP; @@ -3842,7 +3842,7 @@ config_unpeers( continue; } - memset(&peeraddr, 0, sizeof(peeraddr)); + ZERO(peeraddr); AF(&peeraddr) = curr_unpeer->addr->type; name = curr_unpeer->addr->address; rc = getnetnum(name, &peeraddr, 0, t_UNK); @@ -3875,7 +3875,7 @@ config_unpeers( } /* Resolve the hostname to address(es). */ # ifdef WORKER - memset(&hints, 0, sizeof(hints)); + ZERO(hints); hints.ai_family = curr_unpeer->addr->type; hints.ai_socktype = SOCK_DGRAM; hints.ai_protocol = IPPROTO_UDP; @@ -4118,7 +4118,7 @@ config_ntpd( { struct addrinfo hints; - memset(&hints, 0, sizeof(hints)); + ZERO(hints); hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; getaddrinfo_sometime("www.cnn.com", "ntp", &hints, @@ -4167,7 +4167,7 @@ config_remotely( snprintf(origin, sizeof(origin), "remote config from %s", stoa(remote_addr)); - memset(&remote_cuckoo, 0, sizeof(remote_cuckoo)); + ZERO(remote_cuckoo); remote_cuckoo.fname = origin; remote_cuckoo.line_no = 1; remote_cuckoo.col_no = 1; @@ -4314,7 +4314,7 @@ save_and_apply_config_tree(void) */ ptree = emalloc(sizeof(*ptree)); memcpy(ptree, &cfgt, sizeof(*ptree)); - memset(&cfgt, 0, sizeof(cfgt)); + ZERO(cfgt); LINK_TAIL_SLIST(cfg_tree_history, ptree, link, config_tree); diff --git a/ntpd/ntp_control.c b/ntpd/ntp_control.c index 6f434d813..8163b72ee 100644 --- a/ntpd/ntp_control.c +++ b/ntpd/ntp_control.c @@ -624,7 +624,7 @@ static struct utsname utsnamebuf; * timed out. */ /* ntp_request.c */ -struct ctl_trap ctl_trap[CTL_MAXTRAPS]; +struct ctl_trap ctl_traps[CTL_MAXTRAPS]; int num_ctl_traps; /* @@ -773,8 +773,8 @@ init_control(void) ctl_sys_num_events = 0; num_ctl_traps = 0; - for (i = 0; i < COUNTOF(ctl_trap); i++) - ctl_trap[i].tr_flags = 0; + for (i = 0; i < COUNTOF(ctl_traps); i++) + ctl_traps[i].tr_flags = 0; } @@ -1195,20 +1195,20 @@ ctl_flushpkt( rpkt.count = htons((u_short)dlen); rpkt.offset = htons((u_short)res_offset); if (res_async) { - for (i = 0; i < COUNTOF(ctl_trap); i++) { - if (TRAP_INUSE & ctl_trap[i].tr_flags) { + for (i = 0; i < COUNTOF(ctl_traps); i++) { + if (TRAP_INUSE & ctl_traps[i].tr_flags) { rpkt.li_vn_mode = PKT_LI_VN_MODE( sys_leap, - ctl_trap[i].tr_version, + ctl_traps[i].tr_version, MODE_CONTROL); rpkt.sequence = - htons(ctl_trap[i].tr_sequence); - sendpkt(&ctl_trap[i].tr_addr, - ctl_trap[i].tr_localaddr, -4, + htons(ctl_traps[i].tr_sequence); + sendpkt(&ctl_traps[i].tr_addr, + ctl_traps[i].tr_localaddr, -4, (struct pkt *)&rpkt, sendlen); if (!more) - ctl_trap[i].tr_sequence++; + ctl_traps[i].tr_sequence++; numasyncmsgs++; } } @@ -1309,18 +1309,15 @@ ctl_putstr( size_t len ) { - char *cp; - char *cpend; - const char *cq; char buffer[512]; + char *cp; + size_t tl; - cp = buffer; - cpend = buffer + sizeof(buffer); - cq = tag; - while (cp < cpend && *cq != '\0') - *cp++ = *cq++; + tl = strlen(tag); + memcpy(buffer, tag, tl); + cp = buffer + tl; if (len > 0) { - NTP_INSIST(cp + 3 + len <= cpend); + NTP_INSIST(tl + 3 + len <= sizeof(buffer)); *cp++ = '='; *cp++ = '"'; memcpy(cp, data, len); @@ -1347,18 +1344,15 @@ ctl_putunqstr( size_t len ) { - char *cp; - char *cpend; - const char *cq; char buffer[512]; + char *cp; + size_t tl; - cp = buffer; - cpend = buffer + sizeof(buffer); - cq = tag; - while (cp < cpend && *cq != '\0') - *cp++ = *cq++; + tl = strlen(tag); + memcpy(buffer, tag, tl); + cp = buffer + tl; if (len > 0) { - NTP_INSIST(cp + 1 + len <= cpend); + NTP_INSIST(tl + 1 + len <= sizeof(buffer)); *cp++ = '='; memcpy(cp, data, len); cp += len; @@ -1658,7 +1652,7 @@ ctl_putsys( */ if (CS_KERN_FIRST <= varid && varid <= CS_KERN_LAST && current_time != ntp_adjtime_time) { - memset(&ntx, 0, sizeof(ntx)); + ZERO(ntx); if (ntp_adjtime(&ntx) < 0) msyslog(LOG_ERR, "ntp_adjtime() for mode 6 query failed: %m"); else @@ -1784,7 +1778,7 @@ ctl_putsys( case CS_VARLIST: s = buf; be = buf + sizeof(buf); - if (s + strlen(sys_var[CS_VARLIST].text) + 4 > be) + if (strlen(sys_var[CS_VARLIST].text) + 4 > sizeof(buf)) break; /* really long var name */ snprintf(s, sizeof(buf), "%s=\"", @@ -2532,56 +2526,56 @@ ctl_putclock( switch (id) { - case CC_TYPE: + case CC_TYPE: if (mustput || pcs->clockdesc == NULL || *(pcs->clockdesc) == '\0') { ctl_putuint(clock_var[id].text, pcs->type); } break; - case CC_TIMECODE: + case CC_TIMECODE: ctl_putstr(clock_var[id].text, pcs->p_lastcode, (unsigned)pcs->lencode); break; - case CC_POLL: + case CC_POLL: ctl_putuint(clock_var[id].text, pcs->polls); break; - case CC_NOREPLY: + case CC_NOREPLY: ctl_putuint(clock_var[id].text, pcs->noresponse); break; - case CC_BADFORMAT: + case CC_BADFORMAT: ctl_putuint(clock_var[id].text, pcs->badformat); break; - case CC_BADDATA: + case CC_BADDATA: ctl_putuint(clock_var[id].text, pcs->baddata); break; - case CC_FUDGETIME1: + case CC_FUDGETIME1: if (mustput || (pcs->haveflags & CLK_HAVETIME1)) ctl_putdbl(clock_var[id].text, pcs->fudgetime1 * 1e3); break; - case CC_FUDGETIME2: + case CC_FUDGETIME2: if (mustput || (pcs->haveflags & CLK_HAVETIME2)) ctl_putdbl(clock_var[id].text, pcs->fudgetime2 * 1e3); break; - case CC_FUDGEVAL1: + case CC_FUDGEVAL1: if (mustput || (pcs->haveflags & CLK_HAVEVAL1)) ctl_putint(clock_var[id].text, pcs->fudgeval1); break; - case CC_FUDGEVAL2: + case CC_FUDGEVAL2: if (mustput || (pcs->haveflags & CLK_HAVEVAL2)) { if (pcs->fudgeval1 > 1) ctl_putadr(clock_var[id].text, @@ -2592,14 +2586,14 @@ ctl_putclock( } break; - case CC_FLAGS: + case CC_FLAGS: if (mustput || (pcs->haveflags & (CLK_HAVEFLAG1 | CLK_HAVEFLAG2 | CLK_HAVEFLAG3 | CLK_HAVEFLAG4))) ctl_putuint(clock_var[id].text, pcs->flags); break; - case CC_DEVICE: + case CC_DEVICE: if (pcs->clockdesc == NULL || *(pcs->clockdesc) == '\0') { if (mustput) @@ -2612,62 +2606,59 @@ ctl_putclock( } break; - case CC_VARLIST: - { - - s = buf; - be = buf + sizeof(buf); - if (s + strlen(clock_var[CC_VARLIST].text) + 4 > - be) - break; /* really long var name */ - - snprintf(s, sizeof(buf), "%s=\"", - clock_var[CC_VARLIST].text); - s += strlen(s); - t = s; - - for (k = clock_var; !(k->flags & EOV); k++) { - if (k->flags & PADDING) - continue; - - i = strlen(k->text); - if (s + i + 1 >= be) - break; - - if (s != t) - *s++ = ','; - memcpy(s, k->text, i); - s += i; - } - - for (k = pcs->kv_list; k && !(EOV & k->flags); k++) { - if (PADDING & k->flags) - continue; - - ss = k->text; - if (!ss) - continue; - - while (*ss && *ss != '=') - ss++; - i = ss - k->text; - if (s + i + 1 >= be) - break; - - if (s != t) - *s++ = ','; - memcpy(s, k->text, (unsigned)i); - s += i; - *s = '\0'; - } - if (s + 2 >= be) - break; - - *s++ = '"'; - *s = '\0'; - ctl_putdata(buf, (unsigned)(s - buf), 0); - } - break; + case CC_VARLIST: + s = buf; + be = buf + sizeof(buf); + if (strlen(clock_var[CC_VARLIST].text) + 4 > + sizeof(buf)) + break; /* really long var name */ + + snprintf(s, sizeof(buf), "%s=\"", + clock_var[CC_VARLIST].text); + s += strlen(s); + t = s; + + for (k = clock_var; !(EOV & k->flags); k++) { + if (PADDING & k->flags) + continue; + + i = strlen(k->text); + if (s + i + 1 >= be) + break; + + if (s != t) + *s++ = ','; + memcpy(s, k->text, i); + s += i; + } + + for (k = pcs->kv_list; k && !(EOV & k->flags); k++) { + if (PADDING & k->flags) + continue; + + ss = k->text; + if (NULL == ss) + continue; + + while (*ss && *ss != '=') + ss++; + i = ss - k->text; + if (s + i + 1 >= be) + break; + + if (s != t) + *s++ = ','; + memcpy(s, k->text, (unsigned)i); + s += i; + *s = '\0'; + } + if (s + 2 >= be) + break; + + *s++ = '"'; + *s = '\0'; + ctl_putdata(buf, (unsigned)(s - buf), 0); + break; } } #endif @@ -2735,7 +2726,7 @@ ctl_getitem( cp++; while (cp < reqend && *cp != ',') { *tp++ = *cp++; - if (tp >= buf + sizeof(buf)) { + if (tp - buf >= sizeof(buf)) { ctl_error(CERR_BADFMT); numctlbadpkts++; NLOG(NLOG_SYSEVENT) @@ -2882,7 +2873,7 @@ read_peervars(void) rpkt.status = htons(ctlpeerstatus(peer)); if (res_authokay) peer->num_events = 0; - memset(&wants, 0, sizeof(wants)); + ZERO(wants); gotvar = 0; while (NULL != (v = ctl_getitem(peer_var, &valuep))) { if (v->flags & EOV) { @@ -2928,8 +2919,7 @@ read_sysvars(void) if (res_authokay) ctl_sys_num_events = 0; wants_count = CS_MAXCODE + 1 + count_var(ext_sys_var); - wants = emalloc(wants_count); - memset(wants, 0, wants_count); + wants = emalloc_zero(wants_count); gotvar = 0; while (NULL != (v = ctl_getitem(sys_var, &valuep))) { if (!(EOV & v->flags)) { @@ -3291,7 +3281,7 @@ send_mru_entry( const char * pch; remaining = COUNTOF(sent); - memset(sent, 0, sizeof(sent)); + ZERO(sent); noise = (u_int32)ntp_random(); while (remaining > 0) { which = (noise & 7) % COUNTOF(sent); @@ -3547,8 +3537,8 @@ static void read_mru_list( maxlstint = 0; lcladr = NULL; priors = 0; - memset(last, 0, sizeof(last)); - memset(addr, 0, sizeof(addr)); + ZERO(last); + ZERO(addr); while (NULL != (v = ctl_getitem(in_parms, &val)) && !(EOV & v->flags)) { @@ -3726,7 +3716,7 @@ send_ifstats_entry( const char *pch; remaining = COUNTOF(sent); - memset(sent, 0, sizeof(sent)); + ZERO(sent); noise = 0; noisebits = 0; while (remaining > 0) { @@ -3919,8 +3909,7 @@ read_clockstatus( */ rpkt.status = htons(ctlclkstatus(&cs)); gotvar = CC_MAXCODE + 1 + count_var(kv); - wants = emalloc(gotvar); - memset(wants, 0, gotvar); + wants = emalloc_zero(gotvar); gotvar = 0; while (NULL != (v = ctl_getitem(clock_var, &valuep))) { if (!(EOV & v->flags)) { @@ -4062,8 +4051,9 @@ ctlsettrap( int version ) { - register struct ctl_trap *tp; - register struct ctl_trap *tptouse; + size_t n; + struct ctl_trap *tp; + struct ctl_trap *tptouse; /* * See if we can find this trap. If so, we only need update @@ -4072,17 +4062,17 @@ ctlsettrap( if ((tp = ctlfindtrap(raddr, linter)) != NULL) { switch (traptype) { - case TRAP_TYPE_CONFIG: + case TRAP_TYPE_CONFIG: tp->tr_flags = TRAP_INUSE|TRAP_CONFIGURED; break; - case TRAP_TYPE_PRIO: + case TRAP_TYPE_PRIO: if (tp->tr_flags & TRAP_CONFIGURED) return (1); /* don't change anything */ tp->tr_flags = TRAP_INUSE; break; - case TRAP_TYPE_NONPRIO: + case TRAP_TYPE_NONPRIO: if (tp->tr_flags & TRAP_CONFIGURED) return (1); /* don't change anything */ tp->tr_flags = TRAP_INUSE|TRAP_NONPRIO; @@ -4099,29 +4089,30 @@ ctlsettrap( * have to. Clear out anyone who's expired while we're at it. */ tptouse = NULL; - for (tp = ctl_trap; tp < &ctl_trap[CTL_MAXTRAPS]; tp++) { - if ((tp->tr_flags & TRAP_INUSE) && - !(tp->tr_flags & TRAP_CONFIGURED) && + for (n = 0; n < COUNTOF(ctl_traps); n++) { + tp = &ctl_traps[n]; + if ((TRAP_INUSE & tp->tr_flags) && + !(TRAP_CONFIGURED & tp->tr_flags) && ((tp->tr_settime + CTL_TRAPTIME) > current_time)) { tp->tr_flags = 0; num_ctl_traps--; } - if (!(tp->tr_flags & TRAP_INUSE)) { + if (!(TRAP_INUSE & tp->tr_flags)) { tptouse = tp; - } else if (!(tp->tr_flags & TRAP_CONFIGURED)) { + } else if (!(TRAP_CONFIGURED & tp->tr_flags)) { switch (traptype) { - case TRAP_TYPE_CONFIG: + case TRAP_TYPE_CONFIG: if (tptouse == NULL) { tptouse = tp; break; } - if (tptouse->tr_flags & TRAP_NONPRIO && - !(tp->tr_flags & TRAP_NONPRIO)) + if ((TRAP_NONPRIO & tptouse->tr_flags) && + !(TRAP_NONPRIO & tp->tr_flags)) break; - if (!(tptouse->tr_flags & TRAP_NONPRIO) - && tp->tr_flags & TRAP_NONPRIO) { + if (!(TRAP_NONPRIO & tptouse->tr_flags) + && (TRAP_NONPRIO & tp->tr_flags)) { tptouse = tp; break; } @@ -4130,18 +4121,18 @@ ctlsettrap( tptouse = tp; break; - case TRAP_TYPE_PRIO: - if (tp->tr_flags & TRAP_NONPRIO) { + case TRAP_TYPE_PRIO: + if ( TRAP_NONPRIO & tp->tr_flags) { if (tptouse == NULL || - (tptouse->tr_flags & - TRAP_INUSE && + ((TRAP_INUSE & + tptouse->tr_flags) && tptouse->tr_origtime < tp->tr_origtime)) tptouse = tp; } break; - case TRAP_TYPE_NONPRIO: + case TRAP_TYPE_NONPRIO: break; } } @@ -4206,15 +4197,15 @@ ctlfindtrap( struct interface *linter ) { - register struct ctl_trap *tp; + size_t n; - for (tp = ctl_trap; tp < &ctl_trap[CTL_MAXTRAPS]; tp++) { - if ((tp->tr_flags & TRAP_INUSE) - && ADDR_PORT_EQ(raddr, &tp->tr_addr) - && (linter == tp->tr_localaddr) ) - return (tp); - } - return (struct ctl_trap *)NULL; + for (n = 0; n < COUNTOF(ctl_traps); n++) + if ((ctl_traps[n].tr_flags & TRAP_INUSE) + && ADDR_PORT_EQ(raddr, &ctl_traps[n].tr_addr) + && (linter == ctl_traps[n].tr_localaddr)) + return &ctl_traps[n]; + + return NULL; } diff --git a/ntpd/ntp_intres.c b/ntpd/ntp_intres.c index 8e88b9352..c493e3377 100644 --- a/ntpd/ntp_intres.c +++ b/ntpd/ntp_intres.c @@ -212,16 +212,14 @@ getaddrinfo_sometime( servsize = strlen(service) + 1; req_size = sizeof(*gai_req) + nodesize + servsize; - gai_req = emalloc(req_size); + gai_req = emalloc_zero(req_size); gai_req->octets = req_size; now = time(NULL); next_dns_timeslot = max(now, next_dns_timeslot); gai_req->scheduled = now; gai_req->earliest = next_dns_timeslot; - if (NULL == hints) - memset(&gai_req->hints, 0, sizeof(gai_req->hints)); - else + if (hints != NULL) gai_req->hints = *hints; gai_req->retry = retry; gai_req->callback = callback; @@ -575,8 +573,7 @@ getnameinfo_sometime( NTP_REQUIRE(hostoctets); NTP_REQUIRE(hostoctets + servoctets < 1024); - gni_req = emalloc(sizeof(*gni_req)); - memset(gni_req, 0, sizeof(*gni_req)); + gni_req = emalloc_zero(sizeof(*gni_req)); gni_req->octets = sizeof(*gni_req); time_now = time(NULL); diff --git a/ntpd/ntp_io.c b/ntpd/ntp_io.c index 4d25bb592..3e63a2655 100644 --- a/ntpd/ntp_io.c +++ b/ntpd/ntp_io.c @@ -722,10 +722,9 @@ new_asyncio_reader(void) { struct asyncio_reader *reader; - reader = emalloc(sizeof(*reader)); - - memset(reader, 0, sizeof(*reader)); + reader = emalloc_zero(sizeof(*reader)); reader->fd = INVALID_SOCKET; + return reader; } @@ -785,12 +784,12 @@ addr_eqprefix( isc_netaddr_t isc_b; isc_sockaddr_t isc_sa; - memset(&isc_sa, 0, sizeof(isc_sa)); + ZERO(isc_sa); memcpy(&isc_sa.type.sa, &a->sa, min(sizeof(isc_sa.type), sizeof(a))); isc_netaddr_fromsockaddr(&isc_a, &isc_sa); - memset(&isc_sa, 0, sizeof(isc_sa)); + ZERO(isc_sa); memcpy(&isc_sa.type.sa, &b->sa, min(sizeof(isc_sa.type), sizeof(b))); isc_netaddr_fromsockaddr(&isc_b, &isc_sa); @@ -863,7 +862,7 @@ is_ip_address( NTP_REQUIRE(host != NULL); NTP_REQUIRE(addr != NULL); - memset(addr, 0, sizeof(*addr)); + ZERO_SOCK(addr); /* * Try IPv4, then IPv6. In order to handle the extended format @@ -933,7 +932,7 @@ init_interface( endpt *ep ) { - memset(ep, 0, sizeof(*ep)); + ZERO(*ep); ep->fd = INVALID_SOCKET; ep->bfd = INVALID_SOCKET; ep->phase = sys_interphase; @@ -1240,7 +1239,7 @@ create_wildcards( * the first if (v4wild). */ action = ACTION_LISTEN; - memset(&wildaddr, 0, sizeof(wildaddr)); + ZERO(wildaddr); /* * create pseudo-interface with wildcard IPv4 address @@ -1300,7 +1299,7 @@ create_wildcards( v6wild = ipv6_works; if (v6wild) { /* set wildaddr to the v6 wildcard address :: */ - memset(&wildaddr, 0, sizeof(wildaddr)); + ZERO(wildaddr); AF(&wildaddr) = AF_INET6; SET_ADDR6N(&wildaddr, in6addr_any); SET_PORT(&wildaddr, port); @@ -1356,8 +1355,7 @@ add_nic_rule( nic_rule * rule; isc_boolean_t is_ip; - rule = emalloc(sizeof(*rule)); - memset(rule, 0, sizeof(*rule)); + rule = emalloc_zero(sizeof(*rule)); rule->match_type = match_type; rule->prefixlen = prefixlen; rule->action = action; @@ -1693,7 +1691,7 @@ sau_from_netaddr( const isc_netaddr_t *pna ) { - memset(psau, 0, sizeof(*psau)); + ZERO_SOCK(psau); AF(psau) = (u_short)pna->family; switch (pna->family) { @@ -1775,7 +1773,7 @@ is_anycast( return ISC_FALSE; if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) return ISC_FALSE; - memset(&ifr6, 0, sizeof(ifr6)); + ZERO(ifr6); memcpy(&ifr6.ifr_addr, &psau->sa6, sizeof(ifr6.ifr_addr)); strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name)); if (ioctl(fd, SIOCGIFAFLAG_IN6, &ifr6) < 0) { @@ -2498,7 +2496,7 @@ socket_multicast_enable( switch (AF(maddr)) { case AF_INET: - memset(&mreq, 0, sizeof(mreq)); + ZERO(mreq); mreq.imr_multiaddr = SOCK_ADDR4(maddr); mreq.imr_interface.s_addr = htonl(INADDR_ANY); if (setsockopt(iface->fd, @@ -2529,7 +2527,7 @@ socket_multicast_enable( * for other types of multicast addresses. For now let * the kernel figure it out. */ - memset(&mreq6, 0, sizeof(mreq6)); + ZERO(mreq6); mreq6.ipv6mr_multiaddr = SOCK_ADDR6(maddr); mreq6.ipv6mr_interface = iface->ifindex; @@ -2574,7 +2572,7 @@ socket_multicast_disable( #endif struct ip_mreq mreq; - memset(&mreq, 0, sizeof(mreq)); + ZERO(mreq); if (find_addr_in_list(maddr) == NULL) { DPRINTF(4, ("socket_multicast_disable(%s): not found\n", @@ -3911,7 +3909,7 @@ calc_addr_distance( NTP_REQUIRE(AF(a1) == AF(a2)); - memset(dist, 0, sizeof(*dist)); + ZERO_SOCK(dist); AF(dist) = AF(a1); /* v4 can be done a bit simpler */ @@ -4572,7 +4570,7 @@ init_async_notifications() fd = move_fd(fd); #ifdef HAVE_RTNETLINK - memset(&sa, 0, sizeof(sa)); + ZERO(sa); sa.nl_family = PF_NETLINK; sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR | RTMGRP_IPV4_ROUTE diff --git a/ntpd/ntp_monitor.c b/ntpd/ntp_monitor.c index f284587e3..cfead2345 100644 --- a/ntpd/ntp_monitor.c +++ b/ntpd/ntp_monitor.c @@ -142,7 +142,7 @@ mon_free_entry( mon_entry *m ) { - memset(m, 0, sizeof(*m)); + ZERO(*m); LINK_SLIST(mon_free, m, hash_next); } @@ -163,7 +163,7 @@ mon_reclaim_entry( { UNLINK_DLIST(m, mru); remove_from_hash(m); - memset(m, 0, sizeof(*m)); + ZERO(*m); } @@ -221,8 +221,7 @@ mon_start( mon_hash_bits = max(4, mon_hash_bits); mon_hash_bits = min(16, mon_hash_bits); octets = sizeof(*mon_hash) * MON_HASH_SIZE; - mon_hash = erealloc(mon_hash, octets); - memset(mon_hash, 0, octets); + mon_hash = erealloc_zero(mon_hash, octets, 0); mon_enabled = mode; } @@ -259,7 +258,7 @@ mon_stop( /* empty the MRU list and hash table. */ mru_entries = 0; INIT_DLIST(mon_mru_list, mru); - memset(mon_hash, 0, sizeof(*mon_hash) * MON_HASH_SIZE); + zero_mem(mon_hash, sizeof(*mon_hash) * MON_HASH_SIZE); } diff --git a/ntpd/ntp_peer.c b/ntpd/ntp_peer.c index dce06e7e1..5d60b5e0f 100644 --- a/ntpd/ntp_peer.c +++ b/ntpd/ntp_peer.c @@ -167,8 +167,7 @@ getmorepeermem(void) int i; struct peer *peers; - peers = emalloc(INC_PEER_ALLOC * sizeof(*peers)); - memset(peers, 0, INC_PEER_ALLOC * sizeof(*peers)); + peers = emalloc_zero(INC_PEER_ALLOC * sizeof(*peers)); for (i = INC_PEER_ALLOC - 1; i >= 0; i--) LINK_SLIST(peer_free, &peers[i], p_link); @@ -495,7 +494,7 @@ free_peer( free(p->addrs); /* from copy_addrinfo_list() */ /* Add his corporeal form to peer free list */ - memset(p, 0, sizeof(*p)); + ZERO(*p); LINK_SLIST(peer_free, p, p_link); peer_free_count++; } diff --git a/ntpd/ntp_refclock.c b/ntpd/ntp_refclock.c index 70fa576ee..24809fde8 100644 --- a/ntpd/ntp_refclock.c +++ b/ntpd/ntp_refclock.c @@ -197,8 +197,7 @@ refclock_newpeer( /* * Allocate and initialize interface structure */ - pp = emalloc(sizeof(*pp)); - memset(pp, 0, sizeof(*pp)); + pp = emalloc_zero(sizeof(*pp)); peer->procptr = pp; /* @@ -1238,7 +1237,7 @@ refclock_params( struct refclock_atom *ap /* atom structure pointer */ ) { - memset(&ap->pps_params, 0, sizeof(pps_params_t)); + ZERO(ap->pps_params); ap->pps_params.api_version = PPS_API_VERS_1; /* @@ -1310,7 +1309,7 @@ refclock_pps( } timeout.tv_sec = 0; timeout.tv_nsec = 0; - memset(&pps_info, 0, sizeof(pps_info_t)); + ZERO(pps_info); if (time_pps_fetch(ap->handle, PPS_TSFMT_TSPEC, &pps_info, &timeout) < 0) { refclock_report(peer, CEVNT_FAULT); diff --git a/ntpd/ntp_request.c b/ntpd/ntp_request.c index 75e98d0ee..e86d3a433 100644 --- a/ntpd/ntp_request.c +++ b/ntpd/ntp_request.c @@ -739,7 +739,7 @@ list_peers_sum( if (pp->dstadr) ips->dstadr6 = SOCK_ADDR6(&pp->dstadr->sin); else - memset(&ips->dstadr6, 0, sizeof(ips->dstadr6)); + ZERO(ips->dstadr6); skip = 0; } else { skip = 1; @@ -848,7 +848,7 @@ peer_info ( ? SOCK_ADDR6(&pp->dstadr->bcast) : SOCK_ADDR6(&pp->dstadr->sin); else - memset(&ip->dstadr6, 0, sizeof(ip->dstadr6)); + ZERO(ip->dstadr6); ip->srcadr6 = SOCK_ADDR6(&pp->srcadr); ip->v6_flag = 1; @@ -958,7 +958,7 @@ peer_stats ( ip = (struct info_peer_stats *)prepare_pkt(srcadr, inter, inpkt, v6sizeof(struct info_peer_stats)); while (items-- > 0 && ip != 0) { - memset((char *)&addr, 0, sizeof(addr)); + ZERO(addr); NSRCPORT(&addr) = ipl->port; if (client_v6_capable && ipl->v6_flag) { AF(&addr) = AF_INET6; @@ -1007,7 +1007,7 @@ peer_stats ( ? SOCK_ADDR6(&pp->dstadr->bcast) : SOCK_ADDR6(&pp->dstadr->sin); else - memset(&ip->dstadr6, 0, sizeof(ip->dstadr6)); + ZERO(ip->dstadr6); ip->srcadr6 = SOCK_ADDR6(&pp->srcadr); ip->v6_flag = 1; @@ -1290,31 +1290,8 @@ do_conf( */ items = INFO_NITEMS(inpkt->err_nitems); cp = (struct conf_peer *)inpkt->data; - memset(&temp_cp, 0, sizeof(temp_cp)); - memcpy(&temp_cp, (char *)cp, INFO_ITEMSIZE(inpkt->mbz_itemsize)); - -#if 0 /* paranoid checking - these are done in newpeer() */ - fl = 0; - while (items-- > 0 && !fl) { - if (((temp_cp.version) > NTP_VERSION) - || ((temp_cp.version) < NTP_OLDVERSION)) - fl = 1; - if (temp_cp.hmode != MODE_ACTIVE - && temp_cp.hmode != MODE_CLIENT - && temp_cp.hmode != MODE_BROADCAST) - fl = 1; - if (temp_cp.flags & ~(CONF_FLAG_PREFER | CONF_FLAG_BURST | - CONF_FLAG_IBURST | CONF_FLAG_SKEY)) - fl = 1; - cp = (struct conf_peer *) - ((char *)cp + INFO_ITEMSIZE(inpkt->mbz_itemsize)); - } - - if (fl) { - req_ack(srcadr, inter, inpkt, INFO_ERR_FMT); - return; - } -#endif /* end paranoid checking */ + ZERO(temp_cp); + memcpy(&temp_cp, (void *)cp, INFO_ITEMSIZE(inpkt->mbz_itemsize)); /* * Looks okay, try it out @@ -1323,7 +1300,7 @@ do_conf( cp = (struct conf_peer *)inpkt->data; while (items-- > 0) { - memset(&temp_cp, 0, sizeof(struct conf_peer)); + ZERO(temp_cp); memcpy(&temp_cp, (char *)cp, INFO_ITEMSIZE(inpkt->mbz_itemsize)); ZERO_SOCK(&peeraddr); @@ -1404,7 +1381,7 @@ dns_a( */ items = INFO_NITEMS(inpkt->err_nitems); dp = (struct info_dns_assoc *)inpkt->data; - memset((char *)&peeraddr, 0, sizeof(struct sockaddr_in)); + ZERO(peeraddr); peeraddr.sin_family = AF_INET; peeraddr.sin_port = htons(NTP_PORT); @@ -1492,7 +1469,7 @@ do_unconf( bad = 0; while (items-- > 0 && !bad) { - memset(&temp_cp, 0, sizeof(temp_cp)); + ZERO(temp_cp); ZERO_SOCK(&peeraddr); memcpy(&temp_cp, cp, INFO_ITEMSIZE(inpkt->mbz_itemsize)); if (client_v6_capable && temp_cp.v6_flag) { @@ -1537,8 +1514,8 @@ do_unconf( cp = (struct conf_unpeer *)inpkt->data; while (items-- > 0) { - memset(&temp_cp, 0, sizeof(temp_cp)); - memset(&peeraddr, 0, sizeof(peeraddr)); + ZERO(temp_cp); + ZERO(peeraddr); memcpy(&temp_cp, cp, INFO_ITEMSIZE(inpkt->mbz_itemsize)); if (client_v6_capable && temp_cp.v6_flag) { AF(&peeraddr) = AF_INET6; @@ -2141,9 +2118,9 @@ req_get_traps( struct req_pkt *inpkt ) { - register struct info_trap *it; - register struct ctl_trap *tr; - register int i; + struct info_trap *it; + struct ctl_trap *tr; + int i; if (num_ctl_traps == 0) { req_ack(srcadr, inter, inpkt, INFO_ERR_NODATA); @@ -2153,7 +2130,7 @@ req_get_traps( it = (struct info_trap *)prepare_pkt(srcadr, inter, inpkt, v6sizeof(struct info_trap)); - for (i = 0, tr = ctl_trap; i < CTL_MAXTRAPS; i++, tr++) { + for (i = 0, tr = ctl_traps; i < COUNTOF(ctl_traps); i++, tr++) { if (tr->tr_flags & TRAP_INUSE) { if (IS_IPV4(&tr->tr_addr)) { if (tr->tr_localaddr == any_interface) @@ -2403,7 +2380,7 @@ get_kernel_info( return; } - memset((char *)&ntx, 0, sizeof(ntx)); + ZERO(ntx); if (ntp_adjtime(&ntx) < 0) msyslog(LOG_ERR, "get_kernel_info: ntp_adjtime() failed: %m"); ik = (struct info_kernel *)prepare_pkt(srcadr, inter, inpkt, @@ -2523,8 +2500,8 @@ set_clock_fudge( sockaddr_u addr; l_fp ltmp; - ZERO_SOCK(&addr); - memset((char *)&clock_stat, 0, sizeof clock_stat); + ZERO(addr); + ZERO(clock_stat); items = INFO_NITEMS(inpkt->err_nitems); cf = (struct conf_fudge *) inpkt->data; @@ -2616,7 +2593,7 @@ get_clkbug_info( return; } - memset((char *)&bug, 0, sizeof bug); + ZERO(bug); refclock_buginfo(&addr, &bug); if (bug.nvalues == 0 && bug.ntimes == 0) { req_ack(srcadr, inter, inpkt, INFO_ERR_NODATA); @@ -2657,7 +2634,7 @@ fill_info_if_stats(void *data, interface_info_t *interface_info) struct info_if_stats *ifs = *ifsp; endpt *ep = interface_info->ep; - memset(ifs, 0, sizeof(*ifs)); + ZERO(*ifs); if (IS_IPV6(&ep->sin)) { if (!client_v6_capable) { diff --git a/ntpd/ntp_restrict.c b/ntpd/ntp_restrict.c index 22cb40154..198511037 100644 --- a/ntpd/ntp_restrict.c +++ b/ntpd/ntp_restrict.c @@ -166,8 +166,7 @@ alloc_res4(void) if (res != NULL) return res; - rl = emalloc(count * cb); - memset(rl, 0, count * cb); + rl = emalloc_zero(count * cb); /* link all but the first onto free list */ res = (void *)((char *)rl + (count - 1) * cb); for (i = count - 1; i > 0; i--) { @@ -193,8 +192,7 @@ alloc_res6(void) if (res != NULL) return res; - rl = emalloc(count * cb); - memset(rl, 0, count * cb); + rl = emalloc_zero(count * cb); /* link all but the first onto free list */ res = (void *)((char *)rl + (count - 1) * cb); for (i = count - 1; i > 0; i--) { @@ -228,10 +226,10 @@ free_res( NTP_INSIST(unlinked == res); if (v6) { - memset(res, 0, V6_SIZEOF_RESTRICT_U); + zero_mem(res, V6_SIZEOF_RESTRICT_U); plisthead = &resfree6; } else { - memset(res, 0, V4_SIZEOF_RESTRICT_U); + zero_mem(res, V4_SIZEOF_RESTRICT_U); plisthead = &resfree4; } LINK_SLIST(*plisthead, res, link); @@ -504,7 +502,7 @@ hack_restrict( return; } - memset(&match, 0, sizeof(match)); + ZERO(match); /* silence VC9 potentially uninit warnings */ res = NULL; v6 = 0; diff --git a/ntpd/ntp_signd.c b/ntpd/ntp_signd.c index 3beb03be5..ec29863a5 100644 --- a/ntpd/ntp_signd.c +++ b/ntpd/ntp_signd.c @@ -33,14 +33,14 @@ static int ux_socket_connect(const char *name) { int fd; - struct sockaddr_un addr; + struct sockaddr_un addr; if (!name) { return -1; } - memset(&addr, 0, sizeof(addr)); - addr.sun_family = AF_UNIX; - strncpy(addr.sun_path, name, sizeof(addr.sun_path)); + ZERO(addr); + addr.sun_family = AF_UNIX; + strncpy(addr.sun_path, name, sizeof(addr.sun_path)); fd = socket(AF_UNIX, SOCK_STREAM, 0); if (fd == -1) { @@ -163,7 +163,7 @@ send_via_ntp_signd( char *reply = NULL; uint32_t reply_len; - memset(&samba_pkt, 0, sizeof(samba_pkt)); + ZERO(samba_pkt); samba_pkt.op = 0; /* Sign message */ /* This will be echoed into the reply - a different * impelementation might want multiple packets diff --git a/ntpd/ntp_util.c b/ntpd/ntp_util.c index d63156d11..d5c65d512 100644 --- a/ntpd/ntp_util.c +++ b/ntpd/ntp_util.c @@ -1020,6 +1020,8 @@ getauthkeys( "ExpandEnvironmentStrings(KEY_FILE) failed: %m"); strncpy(key_file_name, keyfile, _MAX_PATH); } + key_file_name = erealloc(key_file_name, + 1 + strlen(key_file_name)); #endif /* SYS_WINNT */ authreadkeys(key_file_name); diff --git a/ntpd/ntpsim.c b/ntpd/ntpsim.c index 9ef3bf69b..f936238f8 100644 --- a/ntpd/ntpsim.c +++ b/ntpd/ntpsim.c @@ -238,8 +238,8 @@ int simulate_server( double d1, d2, d3; /* Delays while the packet is enroute */ double t1, t2, t3, t4; /* The four timestamps in the packet */ - memset(&xpkt, 0, sizeof(xpkt)); - memset(&rbuf, 0, sizeof(rbuf)); + ZERO(xpkt); + ZERO(rbuf); /* Search for the server with the desired address */ server = NULL; diff --git a/ntpdate/ntpdate.c b/ntpdate/ntpdate.c index 50e4c7e01..98f14b8a8 100644 --- a/ntpdate/ntpdate.c +++ b/ntpdate/ntpdate.c @@ -1369,7 +1369,7 @@ addserver( strncpy(service, "ntp", sizeof(service)); /* Get host address. Looking for UDP datagram connection. */ - memset(&hints, 0, sizeof(hints)); + ZERO(hints); hints.ai_family = ai_fam_templ; hints.ai_socktype = SOCK_DGRAM; @@ -1406,8 +1406,7 @@ addserver( /* We must get all returned server in case the first one fails */ for (ptr = addrResult; ptr != NULL; ptr = ptr->ai_next) { if (is_reachable ((sockaddr_u *)ptr->ai_addr)) { - server = emalloc(sizeof(*server)); - memset(server, 0, sizeof(*server)); + server = emalloc_zero(sizeof(*server)); memcpy(&server->srcadr, ptr->ai_addr, ptr->ai_addrlen); server->event_time = ++sys_numservers; @@ -1417,7 +1416,8 @@ addserver( struct server *sp; for (sp = sys_servers; sp->next_server != NULL; - sp = sp->next_server) ; + sp = sp->next_server) + /* empty */; sp->next_server = server; } } @@ -1463,15 +1463,15 @@ findserver( complete_servers++; } - server = emalloc(sizeof(*server)); - memset(server, 0, sizeof(*server)); + server = emalloc_zero(sizeof(*server)); server->srcadr = *addr; server->event_time = ++sys_numservers; for (sp = sys_servers; sp->next_server != NULL; - sp = sp->next_server) ; + sp = sp->next_server) + /* empty */; sp->next_server = server; transmit(server); } @@ -1701,7 +1701,7 @@ init_io(void) /* * Init hints addrinfo structure */ - memset(&hints, 0, sizeof(hints)); + ZERO(hints); hints.ai_family = ai_fam_templ; hints.ai_flags = AI_PASSIVE; hints.ai_socktype = SOCK_DGRAM; diff --git a/ntpdc/ntpdc.c b/ntpdc/ntpdc.c index 46ae695af..a2c8fd9f0 100644 --- a/ntpdc/ntpdc.c +++ b/ntpdc/ntpdc.c @@ -427,7 +427,7 @@ openhost( * give it an IPv4 address to lookup. */ strncpy(service, "ntp", sizeof(service)); - memset((char *)&hints, 0, sizeof(struct addrinfo)); + ZERO(hints); hints.ai_family = ai_fam_templ; hints.ai_protocol = IPPROTO_UDP; hints.ai_socktype = SOCK_DGRAM; @@ -573,8 +573,11 @@ sendpkt( static void growpktdata(void) { + size_t priorsz; + + priorsz = (size_t)pktdatasize; pktdatasize += INCDATASIZE; - pktdata = erealloc(pktdata, (size_t)pktdatasize); + pktdata = erealloc_zero(pktdata, (size_t)pktdatasize, priorsz); } @@ -621,7 +624,7 @@ getresponse( numrecv = 0; *rdata = datap = pktdata; lastseq = 999; /* too big to be a sequence number */ - memset(haveseq, 0, sizeof(haveseq)); + ZERO(haveseq); FD_ZERO(&fds); again: @@ -798,7 +801,7 @@ getresponse( for (i = 0; i < items; i++) { memcpy(datap, tmp_data, (unsigned)size); tmp_data += size; - memset(datap + size, 0, pad); + zero_mem(datap + size, pad); datap += size + pad; } @@ -861,8 +864,7 @@ sendrequest( int maclen; char * pass; - memset(&qpkt, 0, sizeof(qpkt)); - + ZERO(qpkt); qpkt.rm_vn_mode = RM_VN_MODE(0, 0, 0); qpkt.implementation = (u_char)implcode; qpkt.request = (u_char)reqcode; @@ -1346,8 +1348,7 @@ getarg( char *cp, *np; static const char *digits = "0123456789"; - memset(argp, 0, sizeof(*argp)); - + ZERO(*argp); argp->string = str; argp->type = code & ~OPT; @@ -1429,7 +1430,7 @@ getnetnum( struct addrinfo hints, *ai = NULL; sockaddr_len = SIZEOF_SOCKADDR(af); - memset((char *)&hints, 0, sizeof(struct addrinfo)); + ZERO(hints); hints.ai_flags = AI_CANONNAME; #ifdef AI_ADDRCONFIG hints.ai_flags |= AI_ADDRCONFIG; diff --git a/ntpdc/ntpdc_ops.c b/ntpdc/ntpdc_ops.c index 9f31e565b..62ccce034 100644 --- a/ntpdc/ntpdc_ops.c +++ b/ntpdc/ntpdc_ops.c @@ -247,7 +247,7 @@ struct xcmd opcmds[] = { */ #define SET_ADDR(address, v6flag, v4addr, v6addr) \ do { \ - memset(&(address), 0, sizeof(address)); \ + ZERO(address); \ if (v6flag) { \ AF(&(address)) = AF_INET6; \ SOCK_ADDR6(&(address)) = (v6addr); \ @@ -265,8 +265,8 @@ do { \ */ #define SET_ADDRS(a1, a2, info, a1prefix, a2prefix) \ do { \ - memset(&(a1), 0, sizeof(a1)); \ - memset(&(a2), 0, sizeof(a2)); \ + ZERO(a1); \ + ZERO(a2); \ if ((info)->v6_flag) { \ AF(&(a1)) = AF_INET6; \ AF(&(a2)) = AF_INET6; \ @@ -283,31 +283,6 @@ do { \ } while (0) -/* - * SET_ADDRS - setup source and destination addresses for - * v4/v6 as needed - */ -#if 0 -#define SET_ADDR_MASK(address, addrmask, info) \ -do { \ - memset(&(address), 0, sizeof(address)); \ - memset(&(mask), 0, sizeof(mask)); \ - if ((info)->v6_flag) { \ - AF(&(address)) = AF_INET6; \ - AF(&(addrmask)) = AF_INET6; \ - SOCK_ADDR6(&(address)) = (info)->addr6; \ - SOCK_ADDR6(&(addrmask)) = (info)->mask6; \ - } else { \ - AF(&(address)) = AF_INET; \ - AF(&(addrmask)) = AF_INET; \ - NSRCADR(&(address)) = (info)->addr; \ - NSRCADR(&(addrmask)) = (info)->mask; \ - } \ - SET_SS_LEN_IF_PRESENT(&(address)); \ - SET_SS_LEN_IF_PRESENT(&(addrmask)); \ -} while (0) -#endif - /* * checkitems - utility to print a message if no items were returned */ @@ -791,7 +766,7 @@ again: else sendsize = v4sizeof(struct info_peer_list); - memset(plist, 0, sizeof(plist)); + ZERO(plist); qitemlim = min(pcmd->nargs, COUNTOF(plist)); for (qitems = 0, pl = plist; qitems < qitemlim; qitems++) { @@ -1448,7 +1423,7 @@ again: if (res) return; - memset(&cpeer, 0, sizeof(cpeer)); + ZERO(cpeer); if (IS_IPV4(&pcmd->argval[0].netnum)) { cpeer.peeraddr = NSRCADR(&pcmd->argval[0].netnum); @@ -2381,9 +2356,8 @@ traps( int res; again: - res = doquery(impl_ver, REQ_TRAPS, 0, 0, 0, (char *)NULL, - &items, &itemsize, (void *)&it, 0, - sizeof(struct info_trap)); + res = doquery(impl_ver, REQ_TRAPS, 0, 0, 0, NULL, &items, + &itemsize, (void *)&it, 0, sizeof(*it)); if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) { impl_ver = IMPL_XNTPD_OLD; @@ -2391,39 +2365,38 @@ again: } if (res != 0) - return; + return; if (!checkitems(items, fp)) - return; + return; if (!checkitemsize(itemsize, sizeof(struct info_trap)) && !checkitemsize(itemsize, v4sizeof(struct info_trap))) - return; + return; for (i = 0; i < items; i++ ) { - if (i != 0) - (void) fprintf(fp, "\n"); SET_ADDRS(trap_addr, local_addr, it, trap_address, local_address); - (void) fprintf(fp, "address %s, port %d\n", - stoa(&trap_addr), - ntohs(it->trap_port)); - (void) fprintf(fp, "interface: %s, ", - (it->local_address == 0) - ? "wildcard" - : stoa(&local_addr)); + fprintf(fp, "%saddress %s, port %d\n", + (0 == i) + ? "" + : "\n", + stoa(&trap_addr), ntohs(it->trap_port)); + fprintf(fp, "interface: %s, ", + (0 == it->local_address) + ? "wildcard" + : stoa(&local_addr)); if (ntohl(it->flags) & TRAP_CONFIGURED) - (void) fprintf(fp, "configured\n"); + fprintf(fp, "configured\n"); else if (ntohl(it->flags) & TRAP_NONPRIO) - (void) fprintf(fp, "low priority\n"); + fprintf(fp, "low priority\n"); else - (void) fprintf(fp, "normal priority\n"); + fprintf(fp, "normal priority\n"); - (void) fprintf(fp, "set for %ld secs, last set %ld secs ago\n", - (long)ntohl(it->origtime), - (long)ntohl(it->settime)); - (void) fprintf(fp, "sequence %d, number of resets %ld\n", - ntohs(it->sequence), - (long)ntohl(it->resets)); + fprintf(fp, "set for %ld secs, last set %ld secs ago\n", + (long)ntohl(it->origtime), + (long)ntohl(it->settime)); + fprintf(fp, "sequence %d, number of resets %ld\n", + ntohs(it->sequence), (long)ntohl(it->resets)); } } @@ -2765,7 +2738,7 @@ fudge( err = 0; - memset((char *)&fudgedata, 0, sizeof fudgedata); + ZERO(fudgedata); fudgedata.clockadr = NSRCADR(&pcmd->argval[0].netnum); if (STREQ(pcmd->argval[1].string, "time1")) { diff --git a/ntpq/ntpq-subs.c b/ntpq/ntpq-subs.c index a977a2e89..556edbac7 100644 --- a/ntpq/ntpq-subs.c +++ b/ntpq/ntpq-subs.c @@ -563,19 +563,18 @@ doprintvlist( FILE *fp ) { - register struct varlist *vl; + size_t n; - if (vlist->name == 0) { - (void) fprintf(fp, "No variables on list\n"); - } else { - for (vl = vlist; vl < vlist + MAXLIST && vl->name != 0; vl++) { - if (vl->value == 0) { - (void) fprintf(fp, "%s\n", vl->name); - } else { - (void) fprintf(fp, "%s=%s\n", - vl->name, vl->value); - } - } + if (NULL == vlist->name) { + fprintf(fp, "No variables on list\n"); + return; + } + for (n = 0; n < MAXLIST && vlist[n].name != NULL; n++) { + if (NULL == vlist[n].value) + fprintf(fp, "%s\n", vlist[n].name); + else + fprintf(fp, "%s=%s\n", vlist[n].name, + vlist[n].value); } } @@ -782,7 +781,7 @@ readvar( else if ((associd = checkassocid(pcmd->argval[0].uval)) == 0) return; - memset(tmplist, 0, sizeof(tmplist)); + ZERO(tmplist); if (pcmd->nargs > 1) { tmpcount = pcmd->nargs - 1; for (u = 0; u < tmpcount; u++) @@ -821,7 +820,7 @@ writevar( else if ((associd = checkassocid(pcmd->argval[0].uval)) == 0) return; - memset((char *)tmplist, 0, sizeof(tmplist)); + ZERO(tmplist); doaddvlist(tmplist, pcmd->argval[1].string); res = doquerylist(tmplist, CTL_OP_WRITEVAR, associd, 1, &rstatus, @@ -890,7 +889,7 @@ clockvar( else if ((associd = checkassocid(pcmd->argval[0].uval)) == 0) return; - memset(tmplist, 0, sizeof(tmplist)); + ZERO(tmplist); if (pcmd->nargs >= 2) doaddvlist(tmplist, pcmd->argval[1].string); @@ -999,7 +998,7 @@ mreadvar( return; if (pcmd->nargs >= 3) { - memset(tmplist, 0, sizeof(tmplist)); + ZERO(tmplist); doaddvlist(tmplist, pcmd->argval[2].string); pvars = tmplist; } else { @@ -1571,10 +1570,10 @@ doprintpeers( ZERO_SOCK(&srcadr); ZERO_SOCK(&dstadr); clock_name[0] = '\0'; - memset(&estoffset, 0, sizeof(estoffset)); - memset(&estdelay, 0, sizeof(estdelay)); - memset(&estjitter, 0, sizeof(estjitter)); - memset(&estdisp, 0, sizeof(estdisp)); + ZERO(estoffset); + ZERO(estdelay); + ZERO(estjitter); + ZERO(estdisp); while (nextvar(&datalen, &data, &name, &value)) { if (!strcmp("srcadr", name) || @@ -2180,7 +2179,7 @@ add_mru( mon = emalloc(sizeof(*mon)); mru_count++; } - memset(mon, 0, sizeof(*mon)); + ZERO(*mon); return mon; } @@ -2250,8 +2249,7 @@ collect_mru_list( INIT_DLIST(mru_list, mlink); cb = NTP_HASH_SIZE * sizeof(*hash_table); NTP_INSIST(NULL == hash_table); - hash_table = emalloc(cb); - memset(hash_table, 0, cb); + hash_table = emalloc_zero(cb); c_mru_l_rc = FALSE; list_complete = FALSE; @@ -2259,10 +2257,9 @@ collect_mru_list( got = 0; ri = 0; cb = sizeof(*mon); - mon = emalloc(cb); - memset(mon, 0, cb); - memset(pnow, 0, sizeof(*pnow)); - memset(&last_older, 0, sizeof(last_older)); + mon = emalloc_zero(cb); + ZERO(*pnow); + ZERO(last_older); limit = min(3 * MAXFRAGS, ntpd_row_limit); snprintf(req_buf, sizeof(req_buf), "nonce=%s, limit=%d%s", @@ -2768,7 +2765,7 @@ mrulist( const char laddr_eq[] = "laddr="; const char sort_eq[] = "sort="; mru_sort_order order; - const char * const *ppkeyword; + size_t n; char parms_buf[128]; char buf[24]; char *parms; @@ -2808,16 +2805,14 @@ mrulist( } else if (!strncmp(sort_eq, arg, sizeof(sort_eq) - 1)) { arg += sizeof(sort_eq) - 1; - for (ppkeyword = mru_sort_keywords; - ppkeyword < mru_sort_keywords + - COUNTOF(mru_sort_keywords); - ppkeyword++) - if (!strcmp(*ppkeyword, arg)) + for (n = 0; + n < COUNTOF(mru_sort_keywords); + n++) + if (!strcmp(mru_sort_keywords[n], + arg)) break; - if (ppkeyword < mru_sort_keywords + - COUNTOF(mru_sort_keywords)) - order = ppkeyword - - mru_sort_keywords; + if (n < COUNTOF(mru_sort_keywords)) + order = n; } else if (!strcmp("limited", arg) || !strcmp("kod", arg)) { /* transform to resany=... */ @@ -2984,7 +2979,7 @@ another_ifstats_field( fprintf(fp, " %s\n", sptoa(&prow->bcast)); *pfields = 0; - memset(prow, 0, sizeof(*prow)); + ZERO(*prow); } @@ -3033,7 +3028,7 @@ ifstats( "==============================================================================\n"); /* '=' x 78 */ - memset(&row, 0, sizeof(row)); + ZERO(row); fields = 0; ifnum = 0; ui = 0; @@ -3160,9 +3155,9 @@ collect_display_vdc( u_long ul; int vtype; - memset(vl, 0, sizeof(vl)); + ZERO(vl); for (pvdc = table; pvdc->tag != NULL; pvdc++) { - memset(&pvdc->v, 0, sizeof(pvdc->v)); + ZERO(pvdc->v); if (NTP_ADD != pvdc->type) { doaddvlist(vl, pvdc->tag); } else { diff --git a/ntpq/ntpq.c b/ntpq/ntpq.c index d98757710..c6af615d5 100644 --- a/ntpq/ntpq.c +++ b/ntpq/ntpq.c @@ -545,7 +545,7 @@ openhost( * will return an "IPv4-mapped IPv6 address" address if you * give it an IPv4 address to lookup. */ - memset(&hints, 0, sizeof(hints)); + ZERO(hints); hints.ai_family = ai_fam_templ; hints.ai_protocol = IPPROTO_UDP; hints.ai_socktype = SOCK_DGRAM; @@ -1701,7 +1701,7 @@ getnetnum( { struct addrinfo hints, *ai = NULL; - memset(&hints, 0, sizeof(hints)); + ZERO(hints); hints.ai_flags = AI_CANONNAME; #ifdef AI_ADDRCONFIG hints.ai_flags |= AI_ADDRCONFIG; @@ -2718,6 +2718,8 @@ nextvar( const char *cp; char *np; const char *cpend; + size_t srclen; + size_t len; char *npend; /* character after last */ int quoted = 0; static char name[MAXVARLEN]; @@ -2731,39 +2733,32 @@ nextvar( */ while (cp < cpend && (*cp == ',' || isspace((int)*cp))) cp++; - if (cp == cpend) + if (cp >= cpend) return 0; /* * Copy name until we hit a ',', an '=', a '\r' or a '\n'. Backspace * over any white space and terminate it. */ - np = name; - npend = &name[MAXVARLEN]; - while (cp < cpend && np < npend && *cp != ',' && *cp != '=' - && *cp != '\r' && *cp != '\n') - *np++ = *cp++; - /* - * Check if we ran out of name space, without reaching the end or a - * terminating character - */ - if (np == npend && !(cp == cpend || *cp == ',' || *cp == '=' || - *cp == '\r' || *cp == '\n')) - return 0; - while (isspace((int)(*(np-1)))) - np--; - *np = '\0'; + srclen = strcspn(cp, ",=\r\n"); + len = srclen; + while (len > 0 && isspace(cp[len - 1])) + len--; + if (len > 0) + memcpy(name, cp, len); + name[len] = '\0'; *vname = name; + cp += srclen; /* * Check if we hit the end of the buffer or a ','. If so we are done. */ - if (cp == cpend || *cp == ',' || *cp == '\r' || *cp == '\n') { - if (cp != cpend) - cp++; + if (cp >= cpend || *cp == ',' || *cp == '\r' || *cp == '\n') { + if (cp < cpend) + cp++; *datap = cp; *datalen = cpend - cp; - *vvalue = (char *)0; + *vvalue = NULL; return 1; } @@ -2772,7 +2767,7 @@ nextvar( */ cp++; /* past '=' */ while (cp < cpend && (isspace((int)*cp) && *cp != '\r' && *cp != '\n')) - cp++; + cp++; np = value; npend = &value[MAXVALLEN]; while (cp < cpend && np < npend && ((*cp != ',') || quoted)) @@ -2785,19 +2780,19 @@ nextvar( * or without finding a comma */ if (np == npend && (quoted || *cp != ',')) - return 0; + return 0; /* * Trim off any trailing whitespace */ while (np > value && isspace((int)(*(np-1)))) - np--; + np--; *np = '\0'; /* * Return this. All done. */ if (cp != cpend) - cp++; + cp++; *datap = cp; *datalen = cpend - cp; *vvalue = value; diff --git a/ports/winnt/include/config.h b/ports/winnt/include/config.h index 82428254a..ab08935e5 100644 --- a/ports/winnt/include/config.h +++ b/ports/winnt/include/config.h @@ -45,11 +45,13 @@ * at exit time (currently only if run under a debugger). */ #if defined(_MSC_VER) && defined(_DEBUG) -#define _CRTDBG_MAP_ALLOC -#include -#include -#include -/* #define MALLOC_LINT */ /* defers free() */ +# define _CRTDBG_MAP_ALLOC +# include +# include +# include +/* # define MALLOC_LINT */ /* defers free() */ +# define EREALLOC_IMPL(ptr, newsz, filenm, loc) \ + _realloc_dbg(ptr, newsz, _NORMAL_BLOCK, filenm, loc) #endif /* diff --git a/ports/winnt/libntp/termios.c b/ports/winnt/libntp/termios.c index 3a09608c9..c13ea4170 100644 --- a/ports/winnt/libntp/termios.c +++ b/ports/winnt/libntp/termios.c @@ -93,9 +93,8 @@ HANDLE common_serial_open( c_hnds = unit + 1; /* round up to closest multiple of 4 to avoid churn */ c_hnds = (c_hnds + 3) & ~3; - hnds = erealloc(hnds, c_hnds * sizeof(hnds[0])); - memset(&hnds[prev_c_hnds], 0, - (c_hnds - prev_c_hnds) * sizeof(hnds[0])); + hnds = erealloc_zero(hnds, c_hnds * sizeof(hnds[0]), + prev_c_hnds * sizeof(hnds[0])); } if (NULL == hnds[unit].h) { diff --git a/ports/winnt/ntpd/nt_clockstuff.c b/ports/winnt/ntpd/nt_clockstuff.c index fdf42bc2d..3eb6a0bbe 100644 --- a/ports/winnt/ntpd/nt_clockstuff.c +++ b/ports/winnt/ntpd/nt_clockstuff.c @@ -1302,8 +1302,8 @@ win_time_stepped(void) clock_backward_max = CLOCK_BACK_THRESHOLD; clock_backward_count = 0; newest_baseline = 0; - memset(baseline_counts, 0, sizeof(baseline_counts)); - memset(baseline_times, 0, sizeof(baseline_times)); + ZERO(baseline_counts); + ZERO(baseline_times); StartClockThread(); } diff --git a/ports/winnt/ntpd/ntp_iocompletionport.c b/ports/winnt/ntpd/ntp_iocompletionport.c index aa7798807..7821ffb42 100644 --- a/ports/winnt/ntpd/ntp_iocompletionport.c +++ b/ports/winnt/ntpd/ntp_iocompletionport.c @@ -113,8 +113,7 @@ GetHeapAlloc(char *fromfunc) HEAP_ZERO_MEMORY, sizeof(olplus)); #else - lpo = emalloc(sizeof(*lpo)); - memset(lpo, 0, sizeof(*lpo)); + lpo = emalloc_zero(sizeof(*lpo)); #endif DPRINTF(3, ("Allocation %d memory for %s, ptr %x\n", sizeof(olplus), fromfunc, lpo)); @@ -406,7 +405,7 @@ QueueSerialWait( lpo->recv_buf = buff; if (clear_timestamp) - memset(&buff->recv_time, 0, sizeof(buff->recv_time)); + ZERO(buff->recv_time); buff->fd = _get_osfhandle(rio->fd); if (!WaitCommEvent((HANDLE)buff->fd, diff --git a/ports/winnt/ntpd/ntservice.c b/ports/winnt/ntpd/ntservice.c index e438ef48e..63fa47345 100644 --- a/ports/winnt/ntpd/ntservice.c +++ b/ports/winnt/ntpd/ntservice.c @@ -264,7 +264,7 @@ void UpdateSCM(DWORD state) { if (state) dwState = state; - memset(&ss, 0, sizeof(SERVICE_STATUS)); + ZERO(ss); ss.dwServiceType |= SERVICE_WIN32_OWN_PROCESS; ss.dwCurrentState = dwState; ss.dwControlsAccepted = SERVICE_ACCEPT_STOP | diff --git a/sntp/configure.ac b/sntp/configure.ac index 1d4d50192..f09c72b7c 100644 --- a/sntp/configure.ac +++ b/sntp/configure.ac @@ -961,49 +961,10 @@ BINSUBDIR=$use_binsubdir AC_SUBST([BINSUBDIR]) AM_CONDITIONAL([NTP_BINSUBDIR_IS_BIN], [test "bin" = "$BINSUBDIR"]) -AC_CONFIG_FILES([Makefile]) - # HMS: if we don't find c++ we should not look for gtest. AC_PROG_CXX +NTP_GOOGLETEST -gta=false -AC_ARG_WITH([gtest], - [AS_HELP_STRING([--with-gtest], - [Use the gtest framework if it's available])], - [try_gtest=$withval], [try_gtest=yes]) - -case "$try_gtest" in - yes) - AC_PATH_PROG([GTEST_CONFIG], [gtest-config]) - case x${GTEST_CONFIG} in - x) ;; - *) - AC_MSG_CHECKING([gtest version]) - gtest_version=`$GTEST_CONFIG --version` - case "$gtest_version" in - 1.5.*) - GTEST_LDFLAGS=`$GTEST_CONFIG --ldflags` - GTEST_LIBS=`$GTEST_CONFIG --libs` - GTEST_CXXFLAGS=`$GTEST_CONFIG --cxxflags` - GTEST_CPPFLAGS=`$GTEST_CONFIG --cppflags` - AC_SUBST(GTEST_LDFLAGS) - AC_SUBST(GTEST_LIBS) - AC_SUBST(GTEST_CXXFLAGS) - AC_SUBST(GTEST_CPPFLAGS) - gta=true - ;; - esac - case "$gta" in - true) - AC_MSG_RESULT([($gtest_version) ok]) - ;; - *) AC_MSG_RESULT([($gtest_version) not ok]) - ;; - esac - esac -esac - -AM_CONDITIONAL([GTEST_AVAILABLE], [$gta]) - -AC_CONFIG_FILES(tests/Makefile) +AC_CONFIG_FILES([Makefile]) +AC_CONFIG_FILES([tests/Makefile]) AC_OUTPUT diff --git a/sntp/tests/Makefile.am b/sntp/tests/Makefile.am index 7cee30bd0..bd4092309 100644 --- a/sntp/tests/Makefile.am +++ b/sntp/tests/Makefile.am @@ -45,11 +45,11 @@ CLEANFILES = data/kod-output-multiple \ data/debug-output-lfp-hex \ $(NULL) -LDADD = @GTEST_LDFLAGS@ \ - @GTEST_LIBS@ \ - $(LIBOPTS_LDADD) \ +LDADD = $(LIBOPTS_LDADD) \ @top_builddir@/../libntp/libntp.a \ - @LCRYPTO@ \ + @LCRYPTO@ \ + @GTEST_LDFLAGS@ \ + @GTEST_LIBS@ \ $(sntp_SOURCES_USED) AM_CXXFLAGS = @GTEST_CXXFLAGS@