]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
Remove nearly all strcpy() and most strcat() from NTP distribution.
authorDave Hart <hart@ntp.org>
Sun, 2 Jan 2011 05:13:56 +0000 (05:13 +0000)
committerDave Hart <hart@ntp.org>
Sun, 2 Jan 2011 05:13:56 +0000 (05:13 +0000)
  One major pocket remains in ntp_crypto.c.  libopts & libisc also have
  (safe) uses of strcpy() and strcat() remaining

bk: 4d200994pfUzhN4SQzXt1XJxF9q11A

20 files changed:
include/ntpd.h
libntp/findconfig.c
libntp/statestr.c
libntp/systime.c
ntpd/ntp_control.c
ntpd/ntp_peer.c
ntpd/ntp_util.c
ntpd/refclock_arbiter.c
ntpd/refclock_as2201.c
ntpd/refclock_chu.c
ntpd/refclock_hpgps.c
ntpd/refclock_neoclock4x.c
ntpd/refclock_oncore.c
ntpd/refclock_parse.c
ntpd/refclock_true.c
ntpdate/ntpdate.c
ntpdc/ntpdc.c
ntpdc/ntpdc_ops.c
ntpq/ntpq.c
ports/winnt/libntp/randfile.c

index ddbb8c7028760b0fea78c515be03f2961484415b..1ad147e9f6a0c5adcce23cfe2e8058da551fa22c 100644 (file)
@@ -46,6 +46,8 @@ extern        int     change_logfile  (const char *, int);
 extern void    setup_logfile   (int);
 extern void    process_control (struct recvbuf *, int);
 extern void    report_event    (int, struct peer *, const char *);
+extern int     mprintf_event   (int, struct peer *, const char *, ...)
+                       __attribute__((__format__(__printf__, 3, 4)));
 
 /* ntp_control.c */
 /*
index 562c01764c3c18cbf6d6445d3ddb570b143dae5b..44e6b07ed624a0d0e092116a751e16f9ab6b2a45 100644 (file)
@@ -22,7 +22,7 @@ FindConfig(
        struct utsname unamebuf; 
 
        /* All keyed by initial target being a directory */
