char *cmd, *value, *ptr;
isc_boolean_t state = ISC_TRUE;
- strncpy(option_store, option, sizeof(option_store));
- option_store[sizeof(option_store)-1]=0;
+ strlcpy(option_store, option, sizeof(option_store));
ptr = option_store;
cmd = next_token(&ptr,"=");
if (cmd == NULL) {
static void
printgreeting(int argc, char **argv, dig_lookup_t *lookup) {
int i;
- size_t remaining;
static isc_boolean_t first = ISC_TRUE;
char append[MXNAME];
if (printcmd) {
- lookup->cmdline[sizeof(lookup->cmdline) - 1] = 0;
snprintf(lookup->cmdline, sizeof(lookup->cmdline),
"%s; <<>> DiG " VERSION " <<>>",
first?"\n":"");
i = 1;
while (i < argc) {
snprintf(append, sizeof(append), " %s", argv[i++]);
- remaining = sizeof(lookup->cmdline) -
- strlen(lookup->cmdline) - 1;
- strncat(lookup->cmdline, append, remaining);
+ strlcat(lookup->cmdline, append,
+ sizeof(lookup->cmdline));
}
- remaining = sizeof(lookup->cmdline) -
- strlen(lookup->cmdline) - 1;
- strncat(lookup->cmdline, "\n", remaining);
+ strlcat(lookup->cmdline, "\n", sizeof(lookup->cmdline));
if (first && addresscount != 0) {
snprintf(append, sizeof(append),
"; (%d server%s found)\n",
addresscount,
addresscount > 1 ? "s" : "");
- remaining = sizeof(lookup->cmdline) -
- strlen(lookup->cmdline) - 1;
- strncat(lookup->cmdline, append, remaining);
+ strlcat(lookup->cmdline, append,
+ sizeof(lookup->cmdline));
}
if (first) {
snprintf(append, sizeof(append),
short_form ? " +short" : "",
printcmd ? " +cmd" : "");
first = ISC_FALSE;
- remaining = sizeof(lookup->cmdline) -
- strlen(lookup->cmdline) - 1;
- strncat(lookup->cmdline, append, remaining);
+ strlcat(lookup->cmdline, append,
+ sizeof(lookup->cmdline));
}
}
}
isc_boolean_t state = ISC_TRUE;
size_t n;
- strncpy(option_store, option, sizeof(option_store));
- option_store[sizeof(option_store)-1]=0;
+ strlcpy(option_store, option, sizeof(option_store));
ptr = option_store;
cmd = next_token(&ptr, "=");
if (cmd == NULL) {
goto need_value;
if (!state)
goto invalid_option;
- strncpy(domainopt, value, sizeof(domainopt));
- domainopt[sizeof(domainopt)-1] = '\0';
+ strlcpy(domainopt, value, sizeof(domainopt));
break;
case 's': /* dscp */
FULLCHECK("dscp");
batchname = value;
return (value_from_next);
case 'k':
- strncpy(keyfile, value, sizeof(keyfile));
- keyfile[sizeof(keyfile)-1]=0;
+ strlcpy(keyfile, value, sizeof(keyfile));
return (value_from_next);
case 'p':
result = parse_uint(&num, value, MAXPORT, "port number");
(*lookup) = clone_lookup(default_lookup,
ISC_TRUE);
*need_clone = ISC_TRUE;
- strncpy((*lookup)->textname, value,
+ strlcpy((*lookup)->textname, value,
sizeof((*lookup)->textname));
- (*lookup)->textname[sizeof((*lookup)->textname)-1]=0;
(*lookup)->trace_root = ISC_TF((*lookup)->trace ||
(*lookup)->ns_search_only);
(*lookup)->new_search = ISC_TRUE;
#endif
digestbits = 0;
}
- strncpy(keynametext, ptr, sizeof(keynametext));
- keynametext[sizeof(keynametext)-1]=0;
- strncpy(keysecret, ptr2, sizeof(keysecret));
- keysecret[sizeof(keysecret)-1]=0;
+ strlcpy(keynametext, ptr, sizeof(keynametext));
+ strlcpy(keysecret, ptr2, sizeof(keysecret));
return (value_from_next);
case 'x':
if (*need_clone)
*need_clone = ISC_TRUE;
if (get_reverse(textname, sizeof(textname), value,
ip6_int, ISC_FALSE) == ISC_R_SUCCESS) {
- strncpy((*lookup)->textname, textname,
+ strlcpy((*lookup)->textname, textname,
sizeof((*lookup)->textname));
- (*lookup)->textname[sizeof((*lookup)->textname)-1] = 0;
debug("looking up %s", (*lookup)->textname);
(*lookup)->trace_root = ISC_TF((*lookup)->trace ||
(*lookup)->ns_search_only);
lookup = clone_lookup(default_lookup,
ISC_TRUE);
need_clone = ISC_TRUE;
- strncpy(lookup->textname, rv[0],
+ strlcpy(lookup->textname, rv[0],
sizeof(lookup->textname));
- lookup->textname[sizeof(lookup->textname)-1]=0;
lookup->trace_root = ISC_TF(lookup->trace ||
lookup->ns_search_only);
lookup->new_search = ISC_TRUE;
dns_name_format(name, namestr, sizeof(namestr));
lookup = clone_lookup(query->lookup, ISC_FALSE);
if (lookup != NULL) {
- strncpy(lookup->textname, namestr,
+ strlcpy(lookup->textname, namestr,
sizeof(lookup->textname));
- lookup->textname[sizeof(lookup->textname)-1] = 0;
lookup->rdtype = dns_rdatatype_aaaa;
lookup->rdtypeset = ISC_TRUE;
lookup->origin = NULL;
}
lookup = clone_lookup(query->lookup, ISC_FALSE);
if (lookup != NULL) {
- strncpy(lookup->textname, namestr,
+ strlcpy(lookup->textname, namestr,
sizeof(lookup->textname));
- lookup->textname[sizeof(lookup->textname)-1] = 0;
lookup->rdtype = dns_rdatatype_mx;
lookup->rdtypeset = ISC_TRUE;
lookup->origin = NULL;
lookup->pending = ISC_FALSE;
if (get_reverse(store, sizeof(store), hostname,
lookup->ip6_int, ISC_TRUE) == ISC_R_SUCCESS) {
- strncpy(lookup->textname, store, sizeof(lookup->textname));
- lookup->textname[sizeof(lookup->textname)-1] = 0;
+ strlcpy(lookup->textname, store, sizeof(lookup->textname));
lookup->rdtype = dns_rdatatype_ptr;
lookup->rdtypeset = ISC_TRUE;
default_lookups = ISC_FALSE;
} else {
- strncpy(lookup->textname, hostname, sizeof(lookup->textname));
- lookup->textname[sizeof(lookup->textname)-1]=0;
+ strlcpy(lookup->textname, hostname, sizeof(lookup->textname));
usesearch = ISC_TRUE;
}
lookup->new_search = ISC_TRUE;
dns_name_format(name, namestr, sizeof(namestr));
lookup = clone_lookup(query->lookup, ISC_FALSE);
if (lookup != NULL) {
- strncpy(lookup->textname, namestr,
+ strlcpy(lookup->textname, namestr,
sizeof(lookup->textname));
- lookup->textname[sizeof(lookup->textname)-1] = 0;
lookup->rdtype = dns_rdatatype_aaaa;
lookup->rdtypeset = ISC_TRUE;
lookup->origin = NULL;
}
UpdateSCM(SERVICE_RUNNING);
} else {
- strcpy(ConsoleTitle, "BIND Version ");
- strcat(ConsoleTitle, VERSION);
+ strlcpy(ConsoleTitle, "BIND Version ", sizeof(ConsoleTitle));
+ strlcat(ConsoleTitle, VERSION, sizeof(ConsoleTitle));
SetConsoleTitle(ConsoleTitle);
}
}
return (ISC_FALSE);
}
- strncpy(buf, hmacstr, len);
- buf[len] = 0;
+ /* Copy len bytes and NUL terminate. */
+ strlcpy(buf, hmacstr, ISC_MIN(len + 1, sizeof(buf)));
#ifndef PK11_MD5_DISABLE
if (strcasecmp(buf, "hmac-md5") == 0) {
region.base = isc_mem_get(mctx, strlen(buf) + 1);
if (region.base != NULL) {
region.length = strlen(buf) + 1;
- strcpy((char *)region.base, buf); /* strcpy is safe */
+ strlcpy((char *)region.base, buf, region.length);
} else
region.length = 0;
isc_socket_send(sock, ®ion, task, my_send, event->ev_arg);
region.base = isc_mem_get(mctx, strlen(buf) + 1);
if (region.base != NULL) {
region.length = strlen(buf) + 1;
- strcpy((char *)region.base, buf); /* This strcpy is safe. */
+ strlcpy((char *)region.base, buf, region.length);
} else {
region.length = 0;
}
isc_boolean_t state = ISC_TRUE;
size_t n;
- strncpy(option_store, option, sizeof(option_store));
- option_store[sizeof(option_store) - 1] = 0;
+ strlcpy(option_store, option, sizeof(option_store));
ptr = option_store;
cmd = next_token(&ptr, "=");
if (cmd == NULL) {
return (value_from_next);
case 'x':
get_reverse(textname, sizeof(textname), value, query->ip6_int);
- strncpy(query->textname, textname, sizeof(query->textname));
- query->textname[sizeof(query->textname) - 1] = 0;
+ strlcpy(query->textname, textname, sizeof(query->textname));
query->rdtype = dns_rdatatype_ptr;
query->rdclass = dns_rdataclass_in;
*setname = ISC_TRUE;
*/
if (query == &default_query)
query = clone_default_query();
- strncpy(query->textname, rv[0],
+ strlcpy(query->textname, rv[0],
sizeof(query->textname));
- query->textname[sizeof(query->textname) - 1] = 0;
ISC_LIST_APPEND(queries, query, link);
query = clone_default_query();
* isc_parse_uint32(). isc_parse_uint32() requires
* null termination, so we must make a copy.
*/
- strncpy(buffer, source->base, sizeof(buffer));
- buffer[sizeof(buffer) - 1] = '\0';
+ strlcpy(buffer, source->base,
+ ISC_MIN(source->length + 1, sizeof(buffer)));
INSIST(buffer[source->length] == '\0');
char *endp;
unsigned int val;
- strncpy(buf, source->base + 5, source->length - 5);
- buf[source->length - 5] = '\0';
+ /*
+ * source->base is not required to be NUL terminated.
+ * Copy up to remaining bytes and NUL terminate.
+ */
+ strlcpy(buf, source->base + 5,
+ ISC_MIN(source->length - 5 + 1, sizeof(buf)));
val = strtoul(buf, &endp, 10);
if (*endp == '\0' && val <= 0xffff) {
*classp = (dns_rdataclass_t)val;
char *endp;
unsigned int val;
- strncpy(buf, source->base + 4, source->length - 4);
- buf[source->length - 4] = '\0';
+ /*
+ * source->base is not required to be NUL terminated.
+ * Copy up to remaining bytes and NUL terminate.
+ */
+ strlcpy(buf, source->base + 4,
+ ISC_MIN(source->length - 4 + 1, sizeof(buf)));
val = strtoul(buf, &endp, 10);
if (*endp == '\0' && val <= 0xffff) {
*typep = (dns_rdatatype_t)val;
*/
if (source->length > sizeof(buf) - 1)
return (DNS_R_SYNTAX);
- strncpy(buf, source->base, source->length);
- buf[source->length] = '\0';
+ /* Copy source->length bytes and NUL terminate. */
+ strlcpy(buf, source->base, ISC_MIN(source->length + 1, sizeof(buf)));
s = buf;
do {
unsigned int i;
isc_result_t result = ISC_R_SUCCESS;
void *mem;
- char **tmp, *tmp2;
+ char **tmp, *tmp2, *base;
REQUIRE(DNS_ZONE_VALID(zone));
REQUIRE(argv != NULL && *argv == NULL);
size += strlen(zone->db_argv[i]) + 1;
mem = isc_mem_allocate(mctx, size);
if (mem != NULL) {
- size_t remaining;
tmp = mem;
tmp2 = mem;
+ base = mem;
tmp2 += (zone->db_argc + 1) * sizeof(char *);
for (i = 0; i < zone->db_argc; i++) {
- remaining = size - (tmp2 - (char *) mem);
*tmp++ = tmp2;
- strlcpy(tmp2, zone->db_argv[i], remaining);
+ strlcpy(tmp2, zone->db_argv[i], size - (tmp2 - base));
tmp2 += strlen(tmp2) + 1;
}
*tmp = NULL;
*/
ntmp[0] = '\0';
if (strchr(hostname, '%') != NULL) {
- strncpy(ntmp, hostname, sizeof(ntmp) - 1);
- ntmp[sizeof(ntmp) - 1] = '\0';
+ strlcpy(ntmp, hostname, sizeof(ntmp));
p = strchr(ntmp, '%');
ep = NULL;
REQUIRE(VALID_CONTEXT(mctx));
REQUIRE(s != NULL);
- len = strlen(s);
+ len = strlen(s) + 1;
- ns = isc__mem_allocate((isc_mem_t *)mctx, len + 1 FLARG_PASS);
+ ns = isc__mem_allocate((isc_mem_t *)mctx, len FLARG_PASS);
if (ns != NULL)
- strncpy(ns, s, len + 1);
+ strlcpy(ns, s, len);
return (ns);
}
REQUIRE(VALID_CONTEXT(ctx));
LOCK(&ctx->lock);
- memset(ctx->name, 0, sizeof(ctx->name));
- strncpy(ctx->name, name, sizeof(ctx->name) - 1);
+ strlcpy(ctx->name, name, sizeof(ctx->name));
ctx->tag = tag;
UNLOCK(&ctx->lock);
}
if (mpctx->lock != NULL)
LOCK(mpctx->lock);
- strncpy(mpctx->name, name, sizeof(mpctx->name) - 1);
- mpctx->name[sizeof(mpctx->name) - 1] = '\0';
+ strlcpy(mpctx->name, name, sizeof(mpctx->name));
if (mpctx->lock != NULL)
UNLOCK(mpctx->lock);
/* Override the token's PIN */
if (logon && pin != NULL && *pin != '\0') {
if (strlen(pin) > PINLEN)
- return ISC_R_RANGE;
- memset(token->pin, 0, PINLEN + 1);
- strncpy(token->pin, pin, PINLEN);
+ return (ISC_R_RANGE);
+ /*
+ * We want to zero out the old pin before
+ * overwriting with a new one.
+ */
+ memset(token->pin, 0, sizeof(token->pin));
+ strlcpy(token->pin, pin, sizeof(token->pin));
}
freelist = &token->sessions;
REQUIRE(VALID_TASK(task));
LOCK(&task->lock);
- memset(task->name, 0, sizeof(task->name));
- strncpy(task->name, name, sizeof(task->name) - 1);
+ strlcpy(task->name, name, sizeof(task->name));
task->tag = tag;
UNLOCK(&task->lock);
}
s = strrchr(path, '/');
if (s != NULL) {
- if ((s - path + 1 + strlen(templet) + 1) > buflen)
+ size_t prefixlen = s - path + 1;
+ if ((prefixlen + strlen(templet) + 1) > buflen)
return (ISC_R_NOSPACE);
- strncpy(buf, path, s - path + 1);
- buf[s - path + 1] = '\0';
+ /* Copy 'prefixlen' bytes and NUL terminate. */
+ strlcpy(buf, path, ISC_MIN(prefixlen + 1, buflen));
strlcat(buf, templet, buflen);
} else {
if ((strlen(templet) + 1) > buflen)
}
}
isc_netaddr_fromin6(&iter->current.netmask, &addr6);
- strncpy(iter->current.name, name, sizeof(iter->current.name));
+ strlcpy(iter->current.name, name, sizeof(iter->current.name));
return (ISC_R_SUCCESS);
}
#endif
#if defined(SO_ACCEPTFILTER) && defined(ENABLE_ACCEPTFILTER)
bzero(&afa, sizeof(afa));
- strncpy(afa.af_name, filter, sizeof(afa.af_name));
+ strlcpy(afa.af_name, filter, sizeof(afa.af_name));
if (setsockopt(sock->fd, SOL_SOCKET, SO_ACCEPTFILTER,
&afa, sizeof(afa)) == -1) {
isc__strerror(errno, strbuf, sizeof(strbuf));
REQUIRE(VALID_SOCKET(sock));
LOCK(&sock->lock);
- memset(sock->name, 0, sizeof(sock->name));
- strncpy(sock->name, name, sizeof(sock->name) - 1);
+ strlcpy(sock->name, name, sizeof(sock->name));
sock->tag = tag;
UNLOCK(&sock->lock);
}
snprintf(buf + flen, len - flen,
".%03u", t->nanoseconds / NS_PER_MS);
else {
- strncpy(buf, "99-Bad-9999 99:99:99.999", len);
- buf[len - 1] = 0;
+ strlcpy(buf, "99-Bad-9999 99:99:99.999", len);
}
}
s = strrchr(path, '\\');
if (s != NULL) {
- if ((s - path + 1 + strlen(templet) + 1) > (ssize_t)buflen)
+ size_t prefixlen = s - path + 1;
+ if ((prefixlen + strlen(templet) + 1) > (ssize_t)buflen)
return (ISC_R_NOSPACE);
- strncpy(buf, path, s - path + 1);
- buf[s - path + 1] = '\0';
+ /* Copy 'prefixlen' bytes and NUL terminate. */
+ strlcpy(buf, path, ISC_MIN(prefixlen + 1, buflen));
strlcat(buf, templet, buflen);
} else {
if ((strlen(templet) + 1) > buflen)
if (len >= namelen)
return (ISC_R_NOSPACE);
- strlcpy(progname, s, len);
- progname[len] = '\0';
+ /* Copy up to 'len' bytes and NUL terminate. */
+ strlcpy(progname, s, ISC_MIN(len + 1, namelen));
return (ISC_R_SUCCESS);
}
*/
if (isalpha(filename[0]) && filename[1] == ':' &&
(filename[2] == '\\' || filename[2] == '/')) {
- strncpy(drive, filename, 3);
- drive[3] = '\0';
- }
-
- else if ((filename[0] == '\\') && (filename[1] == '\\')) {
+ /* Copy 'c:\' or 'c:/' and NUL terminate. */
+ strlcpy(drive, filename, ISC_MIN(3 + 1, sizeof(drive)));
+ } else if ((filename[0] == '\\') && (filename[1] == '\\')) {
/* Find the machine and share name and rebuild the UNC */
strlcpy(tmpbuf, filename, sizeof(tmpbuf));
machinename = strtok(tmpbuf, "\\");
strlcat(drive, sharename, sizeof(drive));
strlcat(drive, "\\", sizeof(drive));
- }
- else /* Not determinable */
+ } else /* Not determinable */
return (FALSE);
GetVolumeInformation(drive, NULL, 0, NULL, 0, NULL, FSType,
sizeof(FSType));
- if(strcmp(FSType,"NTFS") == 0)
+ if (strcmp(FSType, "NTFS") == 0)
return (TRUE);
else
return (FALSE);
HKEY hKey;
BOOL keyFound = TRUE;
- memset(namedBase, 0, MAX_PATH);
+ memset(namedBase, 0, sizeof(namedBase));
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, BIND_SUBKEY, 0, KEY_READ, &hKey)
!= ERROR_SUCCESS)
keyFound = FALSE;
GetSystemDirectory(systemDir, MAX_PATH);
- if (keyFound == FALSE)
+ if (keyFound == FALSE) {
/* Use the System Directory as a default */
- strcpy(namedBase, systemDir);
+ strlcpy(namedBase, systemDir, sizeof(namedBase));
+ }
- strcpy(ns_confFile, namedBase);
- strcat(ns_confFile, "\\etc\\named.conf");
+ strlcpy(ns_confFile, namedBase, sizeof(ns_confFile));
+ strlcat(ns_confFile, "\\etc\\named.conf", sizeof(ns_confFile));
- strcpy(rndc_keyFile, namedBase);
- strcat(rndc_keyFile, "\\etc\\rndc.key");
+ strlcpy(rndc_keyFile, namedBase, sizeof(rndc_keyFile));
+ strlcat(rndc_keyFile, "\\etc\\rndc.key", sizeof(rndc_keyFile));
- strcpy(session_keyFile, namedBase);
- strcat(session_keyFile, "\\etc\\session.key");
+ strlcpy(session_keyFile, namedBase, sizeof(session_keyFile));
+ strlcat(session_keyFile, "\\etc\\session.key", sizeof(session_keyFile));
- strcpy(rndc_confFile, namedBase);
- strcat(rndc_confFile, "\\etc\\rndc.conf");
+ strlcpy(rndc_confFile, namedBase, sizeof(rndc_confFile));
+ strlcat(rndc_confFile, "\\etc\\rndc.conf", sizeof(rndc_confFile));
- strcpy(ns_defaultpidfile, namedBase);
- strcat(ns_defaultpidfile, "\\etc\\named.pid");
+ strlcpy(ns_defaultpidfile, namedBase, sizeof(ns_defaultpidfile));
+ strlcat(ns_defaultpidfile, "\\etc\\named.pid",
+ sizeof(ns_defaultpidfile));
- strcpy(ns_lockfile, namedBase);
- strcat(ns_lockfile, "\\etc\\named.lock");
+ strlcpy(ns_lockfile, namedBase, sizeof(ns_lockfile));
+ strlcat(ns_lockfile, "\\etc\\named.lock", sizeof(ns_lockfile));
- strcpy(local_state_dir, namedBase);
- strcat(local_state_dir, "\\bin");
+ strlcpy(local_state_dir, namedBase, sizeof(local_state_dir));
+ strlcat(local_state_dir, "\\bin", sizeof(local_state_dir));
- strcpy(sys_conf_dir, namedBase);
- strcat(sys_conf_dir, "\\etc");
+ strlcpy(sys_conf_dir, namedBase, sizeof(sys_conf_dir));
+ strlcat(sys_conf_dir, "\\etc", sizeof(sys_conf_dir));
/* Added to avoid an assert on NULL value */
strlcpy(resolv_confFile, namedBase, sizeof(resolv_confFile));
REQUIRE(VALID_SOCKET(socket));
LOCK(&socket->lock);
- memset(socket->name, 0, sizeof(socket->name));
- strncpy(socket->name, name, sizeof(socket->name) - 1);
+ strlcpy(socket->name, name, sizeof(socket->name));
socket->tag = tag;
UNLOCK(&socket->lock);
}
st.wMilliseconds);
} else {
- strncpy(buf, "99-Bad-9999 99:99:99.999", len);
- buf[len - 1] = 0;
+ strlcpy(buf, "99-Bad-9999 99:99:99.999", len);
}
}
ifp->generation = mgr->generation;
ifp->addr = *addr;
ifp->flags = 0;
- strncpy(ifp->name, name, sizeof(ifp->name));
- ifp->name[sizeof(ifp->name)-1] = '\0';
+ strlcpy(ifp->name, name, sizeof(ifp->name));
ifp->clientmgr = NULL;
result = isc_mutex_init(&ifp->lock);