From: Dave Hart Date: Thu, 14 Apr 2011 05:30:10 +0000 (+0000) Subject: Log failure to fetch time from HOPF_P hardware. X-Git-Tag: NTP_4_2_7P151~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5768a15d4cacc27aa806e8e95b7e41832bafeea1;p=thirdparty%2Fntp.git Log failure to fetch time from HOPF_P hardware. Check HOPF_S sscanf() conversion count before converted values. Correct mvsnprintf() and mvfprintf() for vsnprintf() that handles %m. bk: 4da68662IeXVWWM1aJhv-GKoRH6mrw --- diff --git a/ChangeLog b/ChangeLog index f2e1ba486..8431a7e98 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,6 +3,8 @@ flock-build to avoid regressions in (v)snprintf() replacement. * More msnprintf() unit tests. * Coverity Scan error checking fixes. +* Log failure to fetch time from HOPF_P hardware. +* Check HOPF_S sscanf() conversion count before converted values. (4.2.7p150) 2011/04/13 Released by Harlan Stenn * Remove never-used, incomplete ports/winnt/ntpd/refclock_trimbledc.[ch] * On systems without C99-compliant (v)snprintf(), use C99-snprintf diff --git a/libntp/msyslog.c b/libntp/msyslog.c index 18814b5c9..fb6f80556 100644 --- a/libntp/msyslog.c +++ b/libntp/msyslog.c @@ -49,56 +49,7 @@ void format_errmsg (char *, size_t, const char *, int); # endif -/* - * errno_to_str() - a thread-safe strerror() replacement. - * Hides the varied signatures of strerror_r(). - * For Windows, we have: - * #define errno_to_str isc_strerror - */ -# ifndef errno_to_str -void -errno_to_str( - int err, - char * buf, - size_t bufsiz - ) -{ -# if defined(STRERROR_R_CHAR_P) || !HAVE_DECL_STRERROR_R - char * pstatic; - - buf[0] = '\0'; -# ifdef STRERROR_R_CHAR_P - /* - * For older GNU strerror_r, the return value either points to - * buf, or to static storage. We want the result always in buf - */ - pstatic = strerror_r(err, buf, bufsiz); -# else - pstatic = strerror(err); -# endif - if (NULL == pstatic && '\0' == buf[0]) - snprintf(buf, bufsiz, "%s(%d): errno %d", -# ifdef STRERROR_R_CHAR_P - "strerror_r", -# else - "strerror", -# endif - err, errno); - /* protect against believing an int return is a pointer */ - else if (pstatic != buf && pstatic > (char *)bufsiz) - strlcpy(buf, pstatic, bufsiz); -# else - int rc; - - rc = strerror_r(err, buf, bufsiz); - if (rc < 0) - snprintf(buf, bufsiz, "strerror_r(%d): errno %d", - err, errno); -# endif -} -# endif /* errno_to_str */ - - +/* format_errmsg() is under #ifndef VSNPRINTF_PERCENT_M above */ void format_errmsg( char * nfmt, @@ -141,6 +92,56 @@ format_errmsg( #endif /* VSNPRINTF_PERCENT_M */ +/* + * errno_to_str() - a thread-safe strerror() replacement. + * Hides the varied signatures of strerror_r(). + * For Windows, we have: + * #define errno_to_str isc_strerror + */ +#ifndef errno_to_str +void +errno_to_str( + int err, + char * buf, + size_t bufsiz + ) +{ +# if defined(STRERROR_R_CHAR_P) || !HAVE_DECL_STRERROR_R + char * pstatic; + + buf[0] = '\0'; +# ifdef STRERROR_R_CHAR_P + /* + * For older GNU strerror_r, the return value either points to + * buf, or to static storage. We want the result always in buf + */ + pstatic = strerror_r(err, buf, bufsiz); +# else + pstatic = strerror(err); +# endif + if (NULL == pstatic && '\0' == buf[0]) + snprintf(buf, bufsiz, "%s(%d): errno %d", +# ifdef STRERROR_R_CHAR_P + "strerror_r", +# else + "strerror", +# endif + err, errno); + /* protect against believing an int return is a pointer */ + else if (pstatic != buf && pstatic > (char *)bufsiz) + strlcpy(buf, pstatic, bufsiz); +# else + int rc; + + rc = strerror_r(err, buf, bufsiz); + if (rc < 0) + snprintf(buf, bufsiz, "strerror_r(%d): errno %d", + err, errno); +# endif +} +#endif /* errno_to_str */ + + /* * addto_syslog() * This routine adds the contents of a buffer to the syslog or an @@ -225,9 +226,11 @@ mvsnprintf( ) { #ifndef VSNPRINTF_PERCENT_M - char nfmt[256]; + char nfmt[256]; +#else + const char * nfmt = fmt; #endif - int errval; + int errval; /* * Save the error value as soon as possible @@ -255,9 +258,11 @@ mvfprintf( ) { #ifndef VSNPRINTF_PERCENT_M - char nfmt[256]; + char nfmt[256]; +#else + const char * nfmt = fmt; #endif - int errval; + int errval; /* * Save the error value as soon as possible diff --git a/ntpd/refclock_hopfpci.c b/ntpd/refclock_hopfpci.c index 11fc9a0bf..95abfe12d 100644 --- a/ntpd/refclock_hopfpci.c +++ b/ntpd/refclock_hopfpci.c @@ -194,7 +194,9 @@ hopfpci_poll( pp = peer->procptr; #ifndef SYS_WINNT - ioctl(fd,HOPF_CLOCK_GET_UTC,&m_time); + if (ioctl(fd, HOPF_CLOCK_GET_UTC, &m_time) < 0) + msyslog(LOG_ERR, "HOPF_P(%d): HOPF_CLOCK_GET_UTC: %m\n", + unit); #else GetHopfSystemTime(&m_time); #endif diff --git a/ntpd/refclock_hopfser.c b/ntpd/refclock_hopfser.c index 699f0e86f..6ae6d3cdb 100644 --- a/ntpd/refclock_hopfser.c +++ b/ntpd/refclock_hopfser.c @@ -215,10 +215,11 @@ hopfserial_receive ( struct refclockproc *pp; struct peer *peer; - int synch; /* synchhronization indicator */ - int DoW; /* Dow */ + int synch; /* synchhronization indicator */ + int DoW; /* Day of Week */ int day, month; /* ddd conversion */ + int converted; /* * Initialize pointers and read the timecode and timestamp. @@ -230,15 +231,15 @@ hopfserial_receive ( if (up->rpt_next == 0 ) return; - up->rpt_next = 0; /* wait until next poll interval occur */ - pp->lencode = (u_short)refclock_gtlin(rbufp, pp->a_lastcode, BMAX, &pp->lastrec); - - if (pp->lencode == 0) + pp->lencode = (u_short)refclock_gtlin(rbufp, pp->a_lastcode, + sizeof(pp->a_lastcode), + &pp->lastrec); + if (pp->lencode == 0) return; - sscanf(pp->a_lastcode, + converted = sscanf(pp->a_lastcode, #if 1 "%1x%1x%2d%2d%2d%2d%2d%2d", /* ...cr,lf */ #else @@ -258,9 +259,9 @@ hopfserial_receive ( Validate received values at least enough to prevent internal array-bounds problems, etc. */ - if((pp->hour < 0) || (pp->hour > 23) || - (pp->minute < 0) || (pp->minute > 59) || - (pp->second < 0) || (pp->second > 60) /*Allow for leap seconds.*/ || + if ((8 != converted) || (pp->hour < 0) || (pp->hour > 23) || + (pp->minute < 0) || (pp->minute > 59) || (pp->second < 0) || + (pp->second > 60) /*Allow for leap seconds.*/ || (day < 1) || (day > 31) || (month < 1) || (month > 12) || (pp->year < 0) || (pp->year > 99)) { diff --git a/sntp/crypto.c b/sntp/crypto.c index b427e1fab..aa8d91dd2 100644 --- a/sntp/crypto.c +++ b/sntp/crypto.c @@ -112,7 +112,7 @@ auth_init( key_cnt = 0; while (!feof(keyf)) { char * octothorpe; - struct key *act = emalloc(sizeof(struct key)); + struct key *act; int goodline = 0; if (NULL == fgets(kbuf, sizeof(kbuf), keyf)) @@ -122,6 +122,7 @@ auth_init( octothorpe = strchr(kbuf, '#'); if (octothorpe) *octothorpe = '\0'; + act = emalloc(sizeof(*act)); scan_cnt = sscanf(kbuf, "%d %9s %128s", &act->key_id, act->type, keystring); if (scan_cnt == 3) { int len = strlen(keystring); diff --git a/sntp/main.c b/sntp/main.c index f191476b2..e65c9bbe2 100644 --- a/sntp/main.c +++ b/sntp/main.c @@ -498,11 +498,6 @@ sntp_name_resolved( break; } - spkt = emalloc_zero(sizeof(*spkt)); - spkt->dctx = dctx; - octets = min(ai->ai_addrlen, sizeof(spkt->addr)); - memcpy(&spkt->addr, ai->ai_addr, octets); - /* ** We're waiting for a response for either unicast ** or broadcast, so... @@ -510,8 +505,13 @@ sntp_name_resolved( ++n_pending_ntp; /* If this is for a unicast IP, queue a request */ - if (dctx->flags & CTX_UCST) + if (dctx->flags & CTX_UCST) { + spkt = emalloc_zero(sizeof(*spkt)); + spkt->dctx = dctx; + octets = min(ai->ai_addrlen, sizeof(spkt->addr)); + memcpy(&spkt->addr, ai->ai_addr, octets); queue_xmt(sock, dctx, spkt, xmt_delay); + } } } /* n_pending_dns really should be >0 here... */ @@ -819,7 +819,7 @@ sock_cb( } /* Read in the packet */ - rpktl = recvdata(fd, &sender, &r_pkt, sizeof(rbuf)); + rpktl = recvdata(fd, &sender, &rbuf, sizeof(rbuf)); if (rpktl < 0) { msyslog(LOG_DEBUG, "recvfrom error %m"); return;