-       (void) strcpy(result, base);
+       strncpy(result, base, sizeof(result));
        if (stat(result, &sbuf) == 0) {
                if (S_ISDIR(sbuf.st_mode)) {
 
@@ -49,7 +49,9 @@ FindConfig(
                                                if (stat(result, &sbuf) == 0) {
                                                        goto outahere;
                                                } else {
-                                                       (void) strcpy(result, "/not/found");
+                                                       strncpy(result,
+                                                               "/not/found",
+                                                               sizeof(result));
                                                }
                                        }
                                }
index 0dbf2abc01e066026c08ed31c045cee035965361..71dc7cad686bb5f212a3a741aa2c0ee822d61b52 100644 (file)
  */
 struct codestring {
        int code;
-       const char *string;
+       const char * const string;
 };
 
 /*
  * Leap status (leap)
  */
-static
-struct codestring leap_codes[] = {
+static const struct codestring leap_codes[] = {
        { LEAP_NOWARNING,       "leap_none" },
        { LEAP_ADDSECOND,       "leap_add_sec" },
        { LEAP_DELSECOND,       "leap_del_sec" },
@@ -36,8 +35,7 @@ struct codestring leap_codes[] = {
 /*
  * Clock source status (sync)
  */
-static
-struct codestring sync_codes[] = {
+static const struct codestring sync_codes[] = {
        { CTL_SST_TS_UNSPEC,    "sync_unspec" },
        { CTL_SST_TS_ATOM,      "sync_pps" },
        { CTL_SST_TS_LF,        "sync_lf_radio" },
@@ -54,8 +52,7 @@ struct codestring sync_codes[] = {
 /*
  * Peer selection status (sel)
  */
-static
-struct codestring select_codes[] = {
+static const struct codestring select_codes[] = {
        { CTL_PST_SEL_REJECT,   "sel_reject" },
        { CTL_PST_SEL_SANE,     "sel_falsetick" },
        { CTL_PST_SEL_CORRECT,  "sel_excess" },
@@ -70,8 +67,7 @@ struct codestring select_codes[] = {
 /*
  * Clock status (clk)
  */
-static
-struct codestring clock_codes[] = {
+static const struct codestring clock_codes[] = {
        { CTL_CLK_OKAY,         "clk_unspec" },
        { CTL_CLK_NOREPLY,      "clk_no_reply" },
        { CTL_CLK_BADFORMAT,    "clk_bad_format" },
@@ -87,8 +83,7 @@ struct codestring clock_codes[] = {
 /*
  * Flash bits -- see ntpq.c tstflags & tstflagnames
  */
-static
-struct codestring flash_codes[] = {
+static const struct codestring flash_codes[] = {
        { TEST1,                "pkt_dup" },
        { TEST2,                "pkt_bogus" },
        { TEST3,                "pkt_unsync" },
@@ -110,8 +105,7 @@ struct codestring flash_codes[] = {
 /*
  * System events (sys)
  */
-static
-struct codestring sys_codes[] = {
+static const struct codestring sys_codes[] = {
        { EVNT_UNSPEC,          "unspecified" },
        { EVNT_NSET,            "freq_not_set" },
        { EVNT_FSET,            "freq_set" },
@@ -135,8 +129,7 @@ struct codestring sys_codes[] = {
 /*
  * Peer events (peer)
  */
-static
-struct codestring peer_codes[] = {
+static const struct codestring peer_codes[] = {
        { PEVNT_MOBIL & ~PEER_EVENT,    "mobilize" },
        { PEVNT_DEMOBIL & ~PEER_EVENT,  "demobilize" },
        { PEVNT_UNREACH & ~PEER_EVENT,  "unreachable" },
@@ -159,8 +152,7 @@ struct codestring peer_codes[] = {
 /*
  * Crypto events (cryp)
  */
-static
-struct codestring crypto_codes[] = {
+static const struct codestring crypto_codes[] = {
        { XEVNT_OK & ~CRPT_EVENT,       "success" },
        { XEVNT_LEN & ~CRPT_EVENT,      "bad_field_format_or_length" },
        { XEVNT_TSP & ~CRPT_EVENT,      "bad_timestamp" },
@@ -182,26 +174,30 @@ struct codestring crypto_codes[] = {
 #endif /* AUTOKEY */
 
 /* Forwards */
-static const char *getcode (int, struct codestring *);
-static const char *getevents (int);
+static const char *    getcode(int, const struct codestring *);
+static const char *    getevents(int);
+static const char *    peer_st_flags(u_char pst);
 
 /*
  * getcode - return string corresponding to code
  */
 static const char *
 getcode(
-       int code,
-       struct codestring *codetab
+       int                             code,
+       const struct codestring *       codetab
        )
 {
-       static char buf[30];
+       char *  buf;
 
        while (codetab->code != -1) {
                if (codetab->code == code)
                        return codetab->string;
                codetab++;
        }
-       snprintf(buf, sizeof(buf), "%s_%d", codetab->string, code);
+
+       LIB_GETBUF(buf);
+       snprintf(buf, LIB_BUFLENGTH, "%s_%d", codetab->string, code);
+
        return buf;
 }
 
@@ -213,15 +209,60 @@ getevents(
        int cnt
        )
 {
-       static char buf[20];
+       char *  buf;
 
        if (cnt == 0)
                return "no events";
-       snprintf(buf, sizeof(buf), "%d event%s", cnt, (cnt==1) ? "" : 
-           "s");
+
+       LIB_GETBUF(buf);
+       snprintf(buf, LIB_BUFLENGTH, "%d event%s", cnt,
+                (1 == cnt)
+                    ? ""
+                    : "s");
+
        return buf;
 }
 
+
+static const char *
+peer_st_flags(u_char pst)
+{
+       static const char       toosmall[] = "buffer too small!";
+       static const char       csp[] = ", ";
+       const char *            sep;
+       char *                  buf;
+       char *                  pch;
+       char *                  lim;
+       int                     rc;
+
+       LIB_GETBUF(buf);
+       pch = buf;
+       lim = buf + LIB_BUFLENGTH;
+       sep = "";
+
+#define EXPAND_PEERST_BIT(b, text)                             \
+do {                                                           \
+       if ((b) & pst) {                                        \
+               rc = snprintf(pch, (lim - pch), "%s" text, sep);\
+               if (rc < 0)                                     \
+                       return toosmall;                        \
+               pch += (u_int)rc;                               \
+               if (pch >= lim)                                 \
+                       return toosmall;                        \
+               sep = csp;                                      \
+       }                                                       \
+} while (0)
+
+       EXPAND_PEERST_BIT(CTL_PST_CONFIG,       "conf");
+       EXPAND_PEERST_BIT(CTL_PST_AUTHENABLE,   "authenb");
+       EXPAND_PEERST_BIT(CTL_PST_AUTHENTIC,    "auth");
+       EXPAND_PEERST_BIT(CTL_PST_REACH,        "reach");
+       EXPAND_PEERST_BIT(CTL_PST_BCAST,        "bcast");
+
+       return buf;
+}
+
+
 /*
  * statustoa - return a descriptive string for a peer status
  */
@@ -231,78 +272,43 @@ statustoa(
        int st
        )
 {
-       char *cb;
-       u_char pst;
+       char *  cb;
+       char *  cc;
+       u_char  pst;
 
        LIB_GETBUF(cb);
 
        switch (type) {
 
        case TYPE_SYS:
-               strcpy(cb, getcode(CTL_SYS_LI(st), leap_codes));
-               strcat(cb, ", ");
-               strcat(cb, getcode(CTL_SYS_SOURCE(st), sync_codes));
-               strcat(cb, ", ");
-               strcat(cb, getevents(CTL_SYS_NEVNT(st)));
-               strcat(cb, ", ");
-               strcat(cb, getcode(CTL_SYS_EVENT(st), sys_codes));
+               snprintf(cb, LIB_BUFLENGTH, "%s, %s, %s, %s",
+                        getcode(CTL_SYS_LI(st), leap_codes),
+                        getcode(CTL_SYS_SOURCE(st), sync_codes),
+                        getevents(CTL_SYS_NEVNT(st)),
+                        getcode(CTL_SYS_EVENT(st), sys_codes));
                break;
        
        case TYPE_PEER:
-               /*
-                * Handcraft the bits
-                */
-               pst = (u_char) CTL_PEER_STATVAL(st);
-               if (pst & CTL_PST_CONFIG)
-                       strcpy(cb, "conf");
-               if (pst & CTL_PST_AUTHENABLE) {
-                       if (pst & CTL_PST_CONFIG)
-                               strcat(cb, ", authenb");
-                       else
-                               strcat(cb, "authenb");
-               }
-               if (pst & CTL_PST_AUTHENTIC) {
-                       if (pst & (CTL_PST_CONFIG | CTL_PST_AUTHENABLE))
-                               strcat(cb, ", auth");
-                       else
-                               strcat(cb, "auth");
-               }
-               if (pst & CTL_PST_REACH) {
-                       if (pst & (CTL_PST_CONFIG | CTL_PST_AUTHENABLE |
-                           CTL_PST_AUTHENTIC))
-                               strcat(cb, ", reach");
-                       else
-                               strcat(cb, "reach");
-               }
-               if (pst & CTL_PST_BCAST) {
-                       if (pst & (CTL_PST_CONFIG | CTL_PST_AUTHENABLE |
-                           CTL_PST_AUTHENTIC | CTL_PST_REACH))
-                               strcat(cb, ", bcst");
-                       else
-                               strcat(cb, "bcst");
-               }
-
-               /*
-                * Now the codes
-                */
-               strcat(cb, ", ");
-               strcat(cb, getcode(pst & 0x7, select_codes));
-               strcat(cb, ", ");
-               strcat(cb, getevents(CTL_PEER_NEVNT(st)));
+               pst = (u_char)CTL_PEER_STATVAL(st);
+               snprintf(cb, LIB_BUFLENGTH, "%s, %s, %s",
+                        peer_st_flags(pst),
+                        getcode(pst & 0x7, select_codes),
+                        getevents(CTL_PEER_NEVNT(st)));
                if (CTL_PEER_EVENT(st) != EVNT_UNSPEC) {
-                       strcat(cb, ", ");
-                       strcat(cb, getcode(CTL_PEER_EVENT(st),
-                           peer_codes));
+                       cc = cb + strlen(cb);
+                       snprintf(cc, LIB_BUFLENGTH - (cc - cb), ", %s",
+                                getcode(CTL_PEER_EVENT(st),
+                                        peer_codes));
                }
                break;
        
        case TYPE_CLOCK:
-               strcat(cb, ", ");
-               strcat(cb, getevents(CTL_SYS_NEVNT(st)));
-               strcat(cb, ", ");
-               strcat(cb, getcode((st) & 0xf, clock_codes));
+               snprintf(cb, LIB_BUFLENGTH, "%s, %s",
+                        getevents(CTL_SYS_NEVNT(st)),
+                        getcode((st) & 0xf, clock_codes));
                break;
        }
+
        return cb;
 }
 
index b52058a65c4da6009f7a0353dcda57a3357c4940..e29f535a12d0662b1c5e952cc8cb118c1884be84 100644 (file)
@@ -281,10 +281,10 @@ step_systime(
 #endif
 
 #ifdef HAVE_UTMP_H
-               memset((char *)&ut, 0, sizeof(ut));
+               memset(&ut, 0, sizeof(ut));
 #endif
 #ifdef HAVE_UTMPX_H
-               memset((char *)&utx, 0, sizeof(utx));
+               memset(&utx, 0, sizeof(utx));
 #endif
 
                /* UTMP */
@@ -292,12 +292,12 @@ step_systime(
 #ifdef UPDATE_UTMP
 # ifdef HAVE_PUTUTLINE
                ut.ut_type = OLD_TIME;
-               (void)strcpy(ut.ut_line, OTIME_MSG);
+               strncpy(ut.ut_line, OTIME_MSG, sizeof(ut.ut_line));
                ut.ut_time = oldtimetv.tv_sec;
                pututline(&ut);
                setutent();
                ut.ut_type = NEW_TIME;
-               (void)strcpy(ut.ut_line, NTIME_MSG);
+               strncpy(ut.ut_line, NTIME_MSG, sizeof(ut.ut_line));
                ut.ut_time = timetv.tv_sec;
                pututline(&ut);
                endutent();
@@ -310,12 +310,12 @@ step_systime(
 #ifdef UPDATE_UTMPX
 # ifdef HAVE_PUTUTXLINE
                utx.ut_type = OLD_TIME;
-               (void)strcpy(utx.ut_line, OTIME_MSG);
+               strncpy(utx.ut_line, OTIME_MSG, sizeof(utx.ut_line));
                utx.ut_tv = oldtimetv;
                pututxline(&utx);
                setutxent();
                utx.ut_type = NEW_TIME;
-               (void)strcpy(utx.ut_line, NTIME_MSG);
+               strncpy(utx.ut_line, NTIME_MSG, sizeof(utx.ut_line));
                utx.ut_tv = timetv;
                pututxline(&utx);
                endutxent();
@@ -329,11 +329,11 @@ step_systime(
 # ifdef HAVE_PUTUTLINE
                utmpname(WTMP_FILE);
                ut.ut_type = OLD_TIME;
-               (void)strcpy(ut.ut_line, OTIME_MSG);
+               strncpy(ut.ut_line, OTIME_MSG, sizeof(ut.ut_line));
                ut.ut_time = oldtimetv.tv_sec;
                pututline(&ut);
                ut.ut_type = NEW_TIME;
-               (void)strcpy(ut.ut_line, NTIME_MSG);
+               strncpy(ut.ut_line, NTIME_MSG, sizeof(ut.ut_line));
                ut.ut_time = timetv.tv_sec;
                pututline(&ut);
                endutent();
@@ -347,7 +347,7 @@ step_systime(
 # ifdef HAVE_PUTUTXLINE
                utx.ut_type = OLD_TIME;
                utx.ut_tv = oldtimetv;
-               (void)strcpy(utx.ut_line, OTIME_MSG);
+               strncpy(utx.ut_line, OTIME_MSG, sizeof(utx.ut_line));
 #  ifdef HAVE_UPDWTMPX
                updwtmpx(WTMPX_FILE, &utx);
 #  else /* not HAVE_UPDWTMPX */
@@ -357,7 +357,7 @@ step_systime(
 # ifdef HAVE_PUTUTXLINE
                utx.ut_type = NEW_TIME;
                utx.ut_tv = timetv;
-               (void)strcpy(utx.ut_line, NTIME_MSG);
+               strncpy(utx.ut_line, NTIME_MSG, sizeof(utx.ut_line));
 #  ifdef HAVE_UPDWTMPX
                updwtmpx(WTMPX_FILE, &utx);
 #  else /* not HAVE_UPDWTMPX */
index 4d48f2a0ef99d43add43af8612208cf68203770d..2040053b4979ee264db22fd082e3683badbd1a44 100644 (file)
@@ -1641,9 +1641,10 @@ kstatus_to_text(
 
 # define       XLATE_KST_BIT(bitval, text)                     \
 do {                                                           \
-       if (((bitval) & status) && pch + sizeof(text) <= lim) { \
+       if (((bitval) & status) &&                              \
+           (pch + sizeof(text) <= lim)) {                      \
                memcpy(pch, (text), sizeof(text));              \
-               pch += sizeof(text) - 1;                        \
+               pch += (u_int)(sizeof(text) - 1);               \
        }                                                       \
 } while (0)
 
@@ -3132,8 +3133,13 @@ write_variables(
        const struct ctl_var *v;
        int ext_var;
        char *valuep;
-       long val = 0;
+       long val;
+       size_t octets;
+       char *vareqv;
+       const char *t;
+       char *tt;
 
+       val = 0;
        /*
         * If he's trying to write into a peer tell him no way
         */
@@ -3180,30 +3186,19 @@ write_variables(
                }
 
                if (ext_var) {
-                       char *s = (char *)emalloc(strlen(v->text) +
-                                                 strlen(valuep) + 2);
-                       const char *t;
-                       char *tt = s;
-
+                       octets = strlen(v->text) + strlen(valuep) + 2;
+                       vareqv = emalloc(octets);
+                       tt = vareqv;
                        t = v->text;
                        while (*t && *t != '=')
                                *tt++ = *t++;
-
                        *tt++ = '=';
-                       strcat(tt, valuep);
-                       set_sys_var(s, strlen(s)+1, v->flags);
-                       free(s);
+                       memcpy(tt, valuep, 1 + strlen(valuep));
+                       set_sys_var(vareqv, 1 + strlen(vareqv), v->flags);
+                       free(vareqv);
                } else {
-                       /*
-                        * This one seems sane. Save it.
-                        */
-                       switch(v->code) {
-
-                           case CS_LEAP:
-                           default:
-                               ctl_error(CERR_UNSPEC); /* really */
-                               return;
-                       }
+                       ctl_error(CERR_UNSPEC); /* really */
+                       return;
                }
        }
 
@@ -4512,6 +4507,30 @@ report_event(
 }
 
 
+/*
+ * mprintf_event - printf-style varargs variant of report_event()
+ */
+int
+mprintf_event(
+       int             evcode,         /* event code */
+       struct peer *   p,              /* may be NULL */
+       const char *    fmt,            /* msnprintf format */
+       ...
+       )
+{
+       va_list ap;
+       int     rc;
+       char    msg[512];
+
+       va_start(ap, fmt);
+       rc = mvsnprintf(msg, sizeof(msg), fmt, ap);
+       va_end(ap);
+       report_event(evcode, p, msg);
+
+       return rc;
+}
+
+
 /*
  * ctl_clr_stats - clear stat counters
  */
index 34cf00f9d678d6a178e05f7d43556a6f472e81d7..5cb2ffa4305152bbf498d250ccbd70b325fd0a2b 100644 (file)
@@ -509,10 +509,7 @@ unpeer(
        struct peer *peer
        )
 {
-       char tbuf[80];
-
-       snprintf(tbuf, sizeof(tbuf), "assoc %u", peer->associd);
-       report_event(PEVNT_DEMOBIL, peer, tbuf);
+       mprintf_event(PEVNT_DEMOBIL, peer, "assoc %u", peer->associd);
        restrict_source(&peer->srcadr, 1, 0);
        set_peerdstadr(peer, NULL);
        peer_demobilizations++;
@@ -793,7 +790,6 @@ newpeer(
 {
        struct peer *peer;
        u_int   hash;
-       char    tbuf[80];
 
 #ifdef AUTOKEY
        /*
@@ -976,8 +972,7 @@ newpeer(
        LINK_SLIST(peer_list, peer, p_link);
 
        restrict_source(&peer->srcadr, 0, 0);
-       snprintf(tbuf, sizeof(tbuf), "assoc %d", peer->associd);
-       report_event(PEVNT_MOBIL, peer, tbuf);
+       mprintf_event(PEVNT_MOBIL, peer, "assoc %d", peer->associd);
        DPRINTF(1, ("newpeer: %s->%s mode %u vers %u poll %u %u flags 0x%x 0x%x ttl %u key %08x\n",
            latoa(peer->dstadr), stoa(&peer->srcadr), peer->hmode,
            peer->version, peer->minpoll, peer->maxpoll, peer->flags,
index ed422129c205bb2cd0a33287636f9de7bfb1c1b7..db8bf80f61429c7e8446e7206266c31900531d9f 100644 (file)
@@ -12,6 +12,7 @@
 #include "ntp_stdlib.h"
 #include "ntp_assert.h"
 #include "ntp_calendar.h"
+#include "lib_strbuf.h"
 
 #include <stdio.h>
 #include <ctype.h>
@@ -349,8 +350,6 @@ stats_config(
        FILE    *fp;
        const char *value;
        int     len;
-       char    tbuf[80];
-       char    str1[20], str2[20];
        double  old_drift;
 #ifndef VMS
        const char temp_ext[] = ".TEMP";
@@ -366,31 +365,36 @@ stats_config(
        char newvalue[MAX_PATH], parameter[MAX_PATH];
 
        if (!ExpandEnvironmentStrings(invalue, newvalue, MAX_PATH)) {
-               switch(item) {
-                   case STATS_FREQ_FILE:
-                       strcpy(parameter,"STATS_FREQ_FILE");
+               switch (item) {
+               case STATS_FREQ_FILE:
+                       strncpy(parameter, "STATS_FREQ_FILE",
+                               sizeof(parameter));
                        break;
 
-                   case STATS_LEAP_FILE:
-                       strcpy(parameter,"STATS_LEAP_FILE");
+               case STATS_LEAP_FILE:
+                       strncpy(parameter, "STATS_LEAP_FILE",
+                               sizeof(parameter));
                        break;
 
-                   case STATS_STATSDIR:
-                       strcpy(parameter,"STATS_STATSDIR");
+               case STATS_STATSDIR:
+                       strncpy(parameter, "STATS_STATSDIR",
+                               sizeof(parameter));
                        break;
 
-                   case STATS_PID_FILE:
-                       strcpy(parameter,"STATS_PID_FILE");
+               case STATS_PID_FILE:
+                       strncpy(parameter, "STATS_PID_FILE",
+                               sizeof(parameter));
                        break;
 
-                   default:
-                       strcpy(parameter,"UNKNOWN");
+               default:
+                       strncpy(parameter, "UNKNOWN",
+                               sizeof(parameter));
                        break;
                }
                value = invalue;
                msyslog(LOG_ERR,
-                   "ExpandEnvironmentStrings(%s) failed: %m\n",
-                   parameter);
+                       "ExpandEnvironmentStrings(%s) failed: %m\n",
+                       parameter);
        } else {
                value = newvalue;
        }
@@ -398,7 +402,7 @@ stats_config(
        value = invalue;
 #endif /* SYS_WINNT */
 
-       switch(item) {
+       switch (item) {
 
        /*
         * Open and read frequency file.
@@ -545,18 +549,15 @@ stats_config(
                        break;
                }
 
-               if (leap_file(fp) < 0) {
+               if (leap_file(fp) < 0)
                        msyslog(LOG_ERR,
                            "format error leapseconds file %s",
                            value);
-               } else {
-                       strcpy(str1, fstostr(leap_sec));
-                       strcpy(str2, fstostr(leap_expire));
-                       snprintf(tbuf, sizeof(tbuf),
-                           "%d leap %s expire %s", leap_tai, str1,
-                           str2);
-                       report_event(EVNT_TAI, NULL, tbuf);
-               }
+               else
+                       mprintf_event(EVNT_TAI, NULL,
+                                     "%d leap %s expire %s", leap_tai,
+                                     fstostr(leap_sec),
+                                     fstostr(leap_expire));
                fclose(fp);
                break;
 
@@ -1063,21 +1064,25 @@ char * fstostr(
        time_t  ntp_stamp
        )
 {
-       static char     str[20];
+       char    *       buf;
        struct tm *     tm;
        time_t          unix_stamp;
 
+       LIB_GETBUF(buf);
        unix_stamp = ntp_stamp - JAN_1970;
        tm = gmtime(&unix_stamp);
-       if (NULL != tm)
-               snprintf(str, sizeof(str),
-                        "%04d%02d%02d%02d%02d",
-                        tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
-                        tm->tm_hour, tm->tm_min);
+       if (NULL == tm)
+#ifdef WAIT_FOR_NTP_CRYPTO_C_CALLERS_ABLE_TO_HANDLE_MORE_THAN_20_CHARS
+               msnprintf(buf, LIB_BUFLENGTH, "gmtime: %m");
+#else
+               strncpy(buf, "gmtime() error", LIB_BUFLENGTH);
+#endif
        else
-               strcpy(str, "gmtime() error");
+               snprintf(buf, LIB_BUFLENGTH, "%04d%02d%02d%02d%02d",
+                        tm->tm_year + 1900, tm->tm_mon + 1,
+                        tm->tm_mday, tm->tm_hour, tm->tm_min);
 
-       return str;
+       return buf;
 }
 
 
index 8e92c0fbffcf42bb5b86ff30a1aed0b426f7a9cc..e7ef6433a041b281b77ee23ffa43d970b0338337 100644 (file)
@@ -289,7 +289,8 @@ arb_receive(
                        return;
 
                } else if (!strncmp(tbuf, "SR", 2)) {
-                       strcpy(up->status, tbuf + 2);
+                       strncpy(up->status, tbuf + 2,
+                               sizeof(up->status));
                        if (pp->sloppyclockflag & CLK_FLAG4)
                                write(pp->io.fd, "LA", 2);
                        else
@@ -297,7 +298,7 @@ arb_receive(
                        return;
 
                } else if (!strncmp(tbuf, "LA", 2)) {
-                       strcpy(up->latlon, tbuf + 2);
+                       strncpy(up->latlon, tbuf + 2, sizeof(up->latlon));
                        write(pp->io.fd, "LO", 2);
                        return;
 
index 163c2afee1a246287574dd8ba47944eefe12f2cd..c72662b8ab6b698978146d41938200c12669d4b0 100644 (file)
@@ -233,6 +233,7 @@ as2201_receive(
        struct refclockproc *pp;
        struct peer *peer;
        l_fp trtmp;
+       size_t octets;
 
        /*
         * Initialize pointers and read the timecode and timestamp.
@@ -267,7 +268,7 @@ as2201_receive(
                if ((int)(up->lastptr - up->stats + pp->lencode) > SMAX - 2)
                    return;
                *up->lastptr++ = ' ';
-               (void)strcpy(up->lastptr, pp->a_lastcode);
+               memcpy(up->lastptr, pp->a_lastcode, 1 + pp->lencode);
                up->lastptr += pp->lencode;
                return;
        } else {
@@ -328,13 +329,17 @@ as2201_receive(
         * send the next command. If not, simply write the timecode to
         * the clockstats file.
         */
+       if ((int)(up->lastptr - up->stats + pp->lencode) > SMAX - 2)
+           return;
        (void)strcpy(up->lastptr, pp->a_lastcode);
        up->lastptr += pp->lencode;
        if (pp->sloppyclockflag & CLK_FLAG4) {
+               octets = strlen(stat_command[up->index]);
+               if ((int)(up->lastptr - up->stats + octets) > SMAX - 2)
+                   return;
                *up->lastptr++ = ' ';
-               (void)strcpy(up->lastptr, stat_command[up->index]);
-               up->lastptr += strlen(stat_command[up->index]);
-               up->lastptr--;
+               memcpy(up->lastptr, stat_command[up->index], octets);
+               up->lastptr += octets - 1;
                *up->lastptr = '\0';
                (void)write(pp->io.fd, stat_command[up->index],
                    strlen(stat_command[up->index]));
index ca14dc600fb4b4a4b93f580738aca94e754456da..c14bc71cd9d5243a56cab1a68e04a43cbdafebe7 100644 (file)
@@ -527,7 +527,7 @@ chu_start(
         */
        peer->precision = PRECISION;
        pp->clockdesc = DESCRIPTION;
-       strcpy(up->ident, "CHU");
+       strncpy(up->ident, "CHU", sizeof(up->ident));
        memcpy(&pp->refid, up->ident, 4); 
        DTOLFP(CHAR, &up->charstamp);
 #ifdef HAVE_AUDIO
index 5867a1d7b278e92e025418d3ff9cd1f1168714f5..38dadfb43082a4d2e8ce391fe6bbc6a416e6eeb8 100644 (file)
@@ -300,7 +300,7 @@ hpgps_receive(
        if (up->linecnt-- > 0) {
                if ((int)(pp->lencode + 2) <= (SMAX - (up->lastptr - up->statscrn))) {
                        *up->lastptr++ = '\n';
-                       (void)strcpy(up->lastptr, pp->a_lastcode);
+                       memcpy(up->lastptr, pp->a_lastcode, pp->lencode);
                        up->lastptr += pp->lencode;
                }
                if (up->linecnt == 0) 
@@ -333,7 +333,7 @@ hpgps_receive(
         *
         */
 
-       (void)strcpy(prompt,pp->a_lastcode);
+       strncpy(prompt, pp->a_lastcode, sizeof(prompt));
        tcp = strrchr(pp->a_lastcode,'>');
        if (tcp == NULL)
            tcp = pp->a_lastcode; 
index fe6592f82871496c18b7fb67b7d62cb070df3122..059ee4221da19c6c50b25e5e3846c8d7e43b38e6 100644 (file)
@@ -317,10 +317,10 @@ neoclock4x_start(int unit,
 
   up->leap_status = 0;
   up->unit = unit;
-  strcpy(up->firmware, "?");
+  strncpy(up->firmware, "?", sizeof(up->firmware));
   up->firmwaretag = '?';
-  strcpy(up->serial, "?");
-  strcpy(up->radiosignal, "?");
+  strncpy(up->serial, "?", sizeof(up->serial));
+  strncpy(up->radiosignal, "?", sizeof(up->radiosignal));
   up->timesource  = '?';
   up->dststatus   = '?';
   up->quarzstatus = '?';
@@ -336,7 +336,8 @@ neoclock4x_start(int unit,
 
 #if defined(NEOCLOCK4X_FIRMWARE)
 #if NEOCLOCK4X_FIRMWARE == NEOCLOCK4X_FIRMWARE_VERSION_A
-  strcpy(up->firmware, "(c) 2002 NEOL S.A. FRANCE / L0.01 NDF:A:* (compile time)");
+  strncpy(up->firmware, "(c) 2002 NEOL S.A. FRANCE / L0.01 NDF:A:* (compile time)",
+         sizeof(up->firmware));
   up->firmwaretag = 'A';
 #else
   msyslog(LOG_EMERG, "NeoClock4X(%d): unknown firmware defined at compile time for NeoClock4X",
@@ -931,13 +932,13 @@ neol_query_firmware(int fd,
          if(read_errors > 5)
            {
              msyslog(LOG_ERR, "NeoClock4X(%d): can't read firmware version (timeout)", unit);
-             strcpy(tmpbuf, "unknown due to timeout");
+             strncpy(tmpbuf, "unknown due to timeout", sizeof(tmpbuf));
              break;
            }
           if(chars_read > 500)
             {
              msyslog(LOG_ERR, "NeoClock4X(%d): can't read firmware version (garbage)", unit);
-             strcpy(tmpbuf, "unknown due to garbage input");
+             strncpy(tmpbuf, "unknown due to garbage input", sizeof(tmpbuf));
              break;
             }
          if(-1 == read(fd, &c, 1))
@@ -963,7 +964,7 @@ neol_query_firmware(int fd,
              if(0xA9 != c) /* wait for (c) char in input stream */
                continue;
 
-             strcpy(tmpbuf, "(c)");
+             strncpy(tmpbuf, "(c)", sizeof(tmpbuf));
              len = 3;
              init = 0;
              continue;
@@ -1008,7 +1009,7 @@ neol_query_firmware(int fd,
   else
     {
       msyslog(LOG_ERR, "NeoClock4X(%d): can't query firmware version", unit);
-      strcpy(tmpbuf, "unknown error");
+      strncpy(tmpbuf, "unknown error", sizeof(tmpbuf));
     }
   strncpy(firmware, tmpbuf, maxlen);
   firmware[maxlen] = '\0';
index a0c78340e46e9eb4ef2272729153a6dfb7ccd326..1a87cd997fe96e3be2b412a8faa3d36267e09468 100644 (file)
@@ -381,7 +381,8 @@ static      int     oncore_checksum_ok    (u_char *, int);
 static void    oncore_compute_dH     (struct instance *);
 static void    oncore_load_almanac   (struct instance *);
 static void    oncore_log            (struct instance *, int, const char *);
-static int     oncore_log_f          (struct instance *, int, const char *, ...);
+static int     oncore_log_f          (struct instance *, int, const char *, ...)
+               __attribute__((__format__(__printf__, 3, 4)));
 static void    oncore_print_Cb       (struct instance *, u_char *);
 /* static  void    oncore_print_array   (u_char *, int);       */
 static void    oncore_print_posn     (struct instance *);
index 074922a0de08631849a58dd92442b20c9242c4d9..e222f88d6903f214cb68ce4eada13eeab5c87fc3 100644 (file)
@@ -1522,13 +1522,14 @@ mkreadable(
        int hex
        )
 {
+       static const char ellipsis[] = "...";
        char *b    = buffer;
        char *endb = NULL;
 
        if (blen < 4)
                return NULL;            /* don't bother with mini buffers */
 
-       endb = buffer + blen - 4;
+       endb = buffer + blen - sizeof(ellipsis);
 
        blen--;                 /* account for '\0' */
 
@@ -1557,7 +1558,7 @@ mkreadable(
                        {
                                if (*src == '\\')
                                {
-                                       strcpy(buffer,"\\\\");
+                                       memcpy(buffer, "\\\\", 2);
                                        buffer += 2;
                                        blen   -= 2;
                                        src++;
@@ -1571,7 +1572,7 @@ mkreadable(
                        }
                }
                if (srclen && !blen && endb) /* overflow - set last chars to ... */
-                       strcpy(endb, "...");
+                       memcpy(endb, ellipsis, sizeof(ellipsis));
        }
 
        *buffer = '\0';
index e2520c0dc1c360b87cf8a0e574a7d535bd5c66cd..632992abbafc0cc84119e6cb60c79947f2045397 100644 (file)
@@ -367,7 +367,7 @@ true_receive(
         if (rd_lencode == 0)
             return;
         pp->lencode = rd_lencode;
-        strcpy(pp->a_lastcode, rd_lastcode);
+        strncpy(pp->a_lastcode, rd_lastcode, sizeof(pp->a_lastcode));
         pp->lastrec = rd_tmp;
        true_debug(peer, "receive(%s) [%d]\n", pp->a_lastcode, pp->lencode);
 
index bd7d4869be54af99812bb33d9b9813c99a254bcc..d126a34e3b76b8f96eae068479d63ba02e57bcce 100644 (file)
@@ -1366,7 +1366,7 @@ addserver(
        int error;
        /* Service name */
        char service[5];
-       strcpy(service, "ntp");
+       strncpy(service, "ntp", sizeof(service));
 
        /* Get host address. Looking for UDP datagram connection. */
        memset(&hints, 0, sizeof(hints));
@@ -1696,7 +1696,7 @@ init_io(void)
         * Open the socket
         */
 
-       strcpy(service, "ntp");
+       strncpy(service, "ntp", sizeof(service));
 
        /*
         * Init hints addrinfo structure
@@ -1706,10 +1706,10 @@ init_io(void)
        hints.ai_flags = AI_PASSIVE;
        hints.ai_socktype = SOCK_DGRAM;
 
-       if(getaddrinfo(NULL, service, &hints, &res) != 0) {
-              msyslog(LOG_ERR, "getaddrinfo() failed: %m");
-              exit(1);
-              /*NOTREACHED*/
+       if (getaddrinfo(NULL, service, &hints, &res) != 0) {
+               msyslog(LOG_ERR, "getaddrinfo() failed: %m");
+               exit(1);
+               /*NOTREACHED*/
        }
 
 #ifdef SYS_WINNT
index e7d449b792173bfc0896f9cc9a0b9a2e8d5b5bb6..2de9bf67b731b549772bcfea1892e788d1aee2ba 100644 (file)
@@ -426,7 +426,7 @@ openhost(
         * will return an "IPv4-mapped IPv6 address" address if you
         * give it an IPv4 address to lookup.
         */
-       strcpy(service, "ntp");
+       strncpy(service, "ntp", sizeof(service));
        memset((char *)&hints, 0, sizeof(struct addrinfo));
        hints.ai_family = ai_fam_templ;
        hints.ai_protocol = IPPROTO_UDP;
@@ -481,7 +481,7 @@ openhost(
                (void) closesocket(sockfd);
                havehost = 0;
        }
-       (void) strcpy(currenthost, temphost);
+       strncpy(currenthost, temphost, sizeof(currenthost));
        
        /* port maps to the same in both families */
        s_port = ((struct sockaddr_in6 *)ai->ai_addr)->sin6_port; 
index 4d4b3704ea67c987ca8cdd7e87bc83383feca6b2..9f31e565bca4d5274a1a1962d4d83ca9feee7ac9 100644 (file)
@@ -1786,7 +1786,7 @@ again:
                }
 
                if (flagstr[0] == '\0')
-                       strcpy(flagstr, "none");
+                       strncpy(flagstr, "none", sizeof(flagstr));
 
                if (!skip)
                        fprintf(fp, "%-15.15s %-15.15s %9lu  %s\n",
index 19d540454232fd03b3f2c0a387f06b50f42b8cd1..e0403e10f026173aacfae443f5b41116989609e1 100644 (file)
@@ -595,7 +595,7 @@ openhost(
                (void) closesocket(sockfd);
                havehost = 0;
        }
-       (void) strcpy(currenthost, temphost);
+       strncpy(currenthost, temphost, sizeof(currenthost));
 
        /* port maps to the same location in both families */
        s_port = ((struct sockaddr_in6 *)ai->ai_addr)->sin6_port;
index 5e196a825d6f54c426b1082b1afbfc17f1471215..7de80b80d6a40ed98ed12c16eda3d37e4a0127cd 100644 (file)
@@ -41,8 +41,7 @@ init_randfile()
        homedir = getenv("HOME");
        if (homedir != NULL &&
            (strlen(homedir) + 5 /* \.rnd */) < sizeof(tmp)) {
-               strncpy(tmp, homedir, sizeof(tmp));
-               strcat(tmp, "\\.rnd");
+               snprintf(tmp, sizeof(tmp), "%s\\.rnd", homedir);
                rf = fopen(tmp, "rb");
                if (rf != NULL) {
                        fclose(rf);