diff options
author | Todd T. Fries <todd@cvs.openbsd.org> | 2003-06-27 19:08:24 +0000 |
---|---|---|
committer | Todd T. Fries <todd@cvs.openbsd.org> | 2003-06-27 19:08:24 +0000 |
commit | d86da5bd6abde93d50f7b724a52771877c8cabbc (patch) | |
tree | 898d808c8e5b8d3698e098bb3660051ff61e7964 /usr.sbin/bind | |
parent | 6af2197a68b3a0e9ce04f275a4ad9cd3d800e13d (diff) |
string cleaning; "Put it in. If it breaks, tough" Theo.
work by me, useful feedback from krw, jakob, tedu, and tholo
Diffstat (limited to 'usr.sbin/bind')
33 files changed, 102 insertions, 114 deletions
diff --git a/usr.sbin/bind/bin/dig/dig.c b/usr.sbin/bind/bin/dig/dig.c index bcbb3ac99cd..71045bbff95 100644 --- a/usr.sbin/bind/bin/dig/dig.c +++ b/usr.sbin/bind/bin/dig/dig.c @@ -619,7 +619,7 @@ plus_option(char *option, isc_boolean_t is_batchfile, char *cmd, *value, *ptr; isc_boolean_t state = ISC_TRUE; - strncpy(option_store, option, sizeof(option_store)); + strlcpy(option_store, option, sizeof(option_store)); option_store[sizeof(option_store)-1]=0; ptr = option_store; cmd=next_token(&ptr,"="); @@ -718,8 +718,7 @@ plus_option(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; default: goto invalid_option; @@ -960,8 +959,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': port = (in_port_t) parse_uint(value, "port number", MAXPORT); @@ -1014,18 +1012,16 @@ dash_option(char *option, char *next, dig_lookup_t **lookup, if (ptr == NULL) { usage(); } - strncpy(keynametext, ptr, sizeof(keynametext)); - keynametext[sizeof(keynametext)-1]=0; + strlcpy(keynametext, ptr, sizeof(keynametext)); ptr = next_token(&value, ""); if (ptr == NULL) usage(); - strncpy(keysecret, ptr, sizeof(keysecret)); - keysecret[sizeof(keysecret)-1]=0; + strlcpy(keysecret, ptr, sizeof(keysecret)); return (value_from_next); case 'x': *lookup = clone_lookup(default_lookup, ISC_TRUE); if (get_reverse(textname, value, nibble) == ISC_R_SUCCESS) { - strncpy((*lookup)->textname, textname, + strlcpy((*lookup)->textname, textname, sizeof((*lookup)->textname)); debug("looking up %s", (*lookup)->textname); (*lookup)->trace_root = ISC_TF((*lookup)->trace || @@ -1254,9 +1250,8 @@ parse_args(isc_boolean_t is_batchfile, isc_boolean_t config_only, printgreeting(argc, argv, lookup); firstarg = ISC_FALSE; } - 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/usr.sbin/bind/bin/dig/dighost.c b/usr.sbin/bind/bin/dig/dighost.c index db7f0561fe7..67364c7d861 100644 --- a/usr.sbin/bind/bin/dig/dighost.c +++ b/usr.sbin/bind/bin/dig/dighost.c @@ -300,8 +300,7 @@ make_server(const char *servname) { if (srv == NULL) fatal("Memory allocation failure in %s:%d", __FILE__, __LINE__); - strncpy(srv->servername, servname, MXNAME); - srv->servername[MXNAME-1] = 0; + strlcpy(srv->servername, servname, MXNAME); ISC_LINK_INIT(srv, link); return (srv); } @@ -410,9 +409,8 @@ clone_lookup(dig_lookup_t *lookold, isc_boolean_t servers) { looknew = make_empty_lookup(); INSIST(looknew != NULL); - strncpy(looknew->textname, lookold->textname, MXNAME); - strncpy(looknew->cmdline, lookold->cmdline, MXNAME); - looknew->textname[MXNAME-1] = 0; + strlcpy(looknew->textname, lookold->textname, MXNAME); + strlcpy(looknew->cmdline, lookold->cmdline, MXNAME); looknew->rdtype = lookold->rdtype; looknew->qrdtype = lookold->qrdtype; looknew->rdclass = lookold->rdclass; @@ -562,8 +560,7 @@ make_searchlist_entry(char *domain) { if (search == NULL) fatal("Memory allocation failure in %s:%d", __FILE__, __LINE__); - strncpy(search->origin, domain, MXNAME); - search->origin[MXNAME-1] = 0; + strlcpy(search->origin, domain, MXNAME); ISC_LINK_INIT(search, link); return (search); } diff --git a/usr.sbin/bind/bin/dig/host.c b/usr.sbin/bind/bin/dig/host.c index c3a0c0bedf9..82435a1450f 100644 --- a/usr.sbin/bind/bin/dig/host.c +++ b/usr.sbin/bind/bin/dig/host.c @@ -644,8 +644,7 @@ parse_args(isc_boolean_t is_batchfile, int argc, char **argv) { if (isc_commandline_index >= argc) { show_usage(); } - strncpy(hostname, argv[isc_commandline_index], sizeof(hostname)); - hostname[sizeof(hostname)-1]=0; + strlcpy(hostname, argv[isc_commandline_index], sizeof(hostname)); if (argc > isc_commandline_index + 1) { srv = make_server(argv[isc_commandline_index+1]); debug("server is %s", srv->servername); @@ -655,13 +654,11 @@ parse_args(isc_boolean_t is_batchfile, int argc, char **argv) { lookup->pending = ISC_FALSE; if (get_reverse(store, hostname, lookup->nibble) == 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; } else { - strncpy(lookup->textname, hostname, sizeof(lookup->textname)); - lookup->textname[sizeof(lookup->textname)-1]=0; + strlcpy(lookup->textname, hostname, sizeof(lookup->textname)); } lookup->new_search = ISC_TRUE; ISC_LIST_APPEND(lookup_list, lookup, link); diff --git a/usr.sbin/bind/bin/dig/nslookup.c b/usr.sbin/bind/bin/dig/nslookup.c index 1126149d7ba..c4efb3f6819 100644 --- a/usr.sbin/bind/bin/dig/nslookup.c +++ b/usr.sbin/bind/bin/dig/nslookup.c @@ -518,8 +518,7 @@ testclass(char *typetext) { static void safecpy(char *dest, char *src, int size) { - strncpy(dest, src, size); - dest[size-1] = 0; + strlcpy(dest, src, size); } diff --git a/usr.sbin/bind/bin/named/interfacemgr.c b/usr.sbin/bind/bin/named/interfacemgr.c index 4ee7939cd8e..80b9342c96b 100644 --- a/usr.sbin/bind/bin/named/interfacemgr.c +++ b/usr.sbin/bind/bin/named/interfacemgr.c @@ -182,8 +182,7 @@ ns_interface_create(ns_interfacemgr_t *mgr, isc_sockaddr_t *addr, ifp->mgr = NULL; ifp->generation = mgr->generation; ifp->addr = *addr; - 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); diff --git a/usr.sbin/bind/lib/dns/rdata.c b/usr.sbin/bind/lib/dns/rdata.c index cb696841f85..3fddd4ea445 100644 --- a/usr.sbin/bind/lib/dns/rdata.c +++ b/usr.sbin/bind/lib/dns/rdata.c @@ -976,7 +976,7 @@ dns_mnemonic_fromtext(unsigned int *valuep, isc_textregion_t *source, * strtoul() requires null termination, so we must make * a copy. */ - strncpy(buffer, source->base, NUMBERSIZE); + strlcpy(buffer, source->base, NUMBERSIZE); INSIST(buffer[source->length] == '\0'); n = strtoul(buffer, &e, 10); @@ -1053,8 +1053,7 @@ 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'; + strlcpy(buf, source->base + 5, sizeof(buf)); val = strtoul(buf, &endp, 10); if (*endp == '\0' && val <= 0xffff) { *classp = (dns_rdataclass_t)val; @@ -1158,8 +1157,7 @@ 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'; + strlcpy(buf, source->base + 4, sizeof(buf)); val = strtoul(buf, &endp, 10); if (*endp == '\0' && val <= 0xffff) { *typep = (dns_rdatatype_t)val; @@ -1290,7 +1288,7 @@ dns_keyflags_fromtext(dns_keyflags_t *flagsp, isc_textregion_t *source) * strtoul() requires null termination, so we must make * a copy. */ - strncpy(buffer, source->base, NUMBERSIZE); + strlcpy(buffer, source->base, NUMBERSIZE); INSIST(buffer[source->length] == '\0'); n = strtoul(buffer, &e, 0); /* Allow hex/octal. */ diff --git a/usr.sbin/bind/lib/dns/rdata/any_255/tsig_250.c b/usr.sbin/bind/lib/dns/rdata/any_255/tsig_250.c index 53ec6292a1f..063f7bec37e 100644 --- a/usr.sbin/bind/lib/dns/rdata/any_255/tsig_250.c +++ b/usr.sbin/bind/lib/dns/rdata/any_255/tsig_250.c @@ -180,7 +180,7 @@ totext_any_tsig(ARGS_TOTEXT) { */ n = uint16_fromregion(&sr); isc_region_consume(&sr, 2); - sprintf(buf, "%u ", n); + snprintf(buf, sizeof(buf), "%u ", n); RETERR(str_totext(buf, target)); /* @@ -188,7 +188,7 @@ totext_any_tsig(ARGS_TOTEXT) { */ n = uint16_fromregion(&sr); isc_region_consume(&sr, 2); - sprintf(buf, "%u", n); + snprintf(buf, sizeof(buf), "%u", n); RETERR(str_totext(buf, target)); /* @@ -213,7 +213,7 @@ totext_any_tsig(ARGS_TOTEXT) { */ n = uint16_fromregion(&sr); isc_region_consume(&sr, 2); - sprintf(buf, "%u ", n); + snprintf(buf, sizeof(buf), "%u ", n); RETERR(str_totext(buf, target)); /* @@ -224,7 +224,7 @@ totext_any_tsig(ARGS_TOTEXT) { if (dns_tsigrcode_totext((dns_rcode_t)n, target) == ISC_R_SUCCESS) RETERR(str_totext(" ", target)); else { - sprintf(buf, "%u ", n); + snprintf(buf, sizeof(buf), "%u ", n); RETERR(str_totext(buf, target)); } @@ -233,7 +233,7 @@ totext_any_tsig(ARGS_TOTEXT) { */ n = uint16_fromregion(&sr); isc_region_consume(&sr, 2); - sprintf(buf, "%u ", n); + snprintf(buf, sizeof(buf), "%u ", n); RETERR(str_totext(buf, target)); /* diff --git a/usr.sbin/bind/lib/dns/rdata/generic/afsdb_18.c b/usr.sbin/bind/lib/dns/rdata/generic/afsdb_18.c index 30d677746c4..71c0fe51cab 100644 --- a/usr.sbin/bind/lib/dns/rdata/generic/afsdb_18.c +++ b/usr.sbin/bind/lib/dns/rdata/generic/afsdb_18.c @@ -77,7 +77,7 @@ totext_afsdb(ARGS_TOTEXT) { dns_rdata_toregion(rdata, ®ion); num = uint16_fromregion(®ion); isc_region_consume(®ion, 2); - sprintf(buf, "%u ", num); + snprintf(buf, sizeof(buf), "%u ", num); RETERR(str_totext(buf, target)); dns_name_fromregion(&name, ®ion); sub = name_prefix(&name, tctx->origin, &prefix); diff --git a/usr.sbin/bind/lib/dns/rdata/generic/cert_37.c b/usr.sbin/bind/lib/dns/rdata/generic/cert_37.c index 41059ef865a..2485a82d11a 100644 --- a/usr.sbin/bind/lib/dns/rdata/generic/cert_37.c +++ b/usr.sbin/bind/lib/dns/rdata/generic/cert_37.c @@ -94,7 +94,7 @@ totext_cert(ARGS_TOTEXT) { */ n = uint16_fromregion(&sr); isc_region_consume(&sr, 2); - sprintf(buf, "%u ", n); + snprintf(buf, sizeof(buf), "%u ", n); RETERR(str_totext(buf, target)); /* diff --git a/usr.sbin/bind/lib/dns/rdata/generic/key_25.c b/usr.sbin/bind/lib/dns/rdata/generic/key_25.c index bc5bcba1e43..7066465f915 100644 --- a/usr.sbin/bind/lib/dns/rdata/generic/key_25.c +++ b/usr.sbin/bind/lib/dns/rdata/generic/key_25.c @@ -85,19 +85,19 @@ totext_key(ARGS_TOTEXT) { /* flags */ flags = uint16_fromregion(&sr); isc_region_consume(&sr, 2); - sprintf(buf, "%u", flags); + snprintf(buf, sizeof(buf), "%u", flags); RETERR(str_totext(buf, target)); RETERR(str_totext(" ", target)); /* protocol */ - sprintf(buf, "%u", sr.base[0]); + snprintf(buf, sizeof(buf), "%u", sr.base[0]); isc_region_consume(&sr, 1); RETERR(str_totext(buf, target)); RETERR(str_totext(" ", target)); /* algorithm */ algorithm = sr.base[0]; - sprintf(buf, "%u", algorithm); + snprintf(buf, sizeof(buf), "%u", algorithm); isc_region_consume(&sr, 1); RETERR(str_totext(buf, target)); @@ -125,7 +125,7 @@ totext_key(ARGS_TOTEXT) { RETERR(str_totext(" ; key id = ", target)); dns_rdata_toregion(rdata, &tmpr); - sprintf(buf, "%u", dst_region_computeid(&tmpr, algorithm)); + snprintf(buf, sizeof(buf), "%u", dst_region_computeid(&tmpr, algorithm)); RETERR(str_totext(buf, target)); } return (ISC_R_SUCCESS); diff --git a/usr.sbin/bind/lib/dns/rdata/generic/loc_29.c b/usr.sbin/bind/lib/dns/rdata/generic/loc_29.c index 9451b0bfbba..09c4036df72 100644 --- a/usr.sbin/bind/lib/dns/rdata/generic/loc_29.c +++ b/usr.sbin/bind/lib/dns/rdata/generic/loc_29.c @@ -454,19 +454,25 @@ totext_loc(ARGS_TOTEXT) { /* version = sr.base[0]; */ size = sr.base[1]; if ((size&0x0f)> 1) - sprintf(sbuf, "%lum", (size>>4) * poweroften[(size&0x0f)-2]); + snprintf(sbuf, sizeof(sbuf), + "%lum", (size>>4) * poweroften[(size&0x0f)-2]); else - sprintf(sbuf, "0.%02lum", (size>>4) * poweroften[(size&0x0f)]); + snprintf(sbuf, sizeof(sbuf), + "0.%02lum", (size>>4) * poweroften[(size&0x0f)]); hp = sr.base[2]; if ((hp&0x0f)> 1) - sprintf(hbuf, "%lum", (hp>>4) * poweroften[(hp&0x0f)-2]); + snprintf(hbuf, sizeof(hbuf), + "%lum", (hp>>4) * poweroften[(hp&0x0f)-2]); else - sprintf(hbuf, "0.%02lum", (hp>>4) * poweroften[(hp&0x0f)]); + snprintf(hbuf, sizeof(hbuf), + "0.%02lum", (hp>>4) * poweroften[(hp&0x0f)]); vp = sr.base[3]; if ((vp&0x0f)> 1) - sprintf(vbuf, "%lum", (vp>>4) * poweroften[(vp&0x0f)-2]); + snprintf(vbuf, sizeof(vbuf), + "%lum", (vp>>4) * poweroften[(vp&0x0f)-2]); else - sprintf(vbuf, "0.%02lum", (vp>>4) * poweroften[(vp&0x0f)]); + snprintf(vbuf, sizeof(vbuf), + "0.%02lum", (vp>>4) * poweroften[(vp&0x0f)]); isc_region_consume(&sr, 4); latitude = uint32_fromregion(&sr); @@ -513,7 +519,8 @@ totext_loc(ARGS_TOTEXT) { altitude -= 10000000; } - sprintf(buf, "%d %d %d.%03d %s %d %d %d.%03d %s %s%ld.%02ldm %s %s %s", + snprintf(buf, sizeof(buf), + "%d %d %d.%03d %s %d %d %d.%03d %s %s%ld.%02ldm %s %s %s", d1, m1, s1, fs1, north ? "N" : "S", d2, m2, s2, fs2, east ? "E" : "W", below ? "-" : "", altitude/100, altitude % 100, diff --git a/usr.sbin/bind/lib/dns/rdata/generic/mx_15.c b/usr.sbin/bind/lib/dns/rdata/generic/mx_15.c index 0dcae8ca204..8e2806868aa 100644 --- a/usr.sbin/bind/lib/dns/rdata/generic/mx_15.c +++ b/usr.sbin/bind/lib/dns/rdata/generic/mx_15.c @@ -69,7 +69,7 @@ totext_mx(ARGS_TOTEXT) { dns_rdata_toregion(rdata, ®ion); num = uint16_fromregion(®ion); isc_region_consume(®ion, 2); - sprintf(buf, "%u", num); + snprintf(buf, sizeof(buf), "%u", num); RETERR(str_totext(buf, target)); RETERR(str_totext(" ", target)); diff --git a/usr.sbin/bind/lib/dns/rdata/generic/nxt_30.c b/usr.sbin/bind/lib/dns/rdata/generic/nxt_30.c index bb1f46a4514..95854befba5 100644 --- a/usr.sbin/bind/lib/dns/rdata/generic/nxt_30.c +++ b/usr.sbin/bind/lib/dns/rdata/generic/nxt_30.c @@ -117,7 +117,8 @@ totext_nxt(ARGS_TOTEXT) { target)); } else { char buf[sizeof "65535"]; - sprintf(buf, "%u", t); + snprintf(buf, sizeof(buf), + "%u", t); RETERR(str_totext(buf, target)); } diff --git a/usr.sbin/bind/lib/dns/rdata/generic/opt_41.c b/usr.sbin/bind/lib/dns/rdata/generic/opt_41.c index 0e272b07043..fb8bdacf146 100644 --- a/usr.sbin/bind/lib/dns/rdata/generic/opt_41.c +++ b/usr.sbin/bind/lib/dns/rdata/generic/opt_41.c @@ -67,7 +67,7 @@ totext_opt(ARGS_TOTEXT) { isc_region_consume(&r, 2); length = uint16_fromregion(&r); isc_region_consume(&r, 2); - sprintf(buf, "%u %u", option, length); + snprintf(buf, sizeof(buf), "%u %u", option, length); RETERR(str_totext(buf, target)); INSIST(r.length >= length); if (length > 0) { diff --git a/usr.sbin/bind/lib/dns/rdata/generic/rt_21.c b/usr.sbin/bind/lib/dns/rdata/generic/rt_21.c index 13af2c5b18f..a3c02d3b725 100644 --- a/usr.sbin/bind/lib/dns/rdata/generic/rt_21.c +++ b/usr.sbin/bind/lib/dns/rdata/generic/rt_21.c @@ -72,7 +72,7 @@ totext_rt(ARGS_TOTEXT) { dns_rdata_toregion(rdata, ®ion); num = uint16_fromregion(®ion); isc_region_consume(®ion, 2); - sprintf(buf, "%u", num); + snprintf(buf, sizeof(buf), "%u", num); RETERR(str_totext(buf, target)); RETERR(str_totext(" ", target)); dns_name_fromregion(&name, ®ion); diff --git a/usr.sbin/bind/lib/dns/rdata/generic/sig_24.c b/usr.sbin/bind/lib/dns/rdata/generic/sig_24.c index 7b4e57b2ed8..6c6ce9dad4d 100644 --- a/usr.sbin/bind/lib/dns/rdata/generic/sig_24.c +++ b/usr.sbin/bind/lib/dns/rdata/generic/sig_24.c @@ -155,7 +155,7 @@ totext_sig(ARGS_TOTEXT) { RETERR(dns_rdatatype_totext(covered, target)); } else { char buf[sizeof "65535"]; - sprintf(buf, "%u", covered); + snprintf(buf, sizeof(buf), "%u", covered); RETERR(str_totext(buf, target)); } RETERR(str_totext(" ", target)); @@ -163,7 +163,7 @@ totext_sig(ARGS_TOTEXT) { /* * Algorithm. */ - sprintf(buf, "%u", sr.base[0]); + snprintf(buf, sizeof(buf), "%u", sr.base[0]); isc_region_consume(&sr, 1); RETERR(str_totext(buf, target)); RETERR(str_totext(" ", target)); @@ -171,7 +171,7 @@ totext_sig(ARGS_TOTEXT) { /* * Labels. */ - sprintf(buf, "%u", sr.base[0]); + snprintf(buf, sizeof(buf), "%u", sr.base[0]); isc_region_consume(&sr, 1); RETERR(str_totext(buf, target)); RETERR(str_totext(" ", target)); @@ -181,7 +181,7 @@ totext_sig(ARGS_TOTEXT) { */ ttl = uint32_fromregion(&sr); isc_region_consume(&sr, 4); - sprintf(buf, "%lu", ttl); + snprintf(buf, sizeof(buf), "%lu", ttl); RETERR(str_totext(buf, target)); RETERR(str_totext(" ", target)); @@ -209,7 +209,7 @@ totext_sig(ARGS_TOTEXT) { */ foot = uint16_fromregion(&sr); isc_region_consume(&sr, 2); - sprintf(buf, "%lu", foot); + snprintf(buf, sizeof(buf), "%lu", foot); RETERR(str_totext(buf, target)); RETERR(str_totext(" ", target)); diff --git a/usr.sbin/bind/lib/dns/rdata/generic/soa_6.c b/usr.sbin/bind/lib/dns/rdata/generic/soa_6.c index ce66162907b..650ebecaacd 100644 --- a/usr.sbin/bind/lib/dns/rdata/generic/soa_6.c +++ b/usr.sbin/bind/lib/dns/rdata/generic/soa_6.c @@ -117,7 +117,7 @@ totext_soa(ARGS_TOTEXT) { unsigned int numlen; num = uint32_fromregion(&dregion); isc_region_consume(&dregion, 4); - numlen = sprintf(buf, "%lu", num); + numlen = snprintf(buf, sizeof(buf), "%lu", num); INSIST(numlen > 0 && numlen < sizeof "2147483647"); RETERR(str_totext(buf, target)); if (multiline && comment) { diff --git a/usr.sbin/bind/lib/dns/rdata/generic/tkey_249.c b/usr.sbin/bind/lib/dns/rdata/generic/tkey_249.c index 10adafe26d6..83efe2e9425 100644 --- a/usr.sbin/bind/lib/dns/rdata/generic/tkey_249.c +++ b/usr.sbin/bind/lib/dns/rdata/generic/tkey_249.c @@ -153,7 +153,7 @@ totext_tkey(ARGS_TOTEXT) { */ n = uint32_fromregion(&sr); isc_region_consume(&sr, 4); - sprintf(buf, "%lu ", n); + snprintf(buf, sizeof(buf), "%lu ", n); RETERR(str_totext(buf, target)); /* @@ -161,7 +161,7 @@ totext_tkey(ARGS_TOTEXT) { */ n = uint32_fromregion(&sr); isc_region_consume(&sr, 4); - sprintf(buf, "%lu ", n); + snprintf(buf, sizeof(buf), "%lu ", n); RETERR(str_totext(buf, target)); /* @@ -169,7 +169,7 @@ totext_tkey(ARGS_TOTEXT) { */ n = uint16_fromregion(&sr); isc_region_consume(&sr, 2); - sprintf(buf, "%lu ", n); + snprintf(buf, sizeof(buf), "%lu ", n); RETERR(str_totext(buf, target)); /* @@ -180,7 +180,7 @@ totext_tkey(ARGS_TOTEXT) { if (dns_tsigrcode_totext((dns_rcode_t)n, target) == ISC_R_SUCCESS) RETERR(str_totext(" ", target)); else { - sprintf(buf, "%lu ", n); + snprintf(buf, sizeof(buf), "%lu ", n); RETERR(str_totext(buf, target)); } @@ -189,7 +189,7 @@ totext_tkey(ARGS_TOTEXT) { */ n = uint16_fromregion(&sr); isc_region_consume(&sr, 2); - sprintf(buf, "%lu", n); + snprintf(buf, sizeof(buf), "%lu", n); RETERR(str_totext(buf, target)); /* @@ -214,7 +214,7 @@ totext_tkey(ARGS_TOTEXT) { */ n = uint16_fromregion(&sr); isc_region_consume(&sr, 2); - sprintf(buf, "%lu", n); + snprintf(buf, sizeof(buf), "%lu", n); RETERR(str_totext(buf, target)); /* diff --git a/usr.sbin/bind/lib/dns/rdata/in_1/a6_38.c b/usr.sbin/bind/lib/dns/rdata/in_1/a6_38.c index b73abd8c0c9..959cae624fc 100644 --- a/usr.sbin/bind/lib/dns/rdata/in_1/a6_38.c +++ b/usr.sbin/bind/lib/dns/rdata/in_1/a6_38.c @@ -107,7 +107,7 @@ totext_in_a6(ARGS_TOTEXT) { prefixlen = sr.base[0]; INSIST(prefixlen <= 128); isc_region_consume(&sr, 1); - sprintf(buf, "%u", prefixlen); + snprintf(buf, sizeof(buf), "%u", prefixlen); RETERR(str_totext(buf, target)); RETERR(str_totext(" ", target)); diff --git a/usr.sbin/bind/lib/dns/rdata/in_1/kx_36.c b/usr.sbin/bind/lib/dns/rdata/in_1/kx_36.c index 75004b6beb8..4280e7792cf 100644 --- a/usr.sbin/bind/lib/dns/rdata/in_1/kx_36.c +++ b/usr.sbin/bind/lib/dns/rdata/in_1/kx_36.c @@ -73,7 +73,7 @@ totext_in_kx(ARGS_TOTEXT) { dns_rdata_toregion(rdata, ®ion); num = uint16_fromregion(®ion); isc_region_consume(®ion, 2); - sprintf(buf, "%u", num); + snprintf(buf, sizeof(buf), "%u", num); RETERR(str_totext(buf, target)); RETERR(str_totext(" ", target)); diff --git a/usr.sbin/bind/lib/dns/rdata/in_1/naptr_35.c b/usr.sbin/bind/lib/dns/rdata/in_1/naptr_35.c index aa7f1f1432d..977faf28b40 100644 --- a/usr.sbin/bind/lib/dns/rdata/in_1/naptr_35.c +++ b/usr.sbin/bind/lib/dns/rdata/in_1/naptr_35.c @@ -113,7 +113,7 @@ totext_in_naptr(ARGS_TOTEXT) { */ num = uint16_fromregion(®ion); isc_region_consume(®ion, 2); - sprintf(buf, "%u", num); + snprintf(buf, sizeof(buf), "%u", num); RETERR(str_totext(buf, target)); RETERR(str_totext(" ", target)); @@ -122,7 +122,7 @@ totext_in_naptr(ARGS_TOTEXT) { */ num = uint16_fromregion(®ion); isc_region_consume(®ion, 2); - sprintf(buf, "%u", num); + snprintf(buf, sizeof(buf), "%u", num); RETERR(str_totext(buf, target)); RETERR(str_totext(" ", target)); diff --git a/usr.sbin/bind/lib/dns/rdata/in_1/nsap_22.c b/usr.sbin/bind/lib/dns/rdata/in_1/nsap_22.c index 98048bc0c28..6bf95b75f63 100644 --- a/usr.sbin/bind/lib/dns/rdata/in_1/nsap_22.c +++ b/usr.sbin/bind/lib/dns/rdata/in_1/nsap_22.c @@ -88,7 +88,7 @@ totext_in_nsap(ARGS_TOTEXT) { dns_rdata_toregion(rdata, ®ion); RETERR(str_totext("0x", target)); while (region.length != 0) { - sprintf(buf, "%02x", region.base[0]); + snprintf(buf, sizeof(buf), "%02x", region.base[0]); isc_region_consume(®ion, 1); RETERR(str_totext(buf, target)); } diff --git a/usr.sbin/bind/lib/dns/rdata/in_1/px_26.c b/usr.sbin/bind/lib/dns/rdata/in_1/px_26.c index d5265ae3ee3..d9f06b2fe6f 100644 --- a/usr.sbin/bind/lib/dns/rdata/in_1/px_26.c +++ b/usr.sbin/bind/lib/dns/rdata/in_1/px_26.c @@ -92,7 +92,7 @@ totext_in_px(ARGS_TOTEXT) { dns_rdata_toregion(rdata, ®ion); num = uint16_fromregion(®ion); isc_region_consume(®ion, 2); - sprintf(buf, "%u", num); + snprintf(buf, sizeof(buf), "%u", num); RETERR(str_totext(buf, target)); RETERR(str_totext(" ", target)); diff --git a/usr.sbin/bind/lib/dns/rdata/in_1/srv_33.c b/usr.sbin/bind/lib/dns/rdata/in_1/srv_33.c index 7f84d36e564..06f628838b5 100644 --- a/usr.sbin/bind/lib/dns/rdata/in_1/srv_33.c +++ b/usr.sbin/bind/lib/dns/rdata/in_1/srv_33.c @@ -100,7 +100,7 @@ totext_in_srv(ARGS_TOTEXT) { dns_rdata_toregion(rdata, ®ion); num = uint16_fromregion(®ion); isc_region_consume(®ion, 2); - sprintf(buf, "%u", num); + snprintf(buf, sizeof(buf), "%u", num); RETERR(str_totext(buf, target)); RETERR(str_totext(" ", target)); @@ -109,7 +109,7 @@ totext_in_srv(ARGS_TOTEXT) { */ num = uint16_fromregion(®ion); isc_region_consume(®ion, 2); - sprintf(buf, "%u", num); + snprintf(buf, sizeof(buf), "%u", num); RETERR(str_totext(buf, target)); RETERR(str_totext(" ", target)); @@ -118,7 +118,7 @@ totext_in_srv(ARGS_TOTEXT) { */ num = uint16_fromregion(®ion); isc_region_consume(®ion, 2); - sprintf(buf, "%u", num); + snprintf(buf, sizeof(buf), "%u", num); RETERR(str_totext(buf, target)); RETERR(str_totext(" ", target)); diff --git a/usr.sbin/bind/lib/dns/rdata/in_1/wks_11.c b/usr.sbin/bind/lib/dns/rdata/in_1/wks_11.c index 09615df1436..672ea2e82a7 100644 --- a/usr.sbin/bind/lib/dns/rdata/in_1/wks_11.c +++ b/usr.sbin/bind/lib/dns/rdata/in_1/wks_11.c @@ -103,8 +103,7 @@ fromtext_in_wks(ARGS_FROMTEXT) { * Lowercase the service string as some getservbyname() are * case sensitive and the database is usually in lowercase. */ - strncpy(service, token.value.as_pointer, sizeof(service)); - service[sizeof(service)-1] = '\0'; + strlcpy(service, token.value.as_pointer, sizeof(service)); for (i = strlen(service) - 1; i >= 0; i--) if (isupper(service[i]&0xff)) service[i] = tolower(service[i]); @@ -153,7 +152,7 @@ totext_in_wks(ARGS_TOTEXT) { isc_region_consume(&sr, 4); proto = uint8_fromregion(&sr); - sprintf(buf, "%u", proto); + snprintf(buf, sizeof(buf), "%u", proto); RETERR(str_totext(" ", target)); RETERR(str_totext(buf, target)); isc_region_consume(&sr, 1); @@ -162,7 +161,7 @@ totext_in_wks(ARGS_TOTEXT) { if (sr.base[i] != 0) for (j = 0 ; j < 8 ; j++) if ((sr.base[i] & (0x80 >> j)) != 0) { - sprintf(buf, "%u", i * 8 + j); + snprintf(buf, sizeof(buf), "%u", i * 8 + j); RETERR(str_totext(" ", target)); RETERR(str_totext(buf, target)); } diff --git a/usr.sbin/bind/lib/dns/ttl.c b/usr.sbin/bind/lib/dns/ttl.c index c771563aa51..63cb9e28880 100644 --- a/usr.sbin/bind/lib/dns/ttl.c +++ b/usr.sbin/bind/lib/dns/ttl.c @@ -155,8 +155,7 @@ 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'; + strlcpy(buf, source->base, sizeof(buf)); s = buf; do { diff --git a/usr.sbin/bind/lib/isc/log.c b/usr.sbin/bind/lib/isc/log.c index f2de1c42d67..6216cc40e2e 100644 --- a/usr.sbin/bind/lib/isc/log.c +++ b/usr.sbin/bind/lib/isc/log.c @@ -1578,7 +1578,7 @@ isc_log_doit(isc_log_t *lctx, isc_logcategory_t *category, if (new != NULL) { /* * Put the text immediately after - * the struct. The strcpy is safe. + * the struct. The strlcpy is safer. */ new->text = (char *)(new + 1); strlcpy(new->text, lctx->buffer, len); diff --git a/usr.sbin/bind/lib/isc/mem.c b/usr.sbin/bind/lib/isc/mem.c index efc6c1081a8..9cca07e2cd7 100644 --- a/usr.sbin/bind/lib/isc/mem.c +++ b/usr.sbin/bind/lib/isc/mem.c @@ -1250,7 +1250,7 @@ isc__mem_strdup(isc_mem_t *mctx, const char *s FLARG) { ns = isc__mem_allocate(mctx, len + 1 FLARG_PASS); if (ns != NULL) - strncpy(ns, s, len + 1); + strlcpy(ns, s, len + 1); return (ns); } @@ -1383,8 +1383,7 @@ isc_mempool_setname(isc_mempool_t *mpctx, 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/usr.sbin/bind/lib/isc/print.c b/usr.sbin/bind/lib/isc/print.c index ac2081d84b4..c15eb6c0fc9 100644 --- a/usr.sbin/bind/lib/isc/print.c +++ b/usr.sbin/bind/lib/isc/print.c @@ -20,7 +20,7 @@ #include <config.h> #include <ctype.h> -#include <stdio.h> /* for sprintf */ +#include <stdio.h> /* for snprintf */ #include <stdlib.h> #define ISC__PRINT_SOURCE /* Used to get the isc_print_* prototypes. */ @@ -234,8 +234,8 @@ isc_print_vsnprintf(char *str, size_t size, const char *format, va_list ap) { head = ""; tmpui = tmpi; } - sprintf(buf, "%" ISC_PRINT_QUADFORMAT "u", - tmpui); + snprintf(buf, sizeof(buf), + "%" ISC_PRINT_QUADFORMAT "u", tmpui); goto printint; case 'o': if (q) @@ -244,10 +244,10 @@ isc_print_vsnprintf(char *str, size_t size, const char *format, va_list ap) { tmpui = va_arg(ap, long int); else tmpui = va_arg(ap, int); - sprintf(buf, - alt ? "%#" ISC_PRINT_QUADFORMAT "o" + snprintf(buf, sizeof(buf), + alt ? "%#" ISC_PRINT_QUADFORMAT "o" : "%" ISC_PRINT_QUADFORMAT "o", - tmpui); + tmpui); goto printint; case 'u': if (q) @@ -256,8 +256,8 @@ isc_print_vsnprintf(char *str, size_t size, const char *format, va_list ap) { tmpui = va_arg(ap, unsigned long int); else tmpui = va_arg(ap, unsigned int); - sprintf(buf, "%" ISC_PRINT_QUADFORMAT "u", - tmpui); + snprintf(buf, sizeof(buf), + "%" ISC_PRINT_QUADFORMAT "u", tmpui); goto printint; case 'x': if (q) @@ -271,8 +271,8 @@ isc_print_vsnprintf(char *str, size_t size, const char *format, va_list ap) { if (precision > 2) precision -= 2; } - sprintf(buf, "%" ISC_PRINT_QUADFORMAT "x", - tmpui); + snprintf(buf, sizeof(buf), + "%" ISC_PRINT_QUADFORMAT "x", tmpui); goto printint; case 'X': if (q) @@ -286,8 +286,8 @@ isc_print_vsnprintf(char *str, size_t size, const char *format, va_list ap) { if (precision > 2) precision -= 2; } - sprintf(buf, "%" ISC_PRINT_QUADFORMAT "X", - tmpui); + snprintf(buf, sizeof(buf), + "%" ISC_PRINT_QUADFORMAT "X", tmpui); goto printint; printint: if (precision != 0 || width != 0) { @@ -413,7 +413,7 @@ isc_print_vsnprintf(char *str, size_t size, const char *format, va_list ap) { break; case 'p': v = va_arg(ap, void *); - sprintf(buf, "%p", v); + snprintf(buf, sizeof(buf), "%p", v); length = strlen(buf); if (precision > length) zeropad = precision - length; @@ -489,9 +489,10 @@ isc_print_vsnprintf(char *str, size_t size, const char *format, va_list ap) { */ if (precision > 512) precision = 512; - sprintf(fmt, "%%%s%s.%lu%s%c", alt ? "#" : "", - plus ? "+" : space ? " " : "", - precision, l ? "L" : "", *format); + snprintf(fmt, sizeof(fmt), + "%%%s%s.%lu%s%c", alt ? "#" : "", + plus ? "+" : space ? " " : "", + precision, l ? "L" : "", *format); switch (*format) { case 'e': case 'E': @@ -501,12 +502,12 @@ isc_print_vsnprintf(char *str, size_t size, const char *format, va_list ap) { #ifdef HAVE_LONG_DOUBLE if (l) { ldbl = va_arg(ap, long double); - sprintf(buf, fmt, ldbl); + snprintf(buf, sizeof(buf), fmt, ldbl); } else #endif { dbl = va_arg(ap, double); - sprintf(buf, fmt, dbl); + snprintf(buf, sizeof(buf), fmt, dbl); } length = strlen(buf); if (width > 0) { diff --git a/usr.sbin/bind/lib/isc/task.c b/usr.sbin/bind/lib/isc/task.c index cb9a2cb0925..30eeeda2d8f 100644 --- a/usr.sbin/bind/lib/isc/task.c +++ b/usr.sbin/bind/lib/isc/task.c @@ -697,7 +697,7 @@ isc_task_setname(isc_task_t *task, const char *name, void *tag) { #ifdef ISC_TASK_NAMES 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); #else diff --git a/usr.sbin/bind/lib/isc/unix/file.c b/usr.sbin/bind/lib/isc/unix/file.c index 95f5428ca43..f6e80e7fa7e 100644 --- a/usr.sbin/bind/lib/isc/unix/file.c +++ b/usr.sbin/bind/lib/isc/unix/file.c @@ -145,8 +145,7 @@ isc_file_template(const char *path, const char *templet, char *buf, if ((s - path + 1 + strlen(templet) + 1) > buflen) return (ISC_R_NOSPACE); - strncpy(buf, path, s - path + 1); - buf[s - path + 1] = '\0'; + strlcpy(buf, path, buflen); strlcat(buf, templet, buflen); } else { if ((strlen(templet) + 1) > buflen) diff --git a/usr.sbin/bind/lib/lwres/getaddrinfo.c b/usr.sbin/bind/lib/lwres/getaddrinfo.c index 8d1594c84c8..27189887d6a 100644 --- a/usr.sbin/bind/lib/lwres/getaddrinfo.c +++ b/usr.sbin/bind/lib/lwres/getaddrinfo.c @@ -248,8 +248,7 @@ lwres_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; @@ -603,7 +602,7 @@ get_local(const char *name, int socktype, struct addrinfo **res) { return (EAI_MEMORY); sun = SUN(ai->ai_addr); - strncpy(sun->sun_path, name, sizeof(sun->sun_path)); + strlcpy(sun->sun_path, name, sizeof(sun->sun_path)); ai->ai_socktype = socktype; /* diff --git a/usr.sbin/bind/lib/lwres/getrrset.c b/usr.sbin/bind/lib/lwres/getrrset.c index e26b0093287..085f435b583 100644 --- a/usr.sbin/bind/lib/lwres/getrrset.c +++ b/usr.sbin/bind/lib/lwres/getrrset.c @@ -131,8 +131,7 @@ lwres_getrrsetbyname(const char *hostname, unsigned int rdclass, result = ERRSET_NOMEMORY; goto fail; } - strncpy(rrset->rri_name, response->realname, response->realnamelen); - rrset->rri_name[response->realnamelen] = 0; + strlcpy(rrset->rri_name, response->realname, response->realnamelen + 1); if ((response->flags & LWRDATA_VALIDATED) != 0) rrset->rri_flags |= RRSET_VALIDATED; |