]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
First pass at quieting Veracode static analysis warnings,
authorDave Hart <hart@ntp.org>
Sat, 8 Aug 2009 17:30:14 +0000 (17:30 +0000)
committerDave Hart <hart@ntp.org>
Sat, 8 Aug 2009 17:30:14 +0000 (17:30 +0000)
  mostly buffer manipulation that is already safe but used
  unsafe interface functions such as strcpy() and sprintf().
use emalloc(), estrdup() where appropriate.

bk: 4a7db626RejXNB66rrJX_BXKwPCGwQ

21 files changed:
libntp/msyslog.c
libntp/ntp_rfc2553.c
libntp/statestr.c
libparse/parse.c
ntpd/ntp_config.c
ntpd/ntp_control.c
ntpd/ntp_intres.c
ntpd/ntp_io.c
ntpd/ntp_scanner.c
ntpd/ntp_signd.c
ntpd/ntp_util.c
ntpd/ntpd.c
ntpd/refclock_acts.c
ntpd/refclock_bancomm.c
ntpd/refclock_datum.c
ntpd/refclock_oncore.c
ntpdate/ntpdate.c
ntpdc/ntpdc.c
ntpq/ntpq-subs.c
ports/winnt/libisc/isc_strerror.c
ports/winnt/ntpd/ntp_iocompletionport.c

index 39815f6586ac108967b5b582492cc83cea964ee4..4e2bb4bc232edb22e228e317bf887e281a98f6c8 100644 (file)
@@ -84,12 +84,12 @@ format_errmsg(char *nfmt, int lennfmt, const char *fmt, int errval)
        register char c;
        register char *n;
        register const char *f;
-
+       size_t len;
        char *err;
 
        n = nfmt;
        f = fmt;
