diff --git a/bin/delv/delv.c b/bin/delv/delv.c index 9b96154ec3..2560331143 100644 --- a/bin/delv/delv.c +++ b/bin/delv/delv.c @@ -978,8 +978,7 @@ plus_option(char *option) { 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) { diff --git a/bin/dig/dig.c b/bin/dig/dig.c index 6735c1b84b..562ef79a73 100644 --- a/bin/dig/dig.c +++ b/bin/dig/dig.c @@ -669,33 +669,27 @@ cleanup: 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), @@ -703,9 +697,8 @@ printgreeting(int argc, char **argv, dig_lookup_t *lookup) { 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)); } } } @@ -728,8 +721,7 @@ plus_option(const char *option, isc_boolean_t is_batchfile, 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) { @@ -905,8 +897,7 @@ plus_option(const char *option, isc_boolean_t is_batchfile, 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"); @@ -1598,8 +1589,7 @@ dash_option(char *option, char *next, dig_lookup_t **lookup, 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"); @@ -1613,9 +1603,8 @@ dash_option(char *option, char *next, dig_lookup_t **lookup, (*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; @@ -1699,10 +1688,8 @@ dash_option(char *option, char *next, dig_lookup_t **lookup, #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) @@ -1710,9 +1697,8 @@ dash_option(char *option, char *next, dig_lookup_t **lookup, *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); @@ -2006,9 +1992,8 @@ parse_args(isc_boolean_t is_batchfile, isc_boolean_t config_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; diff --git a/bin/dig/host.c b/bin/dig/host.c index 38bdba3faa..13800472e1 100644 --- a/bin/dig/host.c +++ b/bin/dig/host.c @@ -463,9 +463,8 @@ printmessage(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers) { 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; @@ -474,9 +473,8 @@ printmessage(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers) { } 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; @@ -861,14 +859,12 @@ parse_args(isc_boolean_t is_batchfile, int argc, char **argv) { 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; diff --git a/bin/dig/nslookup.c b/bin/dig/nslookup.c index 314d86b3c6..131c9d6b4f 100644 --- a/bin/dig/nslookup.c +++ b/bin/dig/nslookup.c @@ -479,9 +479,8 @@ printmessage(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers) { 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; diff --git a/bin/named/win32/ntservice.c b/bin/named/win32/ntservice.c index 71610ec630..17bb50527a 100644 --- a/bin/named/win32/ntservice.c +++ b/bin/named/win32/ntservice.c @@ -49,8 +49,8 @@ ntservice_init(void) { } UpdateSCM(SERVICE_RUNNING); } else { - strcpy(ConsoleTitle, "BIND Version "); - strcat(ConsoleTitle, VERSION); + strlcpy(ConsoleTitle, "BIND Version ", sizeof(ConsoleTitle)); + strlcat(ConsoleTitle, VERSION, sizeof(ConsoleTitle)); SetConsoleTitle(ConsoleTitle); } } diff --git a/bin/nsupdate/nsupdate.c b/bin/nsupdate/nsupdate.c index e5436e753c..2fc565c681 100644 --- a/bin/nsupdate/nsupdate.c +++ b/bin/nsupdate/nsupdate.c @@ -453,8 +453,8 @@ parse_hmac(const dns_name_t **hmac, const char *hmacstr, size_t len, 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) { diff --git a/bin/tests/sock_test.c b/bin/tests/sock_test.c index 0c68552cf0..0ade9e7444 100644 --- a/bin/tests/sock_test.c +++ b/bin/tests/sock_test.c @@ -106,7 +106,7 @@ my_recv(isc_task_t *task, isc_event_t *event) { 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); @@ -179,7 +179,7 @@ my_connect(isc_task_t *task, isc_event_t *event) { 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; } diff --git a/bin/tools/mdig.c b/bin/tools/mdig.c index c1204495a2..bddf35d2e6 100644 --- a/bin/tools/mdig.c +++ b/bin/tools/mdig.c @@ -1041,8 +1041,7 @@ plus_option(char *option, struct query *query, isc_boolean_t global) 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) { @@ -1657,8 +1656,7 @@ dash_option(const char *option, char *next, struct query *query, 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; @@ -1845,9 +1843,8 @@ parse_args(isc_boolean_t is_batchfile, int argc, char **argv) */ 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(); diff --git a/lib/dns/rcode.c b/lib/dns/rcode.c index 723c0183fb..8cad290937 100644 --- a/lib/dns/rcode.c +++ b/lib/dns/rcode.c @@ -252,8 +252,8 @@ maybe_numeric(unsigned int *valuep, isc_textregion_t *source, * 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'); @@ -504,8 +504,12 @@ dns_rdataclass_fromtext(dns_rdataclass_t *classp, isc_textregion_t *source) { 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; diff --git a/lib/dns/rdata.c b/lib/dns/rdata.c index b2e0819174..fd55eff695 100644 --- a/lib/dns/rdata.c +++ b/lib/dns/rdata.c @@ -1339,8 +1339,12 @@ dns_rdatatype_fromtext(dns_rdatatype_t *typep, isc_textregion_t *source) { 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; diff --git a/lib/dns/ttl.c b/lib/dns/ttl.c index 35cbc2a3d8..223d389fcc 100644 --- a/lib/dns/ttl.c +++ b/lib/dns/ttl.c @@ -159,8 +159,8 @@ bind_ttl(isc_textregion_t *source, isc_uint32_t *ttl) { */ 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 { diff --git a/lib/dns/zone.c b/lib/dns/zone.c index 80c8035b90..514c597472 100644 --- a/lib/dns/zone.c +++ b/lib/dns/zone.c @@ -1372,7 +1372,7 @@ dns_zone_getdbtype(dns_zone_t *zone, char ***argv, isc_mem_t *mctx) { 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); @@ -1383,14 +1383,13 @@ dns_zone_getdbtype(dns_zone_t *zone, char ***argv, isc_mem_t *mctx) { 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; diff --git a/lib/irs/getaddrinfo.c b/lib/irs/getaddrinfo.c index 2b72c22af3..b455666969 100644 --- a/lib/irs/getaddrinfo.c +++ b/lib/irs/getaddrinfo.c @@ -374,8 +374,7 @@ getaddrinfo(const char *hostname, const char *servname, */ 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; diff --git a/lib/isc/mem.c b/lib/isc/mem.c index 6de96efbef..9d421b86c5 100644 --- a/lib/isc/mem.c +++ b/lib/isc/mem.c @@ -1671,12 +1671,12 @@ isc___mem_strdup(isc_mem_t *mctx0, const char *s FLARG) { 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); } @@ -1827,8 +1827,7 @@ isc_mem_setname(isc_mem_t *ctx0, const char *name, void *tag) { 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); } @@ -1913,8 +1912,7 @@ isc__mempool_setname(isc_mempool_t *mpctx0, const char *name) { 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); diff --git a/lib/isc/pk11.c b/lib/isc/pk11.c index a7347d1d2d..00e8f48584 100644 --- a/lib/isc/pk11.c +++ b/lib/isc/pk11.c @@ -408,9 +408,13 @@ pk11_get_session(pk11_context_t *ctx, pk11_optype_t optype, /* 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; diff --git a/lib/isc/task.c b/lib/isc/task.c index c2a371faef..7d2857f228 100644 --- a/lib/isc/task.c +++ b/lib/isc/task.c @@ -882,8 +882,7 @@ isc__task_setname(isc_task_t *task0, const char *name, void *tag) { 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); } diff --git a/lib/isc/unix/file.c b/lib/isc/unix/file.c index 6d31eb74d8..841b070f59 100644 --- a/lib/isc/unix/file.c +++ b/lib/isc/unix/file.c @@ -234,11 +234,12 @@ isc_file_template(const char *path, const char *templet, char *buf, 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) diff --git a/lib/isc/unix/interfaceiter.c b/lib/isc/unix/interfaceiter.c index 1d24b842b7..330545ae0c 100644 --- a/lib/isc/unix/interfaceiter.c +++ b/lib/isc/unix/interfaceiter.c @@ -229,7 +229,7 @@ linux_if_inet6_current(isc_interfaceiter_t *iter) { } } 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 diff --git a/lib/isc/unix/socket.c b/lib/isc/unix/socket.c index 61b598aa50..0f6ea564f6 100644 --- a/lib/isc/unix/socket.c +++ b/lib/isc/unix/socket.c @@ -5634,7 +5634,7 @@ isc__socket_filter(isc_socket_t *sock0, const char *filter) { #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)); @@ -6519,8 +6519,7 @@ isc__socket_setname(isc_socket_t *socket0, const char *name, void *tag) { 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); } diff --git a/lib/isc/unix/time.c b/lib/isc/unix/time.c index 49aa967e97..71bf8504ca 100644 --- a/lib/isc/unix/time.c +++ b/lib/isc/unix/time.c @@ -394,8 +394,7 @@ isc_time_formattimestamp(const isc_time_t *t, char *buf, unsigned int len) { 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); } } diff --git a/lib/isc/win32/file.c b/lib/isc/win32/file.c index 2f81f8a69b..fce055a6b4 100644 --- a/lib/isc/win32/file.c +++ b/lib/isc/win32/file.c @@ -334,11 +334,12 @@ isc_file_template(const char *path, const char *templet, char *buf, 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) @@ -616,8 +617,8 @@ isc_file_progname(const char *filename, char *progname, size_t namelen) { 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); } diff --git a/lib/isc/win32/fsaccess.c b/lib/isc/win32/fsaccess.c index ffd6e23ab5..de3e6c9e00 100644 --- a/lib/isc/win32/fsaccess.c +++ b/lib/isc/win32/fsaccess.c @@ -70,11 +70,9 @@ is_ntfs(const char * file) { */ 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, "\\"); @@ -85,13 +83,12 @@ is_ntfs(const char * file) { 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); diff --git a/lib/isc/win32/ntpaths.c b/lib/isc/win32/ntpaths.c index 5c08599280..08cd895309 100644 --- a/lib/isc/win32/ntpaths.c +++ b/lib/isc/win32/ntpaths.c @@ -46,7 +46,7 @@ isc_ntpaths_init(void) { 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; @@ -61,33 +61,35 @@ isc_ntpaths_init(void) { 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)); diff --git a/lib/isc/win32/socket.c b/lib/isc/win32/socket.c index eb4c2e6c6b..fa752dc1b0 100644 --- a/lib/isc/win32/socket.c +++ b/lib/isc/win32/socket.c @@ -3946,8 +3946,7 @@ isc__socket_setname(isc_socket_t *socket, const char *name, void *tag) { 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); } diff --git a/lib/isc/win32/time.c b/lib/isc/win32/time.c index 037063bc15..fe41d85cf0 100644 --- a/lib/isc/win32/time.c +++ b/lib/isc/win32/time.c @@ -283,8 +283,7 @@ isc_time_formattimestamp(const isc_time_t *t, char *buf, unsigned int len) { 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); } } diff --git a/lib/ns/interfacemgr.c b/lib/ns/interfacemgr.c index 461692ba1c..be819a7d6e 100644 --- a/lib/ns/interfacemgr.c +++ b/lib/ns/interfacemgr.c @@ -395,8 +395,7 @@ ns_interface_create(ns_interfacemgr_t *mgr, isc_sockaddr_t *addr, 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);