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;
*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)
#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);
}
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;
}
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;
}
* 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
/*
* 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);
/*
#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 */
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);
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;
if (s != t)
*s++ = ',';
- strncpy(s, k->text,
+ memcpy(s, k->text,
(unsigned)i);
s += i;
}
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);
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;
if (s != t)
*s++ = ',';
- strcpy(s, k->text);
+ memcpy(s, k->text, i);
s += i;
}
if (s+2 >= be)
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;
if (s != t)
*s++ = ',';
- strcpy(s, k->text);
+ memcpy(s, k->text, i);
s += i;
}
if (s != t)
*s++ = ',';
- strncpy(s, k->text, (unsigned)i);
+ memcpy(s, k->text, (unsigned)i);
s += i;
*s = '\0';
}
{
char statstr[NTP_MAXSTRLEN];
int i;
+ size_t len;
/*
* Report the error to the protostats file, system log and
"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);
"%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);
(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.
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) {
)
{
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;
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;
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])
; /* 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
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] = ' ';
* 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;
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;
}
{
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;
(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")) ==
}
}
- if( chrootdir ) {
+ 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);
* 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;
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 */
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 *));
/* 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 */
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).
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);
}
}
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 */
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;
}
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;
/*
* 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]);
* 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 *);
}
-/*
- * 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
*/
}
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);
}
}
*/
#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"
/*
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);
#endif /* _MSC_VER */
if (!retmsg) {
+#undef strerror
retmsg = strerror(err);
}
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));
* 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");