Commit a009d03a authored by Mark Andrews's avatar Mark Andrews
Browse files

4748. [cleanup] Sprintf to snprintf coversions. [RT #46132]

parent 7cb14b61
4748. [cleanup] Sprintf to snprintf coversions. [RT #46132]
4747. [func] Synthesis of responses from DNSSEC-verified records.
Stage 3 - synthesize NODATA responses. [RT #40138]
......
......@@ -2813,7 +2813,7 @@ writeset(const char *prefix, dns_rdatatype_t type) {
if (filename == NULL)
fatal("out of memory");
if (dsdir != NULL)
sprintf(filename, "%s/", dsdir);
snprintf(filename, filenamelen, "%s/", dsdir);
else
filename[0] = 0;
strlcat(filename, prefix, filenamelen);
......@@ -3490,12 +3490,13 @@ main(int argc, char *argv[]) {
origin = file;
if (output == NULL) {
size_t size;
free_output = ISC_TRUE;
output = isc_mem_allocate(mctx,
strlen(file) + strlen(".signed") + 1);
size = strlen(file) + strlen(".signed") + 1;
output = isc_mem_allocate(mctx, size);
if (output == NULL)
fatal("out of memory");
sprintf(output, "%s.signed", file);
snprintf(output, size, "%s.signed", file);
}
if (inputformatstr != NULL) {
......
......@@ -1790,8 +1790,8 @@ dns64_reverse(dns_view_t *view, isc_mem_t *mctx, isc_netaddr_t *na,
unsigned int prefixlen, const char *server,
const char *contact)
{
char *cp;
char reverse[48+sizeof("ip6.arpa.")];
char reverse[48+sizeof("ip6.arpa.")] = { 0 };
char buf[sizeof("x.x.")];
const char *dns64_dbtype[4] = { "_dns64", "dns64", ".", "." };
const char *sep = ": view ";
const char *viewname = view->name;
......@@ -1814,15 +1814,13 @@ dns64_reverse(dns_view_t *view, isc_mem_t *mctx, isc_netaddr_t *na,
/*
* Construct the reverse name of the zone.
*/
cp = reverse;
s6 = na->type.in6.s6_addr;
while (prefixlen > 0) {
prefixlen -= 8;
sprintf(cp, "%x.%x.", s6[prefixlen/8] & 0xf,
(s6[prefixlen/8] >> 4) & 0xf);
cp += 4;
snprintf(buf, sizeof(buf), "%x.%x.", s6[prefixlen/8] & 0xf,
(s6[prefixlen/8] >> 4) & 0xf);
strlcat(reverse, buf, sizeof(reverse));
}
strlcat(reverse, "ip6.arpa.", sizeof(reverse));
/*
......
......@@ -1270,8 +1270,8 @@ rdatasetstats_dump(dns_rdatastatstype_t type, isc_uint64_t val, void *arg) {
case isc_statsformat_json:
#ifdef HAVE_JSON
zoneobj = (json_object *) dumparg->arg;
sprintf(buf, "%s%s%s", stale ? "#" : "",
nxrrset ? "!" : "", typestr);
snprintf(buf, sizeof(buf), "%s%s%s",
stale ? "#" : "", nxrrset ? "!" : "", typestr);
obj = json_object_new_int64(val);
if (obj == NULL)
return;
......
......@@ -179,7 +179,7 @@ nsecify(char *filename) {
len = strlen(filename);
if (len + 4 + 1 > sizeof(newfilename))
fatal("filename too long");
sprintf(newfilename, "%s.new", filename);
snprintf(newfilename, sizeof(newfilename), "%s.new", filename);
result = dns_db_dump(db, NULL, newfilename);
check_result(result, "dns_db_dump");
dns_db_detach(&db);
......
......@@ -109,7 +109,7 @@ main(int argc, char *argv[]) {
RUNTIME_CHECK(isc_rwlock_init(&lock, 5, 10) == ISC_R_SUCCESS);
for (i = 0; i < nworkers; i++) {
sprintf(name, "%02u", i);
snprintf(name, sizeof(name), "%02u", i);
dupname = strdup(name);
RUNTIME_CHECK(dupname != NULL);
if (i != 0 && i % 3 == 0)
......
......@@ -102,8 +102,8 @@ my_recv(isc_task_t *task, isc_event_t *event) {
*/
if (strcmp(event->ev_arg, "so2") != 0) {
region = dev->region;
sprintf(buf, "\r\nReceived: %.*s\r\n\r\n",
(int)dev->n, (char *)region.base);
snprintf(buf, sizeof(buf), "\r\nReceived: %.*s\r\n\r\n",
(int)dev->n, (char *)region.base);
region.base = isc_mem_get(mctx, strlen(buf) + 1);
if (region.base != NULL) {
region.length = strlen(buf) + 1;
......
......@@ -1878,8 +1878,9 @@ buildfilename(dns_name_t *name, dns_keytag_t id,
len = 1 + 3 + 1 + 5 + strlen(suffix) + 1;
if (isc_buffer_availablelength(out) < len)
return (ISC_R_NOSPACE);
sprintf((char *) isc_buffer_used(out), "+%03d+%05d%s", alg, id,
suffix);
snprintf((char *) isc_buffer_used(out),
(int)isc_buffer_availablelength(out),
"+%03d+%05d%s", alg, id, suffix);
isc_buffer_add(out, len);
return (ISC_R_SUCCESS);
......
......@@ -695,10 +695,14 @@ dst_gssapi_acceptctx(gss_cred_id_t cred,
*/
const char *old = getenv("KRB5_KTNAME");
if (old == NULL || strcmp(old, gssapi_keytab) != 0) {
char *kt = malloc(strlen(gssapi_keytab) + 13);
size_t size;
char *kt;
size = strlen(gssapi_keytab) + 13;
kt = malloc(size);
if (kt == NULL)
return (ISC_R_NOMEMORY);
sprintf(kt, "KRB5_KTNAME=%s", gssapi_keytab);
snprintf(kt, size, "KRB5_KTNAME=%s", gssapi_keytab);
if (putenv(kt) != 0)
return (ISC_R_NOMEMORY);
}
......
......@@ -351,7 +351,7 @@ dns_private_totext(dns_rdata_t *private, isc_buffer_t *buf) {
isc_buffer_putstr(buf, "Signing with ");
dns_secalg_format(alg, algbuf, sizeof(algbuf));
sprintf(keybuf, "key %d/%s", keyid, algbuf);
snprintf(keybuf, sizeof(keybuf), "key %d/%s", keyid, algbuf);
isc_buffer_putstr(buf, keybuf);
} else
return (ISC_R_NOTFOUND);
......
......@@ -522,7 +522,7 @@ typemap_totext(isc_region_t *sr, dns_rdata_textctx_t *tctx,
RETERR(dns_rdatatype_totext(t, target));
} else {
char buf[sizeof("TYPE65535")];
sprintf(buf, "TYPE%u", t);
snprintf(buf, sizeof(buf), "TYPE%u", t);
RETERR(str_totext(buf, target));
}
}
......
......@@ -174,7 +174,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));
/*
......@@ -182,7 +182,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));
/*
......@@ -210,7 +210,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));
/*
......@@ -225,7 +225,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));
/*
......
......@@ -80,7 +80,7 @@ totext_ch_a(ARGS_TOTEXT) {
sub = name_prefix(&name, tctx->origin, &prefix);
RETERR(dns_name_totext(&prefix, sub, target));
sprintf(buf, "%o", addr); /* note octal */
snprintf(buf, sizeof(buf), "%o", addr); /* note octal */
RETERR(str_totext(" ", target));
return (str_totext(buf, target));
}
......
......@@ -77,7 +77,7 @@ totext_afsdb(ARGS_TOTEXT) {
dns_rdata_toregion(rdata, &region);
num = uint16_fromregion(&region);
isc_region_consume(&region, 2);
sprintf(buf, "%u ", num);
snprintf(buf, sizeof(buf), "%u ", num);
RETERR(str_totext(buf, target));
dns_name_fromregion(&name, &region);
sub = name_prefix(&name, tctx->origin, &prefix);
......
......@@ -85,7 +85,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));
/*
......
......@@ -56,14 +56,14 @@ totext_csync(ARGS_TOTEXT) {
num = uint32_fromregion(&sr);
isc_region_consume(&sr, 4);
sprintf(buf, "%lu", num);
snprintf(buf, sizeof(buf), "%lu", num);
RETERR(str_totext(buf, target));
RETERR(str_totext(" ", target));
num = uint16_fromregion(&sr);
isc_region_consume(&sr, 2);
sprintf(buf, "%lu", num);
snprintf(buf, sizeof(buf), "%lu", num);
RETERR(str_totext(buf, target));
return (typemap_totext(&sr, NULL, target));
......
......@@ -111,7 +111,7 @@ generic_totext_ds(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));
/*
......@@ -119,7 +119,7 @@ generic_totext_ds(ARGS_TOTEXT) {
*/
n = uint8_fromregion(&sr);
isc_region_consume(&sr, 1);
sprintf(buf, "%u ", n);
snprintf(buf, sizeof(buf), "%u ", n);
RETERR(str_totext(buf, target));
/*
......@@ -127,7 +127,7 @@ generic_totext_ds(ARGS_TOTEXT) {
*/
n = uint8_fromregion(&sr);
isc_region_consume(&sr, 1);
sprintf(buf, "%u", n);
snprintf(buf, sizeof(buf), "%u", n);
RETERR(str_totext(buf, target));
/*
......
......@@ -140,7 +140,7 @@ totext_hip(ARGS_TOTEXT) {
/*
* Algorithm
*/
sprintf(buf, "%u ", algorithm);
snprintf(buf, sizeof(buf), "%u ", algorithm);
RETERR(str_totext(buf, target));
/*
......
......@@ -134,7 +134,7 @@ totext_ipseckey(ARGS_TOTEXT) {
dns_rdata_toregion(rdata, &region);
num = uint8_fromregion(&region);
isc_region_consume(&region, 1);
sprintf(buf, "%u ", num);
snprintf(buf, sizeof(buf), "%u ", num);
RETERR(str_totext(buf, target));
/*
......@@ -142,7 +142,7 @@ totext_ipseckey(ARGS_TOTEXT) {
*/
gateway = uint8_fromregion(&region);
isc_region_consume(&region, 1);
sprintf(buf, "%u ", gateway);
snprintf(buf, sizeof(buf), "%u ", gateway);
RETERR(str_totext(buf, target));
/*
......@@ -150,7 +150,7 @@ totext_ipseckey(ARGS_TOTEXT) {
*/
num = uint8_fromregion(&region);
isc_region_consume(&region, 1);
sprintf(buf, "%u ", num);
snprintf(buf, sizeof(buf), "%u ", num);
RETERR(str_totext(buf, target));
/*
......
......@@ -85,7 +85,7 @@ generic_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));
if ((flags & DNS_KEYFLAG_KSK) != 0) {
......@@ -98,14 +98,14 @@ generic_totext_key(ARGS_TOTEXT) {
/* 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));
......@@ -161,7 +161,8 @@ generic_totext_key(ARGS_TOTEXT) {
RETERR(str_totext(algbuf, target));
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);
......
......@@ -122,7 +122,7 @@ totext_keydata(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));
if ((flags & DNS_KEYFLAG_KSK) != 0) {
......@@ -134,14 +134,14 @@ totext_keydata(ARGS_TOTEXT) {
keyinfo = "ZSK";
/* 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));
......@@ -184,7 +184,8 @@ totext_keydata(ARGS_TOTEXT) {
dns_rdata_toregion(rdata, &tmpr);
/* Skip over refresh, addhd, and removehd */
isc_region_consume(&tmpr, 12);
sprintf(buf, "%u", dst_region_computeid(&tmpr, algorithm));
snprintf(buf, sizeof(buf), "%u",
dst_region_computeid(&tmpr, algorithm));
RETERR(str_totext(buf, target));
if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0) {
......
......@@ -62,7 +62,7 @@ totext_l32(ARGS_TOTEXT) {
dns_rdata_toregion(rdata, &region);
num = uint16_fromregion(&region);
isc_region_consume(&region, 2);
sprintf(buf, "%u", num);
snprintf(buf, sizeof(buf), "%u", num);
RETERR(str_totext(buf, target));
RETERR(str_totext(" ", target));
......
......@@ -56,16 +56,16 @@ totext_l64(ARGS_TOTEXT) {
dns_rdata_toregion(rdata, &region);
num = uint16_fromregion(&region);
isc_region_consume(&region, 2);
sprintf(buf, "%u", num);
snprintf(buf, sizeof(buf), "%u", num);
RETERR(str_totext(buf, target));
RETERR(str_totext(" ", target));
sprintf(buf, "%x:%x:%x:%x",
region.base[0]<<8 | region.base[1],
region.base[2]<<8 | region.base[3],
region.base[4]<<8 | region.base[5],
region.base[6]<<8 | region.base[7]);
snprintf(buf, sizeof(buf), "%x:%x:%x:%x",
region.base[0]<<8 | region.base[1],
region.base[2]<<8 | region.base[3],
region.base[4]<<8 | region.base[5],
region.base[6]<<8 | region.base[7]);
return (str_totext(buf, target));
}
......
......@@ -478,22 +478,31 @@ totext_loc(ARGS_TOTEXT) {
size = sr.base[1];
INSIST((size&0x0f) < 10 && (size>>4) < 10);
if ((size&0x0f)> 1)
sprintf(sbuf, "%lum", (size>>4) * poweroften[(size&0x0f)-2]);
else
sprintf(sbuf, "0.%02lum", (size>>4) * poweroften[(size&0x0f)]);
if ((size&0x0f)> 1) {
snprintf(sbuf, sizeof(sbuf),
"%lum", (size>>4) * poweroften[(size&0x0f)-2]);
} else {
snprintf(sbuf, sizeof(sbuf),
"0.%02lum", (size>>4) * poweroften[(size&0x0f)]);
}
hp = sr.base[2];
INSIST((hp&0x0f) < 10 && (hp>>4) < 10);
if ((hp&0x0f)> 1)
sprintf(hbuf, "%lum", (hp>>4) * poweroften[(hp&0x0f)-2]);
else
sprintf(hbuf, "0.%02lum", (hp>>4) * poweroften[(hp&0x0f)]);
if ((hp&0x0f)> 1) {
snprintf(hbuf, sizeof(hbuf),
"%lum", (hp>>4) * poweroften[(hp&0x0f)-2]);
} else {
snprintf(hbuf, sizeof(hbuf),
"0.%02lum", (hp>>4) * poweroften[(hp&0x0f)]);
}
vp = sr.base[3];
INSIST((vp&0x0f) < 10 && (vp>>4) < 10);
if ((vp&0x0f)> 1)
sprintf(vbuf, "%lum", (vp>>4) * poweroften[(vp&0x0f)-2]);
else
sprintf(vbuf, "0.%02lum", (vp>>4) * poweroften[(vp&0x0f)]);
if ((vp&0x0f)> 1) {
snprintf(vbuf, sizeof(vbuf),
"%lum", (vp>>4) * poweroften[(vp&0x0f)-2]);
} else {
snprintf(vbuf, sizeof(vbuf),
"0.%02lum", (vp>>4) * poweroften[(vp&0x0f)]);
}
isc_region_consume(&sr, 4);
latitude = uint32_fromregion(&sr);
......@@ -542,11 +551,12 @@ totext_loc(ARGS_TOTEXT) {
altitude -= 10000000;
}
sprintf(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,
sbuf, hbuf, vbuf);
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,
sbuf, hbuf, vbuf);
return (str_totext(buf, target));
}
......
......@@ -61,7 +61,7 @@ totext_lp(ARGS_TOTEXT) {
dns_rdata_toregion(rdata, &region);
num = uint16_fromregion(&region);
isc_region_consume(&region, 2);
sprintf(buf, "%u", num);
snprintf(buf, sizeof(buf), "%u", num);
RETERR(str_totext(buf, target));
RETERR(str_totext(" ", target));
......
......@@ -101,7 +101,7 @@ totext_mx(ARGS_TOTEXT) {
dns_rdata_toregion(rdata, &region);
num = uint16_fromregion(&region);
isc_region_consume(&region, 2);
sprintf(buf, "%u", num);
snprintf(buf, sizeof(buf), "%u", num);
RETERR(str_totext(buf, target));
RETERR(str_totext(" ", target));
......
......@@ -200,7 +200,7 @@ totext_naptr(ARGS_TOTEXT) {
*/
num = uint16_fromregion(&region);
isc_region_consume(&region, 2);
sprintf(buf, "%u", num);
snprintf(buf, sizeof(buf), "%u", num);
RETERR(str_totext(buf, target));
RETERR(str_totext(" ", target));
......@@ -209,7 +209,7 @@ totext_naptr(ARGS_TOTEXT) {
*/
num = uint16_fromregion(&region);
isc_region_consume(&region, 2);
sprintf(buf, "%u", num);
snprintf(buf, sizeof(buf), "%u", num);
RETERR(str_totext(buf, target));
RETERR(str_totext(" ", target));
......
......@@ -56,16 +56,16 @@ totext_nid(ARGS_TOTEXT) {
dns_rdata_toregion(rdata, &region);
num = uint16_fromregion(&region);
isc_region_consume(&region, 2);
sprintf(buf, "%u", num);
snprintf(buf, sizeof(buf), "%u", num);
RETERR(str_totext(buf, target));
RETERR(str_totext(" ", target));
sprintf(buf, "%x:%x:%x:%x",
region.base[0]<<8 | region.base[1],
region.base[2]<<8 | region.base[3],
region.base[4]<<8 | region.base[5],
region.base[6]<<8 | region.base[7]);
snprintf(buf, sizeof(buf), "%x:%x:%x:%x",
region.base[0]<<8 | region.base[1],
region.base[2]<<8 | region.base[3],
region.base[4]<<8 | region.base[5],
region.base[6]<<8 | region.base[7]);
return (str_totext(buf, target));
}
......
......@@ -115,19 +115,19 @@ totext_nsec3(ARGS_TOTEXT) {
/* Hash */
hash = uint8_fromregion(&sr);
isc_region_consume(&sr, 1);
sprintf(buf, "%u ", hash);
snprintf(buf, sizeof(buf), "%u ", hash);
RETERR(str_totext(buf, target));
/* Flags */
flags = uint8_fromregion(&sr);
isc_region_consume(&sr, 1);
sprintf(buf, "%u ", flags);
snprintf(buf, sizeof(buf), "%u ", flags);
RETERR(str_totext(buf, target));
/* Iterations */
iterations = uint16_fromregion(&sr);
isc_region_consume(&sr, 2);
sprintf(buf, "%u ", iterations);
snprintf(buf, sizeof(buf), "%u ", iterations);
RETERR(str_totext(buf, target));
/* Salt */
......
......@@ -109,13 +109,13 @@ totext_nsec3param(ARGS_TOTEXT) {
iterations = uint16_fromregion(&sr);
isc_region_consume(&sr, 2);
sprintf(buf, "%u ", hash);
snprintf(buf, sizeof(buf), "%u ", hash);
RETERR(str_totext(buf, target));
sprintf(buf, "%u ", flags);
snprintf(buf, sizeof(buf), "%u ", flags);
RETERR(str_totext(buf, target));
sprintf(buf, "%u ", iterations);
snprintf(buf, sizeof(buf), "%u ", iterations);
RETERR(str_totext(buf, target));
j = uint8_fromregion(&sr);
......