-       while ((c = *f++) != '\0' && n < (nfmt+lennfmt - 2)) {
+       while ((c = *f++) != '\0' && n < (nfmt + lennfmt - 2)) {
                if (c != '%') {
                        *n++ = c;
                        continue;
@@ -99,12 +99,13 @@ format_errmsg(char *nfmt, int lennfmt, const char *fmt, int errval)
                        *n++ = c;
                        continue;
                }
-               err = 0;
                err = strerror(errval);
+               len = strlen(err);
+
                /* Make sure we have enough space for the error message */
-               if ((n + strlen(err)) < (nfmt + lennfmt -2)) {
-                       strcpy(n, err);
-                       n += strlen(err);
+               if ((n + len) < (nfmt + lennfmt - 2)) {
+                       memcpy(n, err, len);
+                       n += len;
                }
        }
 #if !defined(VMS)
index 592587c6e2657ee05cdd22e498625418158ad1dc..01a3c26d6fae3bd2d1a81d6cf2899263ccc6d2c6 100644 (file)
@@ -431,12 +431,8 @@ do_nodename(
 #ifdef HAVE_SA_LEN_IN_STRUCT_SOCKADDR
        ai->ai_addr->sa_len = sizeof(struct sockaddr);
 #endif
-       if (hints != NULL && hints->ai_flags & AI_CANONNAME) {
-               ai->ai_canonname = malloc(strlen(hp->h_name) + 1);
-               if (ai->ai_canonname == NULL)
-                       return (EAI_MEMORY);
-               strcpy(ai->ai_canonname, hp->h_name);
-       }
+       if (hints != NULL && hints->ai_flags & AI_CANONNAME)
+               ai->ai_canonname = estrdup(hp->h_name);
        return (0);
 }
 
index 9ad7f38e3968712597b5167e8d6f086b9ee9ed1f..fcb2c121e8c93371b2d059f321b71889ebde8e4d 100644 (file)
@@ -198,10 +198,10 @@ getcode(
 
        while (codetab->code != -1) {
                if (codetab->code == code)
-                   return codetab->string;
+                       return codetab->string;
                codetab++;
        }
-       (void) sprintf(buf, "%s_%d", codetab->string, code);
+       snprintf(buf, sizeof(buf), "%s_%d", codetab->string, code);
        return buf;
 }
 
@@ -216,8 +216,9 @@ getevents(
        static char buf[20];
 
        if (cnt == 0)
-           return "no events";
-       (void) sprintf(buf, "%d event%s", cnt, (cnt==1) ? "" : "s");
+               return "no events";
+       snprintf(buf, sizeof(buf), "%d event%s", cnt, (cnt==1) ? "" : 
+           "s");
        return buf;
 }
 
index c33ecca9d351a37d3d7f5a57bc40f52cbac38f87..83e560aff0c635c0ee6c5a5fbd48c40639362032 100644 (file)
@@ -234,7 +234,7 @@ parse_addchar(
                 * collect into buffer
                 */
                parseprintf(DD_PARSE, ("parse: parse_addchar: buffer[%d] = 0x%x\n", parseio->parse_index, ch));
-               parseio->parse_data[parseio->parse_index++] = ch;
+               parseio->parse_data[parseio->parse_index++] = (char)ch;
                return PARSE_INP_SKIP;
        }
        else
index 8be82e5c45866ed75cb719d936d6fb908378d5b4..694e32d23171befda75b411270b7a07ee84d2f37 100644 (file)
@@ -2313,13 +2313,14 @@ getconfig(
        /*
         * install a non default variable with this daemon version
         */
-       (void) sprintf(line, "daemon_version=\"%s\"", Version);
+       snprintf(line, sizeof(line), "daemon_version=\"%s\"", Version);
        set_sys_var(line, strlen(line)+1, RO);
 
        /*
         * Say how we're setting the time of day
         */
-       (void) sprintf(line, "settimeofday=\"%s\"", set_tod_using);
+       snprintf(line, sizeof(line), "settimeofday=\"%s\"",
+           set_tod_using);
        set_sys_var(line, strlen(line)+1, RO);
 
        /*
@@ -2852,7 +2853,10 @@ abort_resolve(void)
 
 #ifndef SYS_VXWORKS            /* we don't open the file to begin with */
 #if !defined(VMS)
-       (void) unlink(res_file);
+       if (unlink(res_file))
+               msyslog(LOG_WARNING, 
+                       "Unable to remove temporary resolver file %s, %m",
+                       res_file);
 #else
        (void) delete(res_file);
 #endif /* VMS */
index 5a138e57653b561852df55981a75d6824d15ddd9..fb7397c4e6558367df203cdcf2ecebf4e2a76650 100644 (file)
@@ -1333,16 +1333,15 @@ ctl_putsys(
                    register struct ctl_var *k;
 
                    s = buf;
-                   be = buf + sizeof(buf) -
-                       strlen(sys_var[CS_VARLIST].text) - 4;
-                   if (s > be)
+                   be = buf + sizeof(buf);
+                   if (s + strlen(sys_var[CS_VARLIST].text) + 4 > be)
                            break;      /* really long var name */
 
-                   strcpy(s, sys_var[CS_VARLIST].text);
-                   strcat(s, "=\"");
+                   snprintf(s, sizeof(buf), "%s=\"",
+                       sys_var[CS_VARLIST].text);
                    s += strlen(s);
                    t = s;
-                   for (k = sys_var; !(k->flags &EOV); k++) {
+                   for (k = sys_var; !(k->flags & EOV); k++) {
                            if (k->flags & PADDING)
                                    continue;
                            i = strlen(k->text);
@@ -1351,11 +1350,11 @@ ctl_putsys(
 
                            if (s != t)
                                    *s++ = ',';
-                           strcpy(s, k->text);
+                           memcpy(s, k->text, i);
                            s += i;
                    }
 
-                   for (k = ext_sys_var; k && !(k->flags &EOV);
+                   for (k = ext_sys_var; k && !(k->flags & EOV);
                         k++) {
                            if (k->flags & PADDING)
                                    continue;
@@ -1372,7 +1371,7 @@ ctl_putsys(
 
                            if (s != t)
                                    *s++ = ',';
-                           strncpy(s, k->text,
+                           memcpy(s, k->text,
                                    (unsigned)i);
                            s += i;
                    }
@@ -1440,8 +1439,8 @@ ctl_putsys(
 
            case CS_CERTIF:
                for (cp = cinfo; cp != NULL; cp = cp->link) {
-                       sprintf(cbuf, "%s %s 0x%x", cp->subject,
-                           cp->issuer, cp->flags);
+                       snprintf(cbuf, sizeof(cbuf), "%s %s 0x%x",
+                           cp->subject, cp->issuer, cp->flags);
                        ctl_putstr(sys_var[CS_CERTIF].text, cbuf,
                            strlen(cbuf));
                        ctl_putfs(sys_var[CS_REVTIME].text, cp->last);
@@ -1693,16 +1692,15 @@ ctl_putpeer(
                    register struct ctl_var *k;
 
                    s = buf;
-                   be = buf + sizeof(buf) -
-                       strlen(peer_var[CP_VARLIST].text) - 4;
-                   if (s > be)
+                   be = buf + sizeof(buf);
+                   if (s + strlen(peer_var[CP_VARLIST].text) + 4 > be)
                            break;      /* really long var name */
 
-                   strcpy(s, peer_var[CP_VARLIST].text);
-                   strcat(s, "=\"");
+                   snprintf(s, sizeof(buf), "%s=\"",
+                       peer_var[CP_VARLIST].text);
                    s += strlen(s);
                    t = s;
-                   for (k = peer_var; !(k->flags &EOV); k++) {
+                   for (k = peer_var; !(k->flags & EOV); k++) {
                            if (k->flags & PADDING)
                                    continue;
 
@@ -1712,7 +1710,7 @@ ctl_putpeer(
 
                            if (s != t)
                                    *s++ = ',';
-                           strcpy(s, k->text);
+                           memcpy(s, k->text, i);
                            s += i;
                    }
                    if (s+2 >= be)
@@ -1869,12 +1867,12 @@ ctl_putclock(
                        be)
                            break;      /* really long var name */
 
-                   strcpy(s, clock_var[CC_VARLIST].text);
-                   strcat(s, "=\"");
+                   snprintf(s, sizeof(buf), "%s=\"", 
+                       clock_var[CC_VARLIST].text);
                    s += strlen(s);
                    t = s;
 
-                   for (k = clock_var; !(k->flags &EOV); k++) {
+                   for (k = clock_var; !(k->flags & EOV); k++) {
                            if (k->flags & PADDING)
                                    continue;
 
@@ -1884,7 +1882,7 @@ ctl_putclock(
 
                            if (s != t)
                                    *s++ = ',';
-                           strcpy(s, k->text);
+                           memcpy(s, k->text, i);
                            s += i;
                    }
 
@@ -1905,7 +1903,7 @@ ctl_putclock(
 
                            if (s != t)
                                    *s++ = ',';
-                           strncpy(s, k->text, (unsigned)i);
+                           memcpy(s, k->text, (unsigned)i);
                            s += i;
                            *s = '\0';
                    }
@@ -2778,6 +2776,7 @@ report_event(
 {
        char    statstr[NTP_MAXSTRLEN];
        int     i;
+       size_t  len;
 
        /*
         * Report the error to the protostats file, system log and
@@ -2800,8 +2799,9 @@ report_event(
                    "0.0.0.0 %04x %02x %s",
                    ctlsysstatus(), err, eventstr(err));
                if (str != NULL) {
-                       strcat(statstr, " ");
-                       strcat(statstr, str);
+                       len = strlen(statstr);
+                       snprintf(statstr + len, sizeof(statstr) - len,
+                           " %s", str);
                }
                NLOG(NLOG_SYSEVENT)
                    msyslog(LOG_INFO, statstr);
@@ -2832,8 +2832,9 @@ report_event(
                    "%s %04x %02x %s", src,
                    ctlpeerstatus(peer), err, eventstr(err));
                if (str != NULL) {
-                       strcat(statstr, " ");
-                       strcat(statstr, str);
+                       len = strlen(statstr);
+                       snprintf(statstr + len, sizeof(statstr) - len,
+                           " %s", str);
                }
                NLOG(NLOG_PEEREVENT)
                    msyslog(LOG_INFO, statstr);
index ef398a27182b8e77945d3d2baa01bf395fbc6faa..eb88b24edb382f191c8308f4a631ce8acc09936c 100644 (file)
@@ -247,9 +247,12 @@ ntp_intres(void)
        (void) fclose(in);
 
 #ifdef DEBUG
-       if (!debug )
+       if (!debug)
 #endif
-               (void) unlink(req_file);
+               if (unlink(req_file))
+                       msyslog(LOG_WARNING,
+                               "unable to remove intres request file %s, %m",
+                               req_file);
 
        /*
         * Set up the timers to do first shot immediately.
@@ -437,7 +440,8 @@ addentry(
        ce->ce_flags = (u_char)flags;
        ce->ce_ttl = (u_char)ttl;
        ce->ce_keyid = keyid;
-       strncpy(ce->ce_keystr, keystr, sizeof(ce->ce_keystr));
+       strncpy(ce->ce_keystr, keystr, sizeof(ce->ce_keystr) - 1);
+       ce->ce_keystr[sizeof(ce->ce_keystr) - 1] = 0;
        ce->ce_next = NULL;
 
        if (confentries == NULL) {
index bc19f3435dbbbd4f2bdda4d3bc806df528b6697a..af3389f090946021c22dcbb1f1a71d7f8cc42639 100644 (file)
@@ -1108,6 +1108,7 @@ convert_isc_if(
        )
 {
        strncpy(itf->name, isc_if->name, sizeof(itf->name));
+       itf->name[sizeof(itf->name) - 1] = 0; /* strncpy may not */
        itf->family = (u_short)isc_if->af;
        AF(&itf->sin) = itf->family;
        AF(&itf->mask) = itf->family;
index 5799f7a379b8a02fc2e9f3a0304e7c2430937c37..be7a4d8868cf58faa36746bc77fe027d38123dc6 100644 (file)
@@ -106,7 +106,7 @@ create_states(
        if (curr_char && (keyword[0] == curr_char->ch))
                my_state = curr_char;
        else {
-               my_state = (struct state *) malloc(sizeof(struct state));
+               my_state = emalloc(sizeof(*my_state));
                my_state->ch = keyword[0];  /* Store the first character
                                               of the keyword */
                my_state->next_state = NULL;
@@ -528,9 +528,8 @@ yylex(
                ip_file->prev_token_col_no = ip_file->col_no;
 
                /* Read in the lexeme */
-               for (i = 0;
-                    (i < MAX_LEXEME) && EOF != (yytext[i] = get_next_char());
-                    i++) {
+               i = 0;
+               while (EOF != (yytext[i] = get_next_char())) {
 
                        /* Break on whitespace or a special character */
                        if (isspace(yytext[i]) 
@@ -546,6 +545,10 @@ yylex(
                                        ; /* Null Statement */
                                break;
                        }
+
+                       i++;
+                       if (i >= COUNTOF(yytext))
+                               goto lex_too_long;
                }
                /* Pick up all of the string inside between " marks, to
                 * end of line.  If we make it to EOL without a
@@ -558,6 +561,8 @@ yylex(
                        while ((yytext[i] = get_next_char()) != EOF &&
                               yytext[i] != '"' && yytext[i] != '\n') {
                                i++;
+                               if (i >= COUNTOF(yytext))
+                                       goto lex_too_long;
                        }
                        if (yytext[i] == '"')
                                yytext[i] =  ' ';
@@ -584,7 +589,7 @@ yylex(
         * returned) and that we haven't read a string.
         */
        
-       if ((expect_string == NO_ARG) &&  (!instring)) {
+       if (expect_string == NO_ARG && !instring) {
                token = is_keyword(yytext, &expect_string);
                if (token) 
                        return token;
@@ -648,4 +653,24 @@ yylex(
                expect_string = NO_ARG;
 
        return create_string_token(yytext);
+
+lex_too_long:
+       yytext[min(sizeof(yytext) - 1, 50)] = 0;
+       msyslog(LOG_ERR, 
+               "configuration item on line %d longer than limit of %d, began with '%s'",
+               ip_file->line_no, sizeof(yytext) - 1, yytext);
+
+       /*
+        * If we hit the length limit reading the startup configuration
+        * file, abort.
+        */
+       if (input_from_file)
+               exit(sizeof(yytext) - 1);
+
+       /*
+        * If it's runtime configuration via ntpq :config treat it as
+        * if the configuration text ended before the too-long lexeme,
+        * hostname, or string.
+        */
+       return 0;
 }
index 3b3c5ef634a34fdf95eb8c035339a63e6d2851aa..3beb03be5a6961ff760e60da64310d80c261adc1 100644 (file)
@@ -110,10 +110,7 @@ recv_packet(int fd, char **buf, uint32_t *len)
 {
        if (read_all(fd, len, sizeof(*len)) != sizeof(*len)) return -1;
        *len = ntohl(*len);
-       (*buf) = malloc(*len);
-       if (!*buf) {
-               return -1;
-       }
+       (*buf) = emalloc(*len);
        if (read_all(fd, *buf, *len) != *len) {
                free(*buf);
                return -1;
index e3b858fd69510ff9d616faf94bfe51580efce512..65f3d356855cfa5e82a1724865fa402b0af1fb67 100644 (file)
@@ -295,12 +295,17 @@ write_stats(void)
                        (void)fclose(fp);
                        /* atomic */
 #ifdef SYS_WINNT
-                       (void) _unlink(stats_drift_file); /* rename semantics differ under NT */
+                       if (_unlink(stats_drift_file)) /* rename semantics differ under NT */
+                               msyslog(LOG_WARNING, 
+                                       "Unable to remove prior drift file %s, %m", 
+                                       stats_drift_file);
 #endif /* SYS_WINNT */
 
 #ifndef NO_RENAME
-                       (void) rename(stats_temp_file,
-                           stats_drift_file);
+                       if (rename(stats_temp_file, stats_drift_file))
+                               msyslog(LOG_WARNING, 
+                                       "Unable to rename temp drift file %s to %s, %m", 
+                                       stats_temp_file, stats_drift_file);
 #else
                        /* we have no rename NFS of ftp in use */
                        if ((fp = fopen(stats_drift_file, "w")) ==
index b7be64afbd7f0020f0799701b2ecff6a859b9732..dd9abf134ba58944f7e6b40833ca8f35a199975d 100644 (file)
@@ -918,16 +918,20 @@ getgroup:
                        }
                }
 
-               ifchrootdir ) {
+               if (chrootdir ) {
                        /* make sure cwd is inside the jail: */
-                       if( chdir(chrootdir) ) {
+                       if (chdir(chrootdir)) {
                                msyslog(LOG_ERR, "Cannot chdir() to `%s': %m", chrootdir);
                                exit (-1);
                        }
-                       if( chroot(chrootdir) ) {
+                       if (chroot(chrootdir)) {
                                msyslog(LOG_ERR, "Cannot chroot() to `%s': %m", chrootdir);
                                exit (-1);
                        }
+                       if (chdir("/")) {
+                               msyslog(LOG_ERR, "Cannot chdir() to`root after chroot(): %m");
+                               exit (-1);
+                       }
                }
                if (user && initgroups(user, sw_gid)) {
                        msyslog(LOG_ERR, "Cannot initgroups() to user `%s': %m", user);
index 058abfd6428dd4266a2f91844f57f6cabc2fdf48..71923ce514ec558706e23fc07d2ec0886ec81b94 100644 (file)
@@ -250,9 +250,6 @@ acts_start (
         * Allocate and initialize unit structure
         */
        up = emalloc(sizeof(struct actsunit));
-       if (up == NULL)
-               return (0);
-
        memset(up, 0, sizeof(struct actsunit));
        up->unit = unit;
        pp = peer->procptr;
index 5c49662180e7cae53f78732f7683c46314d43ef0..0e9ceb00202a53e500a76b1d9f0604d526e4869a 100644 (file)
@@ -422,9 +422,8 @@ get_datumtime(struct vmedate *time_vme)
        uint8_t dmy;
        struct stfp_time stfpm;
        
-       if ( time_vme == (struct vmedate *)NULL) {
-         time_vme = (struct vmedate *)malloc(sizeof(struct vmedate ));
-       }
+       if (time_vme == NULL)
+               time_vme = emalloc(sizeof(*time_vme));
 
        switch (tfp_type) {
                case 1:                         /* BSD, PCI, 2 32bit time words */
index e3fcdcd769565c29d9e40a27152d3feafb405049..247d7da4acf52a7500122c443306f9cd70b691f3 100644 (file)
@@ -397,7 +397,7 @@ datum_pts_shutdown(
                        if (nunits > 1) {
 
                                temp_datum_pts_unit = (struct datum_pts_unit **)
-                                       malloc((nunits-1)*sizeof(struct datum_pts_unit *));
+                                       emalloc((nunits-1)*sizeof(struct datum_pts_unit *));
                                if (i!= 0) memcpy(temp_datum_pts_unit, datum_pts_unit,
                                                  i*sizeof(struct datum_pts_unit *));
 
index 52507057f1bc444963313b177d99c9c69e347fb5..97a62986b80ae45bef473e50a80ea246d2a7e53b 100644 (file)
@@ -600,11 +600,8 @@ oncore_start(
 
        /* create instance structure for this unit */
 
-       if (!(instance = (struct instance *) malloc(sizeof *instance))) {
-               perror("malloc");
-               return (0);
-       }
-       memset((char *) instance, 0, sizeof *instance);
+       instance = emalloc(sizeof(*instance));
+       memset(instance, 0, sizeof(*instance));
 
        /* initialize miscellaneous variables */
 
@@ -929,7 +926,7 @@ oncore_init_shmem(
        struct stat sbuf;
        size_t shmem_length;
 
-       /*
+       /*
        * The first thing we do is see if there is an instance->shmem_fname file (still)
        * out there from a previous run.  If so, we copy it in and use it to initialize
        * shmem (so we won't lose our almanac if we need it).
@@ -943,11 +940,8 @@ oncore_init_shmem(
                fstat(fd, &sbuf);
                shmem_old_size = sbuf.st_size;
                if (shmem_old_size != 0) {
-                       shmem_old = (u_char *) malloc((unsigned) sbuf.st_size);
-                       if (shmem_old == NULL)
-                               oncore_log(instance, LOG_WARNING, "ONCORE: Can't malloc buffer for shmem_old");
-                       else
-                               read(fd, shmem_old, shmem_old_size);
+                       shmem_old = emalloc((unsigned) sbuf.st_size);
+                       read(fd, shmem_old, shmem_old_size);
                }
                close(fd);
        }
@@ -988,16 +982,7 @@ oncore_init_shmem(
        }
        shmem_length = n + 2;
 
-       buf = malloc(shmem_length);
-       if (buf == NULL) {
-               oncore_log(instance, LOG_WARNING, "ONCORE: Can't malloc buffer for shmem");
-               close(instance->shmemfd);
-               if (shmem_old)
-                       free(shmem_old);
-
-               return;
-       }
-
+       buf = emalloc(shmem_length);
        memset(buf, 0, shmem_length);
 
        /* next build the new SHMEM buffer in memory */
@@ -1263,9 +1248,7 @@ oncore_read_config(
                        continue;
 
                if (!strncmp(cc, "STATUS", (size_t) 6) || !strncmp(cc, "SHMEM", (size_t) 5)) {
-                       i = strlen(ca);
-                       instance->shmem_fname = (char *) malloc((unsigned) (i+1));
-                       strcpy(instance->shmem_fname, ca);
+                       instance->shmem_fname = estrdup(ca);
                        continue;
                }
 
index 27c3c7361a8a9cecb9d0dc71de151b1809cc33f1..70b33704c7da3401e12730545bb1868ead2c1a94 100644 (file)
@@ -2232,7 +2232,7 @@ getnetinfoservers(void)
        ni_status status;
        void *domain;
        ni_id confdir;
-       ni_namelist *namelist = (ni_namelist*)malloc(sizeof(ni_namelist));
+       ni_namelist *namelist = emalloc(sizeof(ni_namelist));
 
        /* Find a time server in NetInfo */
        if ((status = ni_open(NULL, ".", &domain)) != NI_OK) return NULL;
index 78abce39a0e4b8c3cfa461b2aa8e552f62b0b6bc..d5bab66b92aebf3b46e0f957b0352b2a39c4dd53 100644 (file)
@@ -436,12 +436,8 @@ ntpdcmain(
        /*
         * Initialize the packet data buffer
         */
-       pktdata = (char *)malloc(INITDATASIZE);
-       if (pktdata == NULL) {
-               (void) fprintf(stderr, "%s: malloc() failed!\n", progname);
-               exit(1);
-       }
        pktdatasize = INITDATASIZE;
+       pktdata = emalloc(INITDATASIZE);
 
        if (numcmds == 0) {
                (void) openhost(chosts[0]);
index ac8766bbf7032e1380020c3fb0a587380a8a79ae..d39eb67c6b2b0d9ffd7b8e40da2d4aee71c3ee37 100644 (file)
@@ -19,7 +19,6 @@ int   maxhostlen;
  * Declarations for command handlers in here
  */
 static int checkassocid        (u_int32);
-static char *  strsave         (char *);
 static struct varlist *findlistvar (struct varlist *, char *);
 static void    doaddvlist      (struct varlist *, char *);
 static void    dormvlist       (struct varlist *, char *);
@@ -225,29 +224,6 @@ checkassocid(
 }
 
 
-/*
- * strsave - save a string
- * XXX - should be in libntp.a
- */
-static char *
-strsave(
-       char *str
-       )
-{
-       char *cp;
-       u_int len;
-
-       len = strlen(str) + 1;
-       if ((cp = (char *)malloc(len)) == NULL) {
-               (void) fprintf(stderr, "Malloc failed!!\n");
-               exit(1);
-       }
-
-       memmove(cp, str, len);
-       return (cp);
-}
-
-
 /*
  * findlistvar - look for the named variable in a list and return if found
  */
@@ -291,14 +267,14 @@ doaddvlist(
                }
 
                if (vl->name == 0) {
-                       vl->name = strsave(name);
+                       vl->name = estrdup(name);
                } else if (vl->value != 0) {
                        free(vl->value);
                        vl->value = 0;
                }
 
                if (value != 0)
-                       vl->value = strsave(value);
+                       vl->value = estrdup(value);
        }
 }
 
index 299d29165f58c04b3adc5d84915402fcd521d8d1..c5883caa7aea7c12910df0e5c267652a25fbfbc4 100644 (file)
  */
 #pragma warning(disable: 4127) /* conditional expression is constant */
 
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
 #include <stdio.h>
 #include <string.h>
 #include <windows.h>
 #include <isc/list.h>
+#include "ntp_stdlib.h"
 
 
 /*
@@ -165,12 +169,10 @@ FormatError(int error) {
                NULL); 
 
        if (lpMsgBuf) {
-               lmsg = malloc(sizeof(*lmsg));
-               if (lmsg) {
-                       lmsg->code = error;
-                       lmsg->msg = lpMsgBuf;
-                       ISC_LIST_APPEND(errormsg_list, lmsg, link);
-               }
+               lmsg = emalloc(sizeof(*lmsg));
+               lmsg->code = error;
+               lmsg->msg = lpMsgBuf;
+               ISC_LIST_APPEND(errormsg_list, lmsg, link);
        }
        UNLOCK(&ErrorMsgLock);
        return (lpMsgBuf);
@@ -235,6 +237,7 @@ isc__NTstrerror(int err) {
        #endif /* _MSC_VER */
 
        if (!retmsg) {
+#undef strerror
                retmsg = strerror(err);
        }
 
index f900fbf7bb057a96d932acb96c83c5dbf32c712e..9d5bb469e2bfb1e7b6eeece61c1f1167b99b4243 100644 (file)
@@ -101,11 +101,12 @@ GetHeapAlloc(char *fromfunc)
        IoCompletionInfo *lpo;
 
 #ifdef USE_HEAP
-       lpo = (IoCompletionInfo *) HeapAlloc(hHeapHandle,
-                            HEAP_ZERO_MEMORY,
-                            sizeof(IoCompletionInfo));
+       lpo = HeapAlloc(hHeapHandle,
+                       HEAP_ZERO_MEMORY,
+                       sizeof(IoCompletionInfo));
 #else
-       lpo = (IoCompletionInfo *) calloc(1, sizeof(*lpo));
+       lpo = emalloc(sizeof(*lpo));
+       memset(lpo, 0, sizeof(*lpo));
 #endif
        DPRINTF(3, ("Allocation %d memory for %s, ptr %x\n", sizeof(IoCompletionInfo), fromfunc, lpo));
 
@@ -879,7 +880,9 @@ io_completion_port_sendto(
                         * Something bad happened
                         */
                        default :
-                               msyslog(LOG_ERR, "WSASendTo - error sending message: %m");
+                               msyslog(LOG_ERR,
+                                       "WSASendTo(%s) error %d: %s",
+                                       stoa(dest), errval, strerror(errval));
                                free_trans_buf(buff);
                                lpo->trans_buf = NULL;
                                FreeHeap(lpo, "io_completion_port_sendto");