summaryrefslogtreecommitdiffstats
path: root/src/tests
diff options
context:
space:
mode:
authorGreg Hudson <ghudson@mit.edu>2008-12-01 17:09:59 +0000
committerGreg Hudson <ghudson@mit.edu>2008-12-01 17:09:59 +0000
commit0692bffad6dbd696b2817f59ee7dd2e7e38ceb4b (patch)
tree3ab0a7b22c93fd929b1f532e98618d62f1a848d9 /src/tests
parent689e929e5e0240684408bb0bc53d536f5696cdea (diff)
downloadkrb5-0692bffad6dbd696b2817f59ee7dd2e7e38ceb4b.tar.gz
krb5-0692bffad6dbd696b2817f59ee7dd2e7e38ceb4b.tar.xz
krb5-0692bffad6dbd696b2817f59ee7dd2e7e38ceb4b.zip
Convert many uses of sprintf to snprintf or asprintf
ticket: 6200 status: open git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@21258 dc483132-0cff-0310-8789-dd5450dbe970
Diffstat (limited to 'src/tests')
-rw-r--r--src/tests/create/kdb5_mkdums.c16
-rw-r--r--src/tests/gss-threads/gss-client.c5
-rw-r--r--src/tests/hammer/kdc5_hammer.c16
-rw-r--r--src/tests/resolve/addrinfo-test.c6
-rw-r--r--src/tests/shlib/t_loader.c2
-rw-r--r--src/tests/threads/t_rcache.c6
-rw-r--r--src/tests/verify/kdb5_verify.c16
7 files changed, 35 insertions, 32 deletions
diff --git a/src/tests/create/kdb5_mkdums.c b/src/tests/create/kdb5_mkdums.c
index 3abef65db..12f4587b8 100644
--- a/src/tests/create/kdb5_mkdums.c
+++ b/src/tests/create/kdb5_mkdums.c
@@ -96,6 +96,7 @@ main(argc, argv)
int num_to_create;
char principal_string[BUFSIZ];
char *suffix = 0;
+ size_t suffix_size;
int depth;
krb5_init_context(&test_context);
@@ -121,6 +122,8 @@ main(argc, argv)
strncpy(principal_string, optarg, sizeof(principal_string) - 1);
principal_string[sizeof(principal_string) - 1] = '\0';
suffix = principal_string + strlen(principal_string);
+ suffix_size = sizeof(principal_string) -
+ (suffix - principal_string);
break;
case 'n': /* how many to create */
num_to_create = atoi(optarg);
@@ -175,14 +178,15 @@ main(argc, argv)
/* build the new principal name */
/* we can't pick random names because we need to generate all the names
again given a prefix and count to test the db lib and kdb */
- (void) sprintf(suffix, "%d", n);
- (void) sprintf(tmp, "%s-DEPTH-1", principal_string);
+ (void) snprintf(suffix, suffix_size, "%d", n);
+ (void) snprintf(tmp, sizeof(tmp), "%s-DEPTH-1", principal_string);
tmp[sizeof(tmp) - 1] = '\0';
str_newprinc = tmp;
add_princ(test_context, str_newprinc);
for (i = 2; i <= depth; i++) {
- (void) sprintf(tmp2, "/%s-DEPTH-%d", principal_string, i);
+ (void) snprintf(tmp2, sizeof(tmp2), "/%s-DEPTH-%d",
+ principal_string, i);
tmp2[sizeof(tmp2) - 1] = '\0';
strncat(tmp, tmp2, sizeof(tmp) - 1 - strlen(tmp));
str_newprinc = tmp;
@@ -215,7 +219,7 @@ add_princ(context, str_newprinc)
char princ_name[4096];
memset((char *)&newentry, 0, sizeof(newentry));
- sprintf(princ_name, "%s@%s", str_newprinc, cur_realm);
+ snprintf(princ_name, sizeof(princ_name), "%s@%s", str_newprinc, cur_realm);
if ((retval = krb5_parse_name(context, princ_name, &newprinc))) {
com_err(progname, retval, "while parsing '%s'", princ_name);
return;
@@ -375,12 +379,10 @@ char *dbname;
}
/* Pathname is passed to db2 via 'args' parameter. */
args[1] = NULL;
- args[0] = malloc(sizeof("dbname=") + strlen(dbname));
- if (args[0] == NULL) {
+ if (asprintf(&args[0], "dbname=%s", dbname) < 0) {
com_err(pname, errno, "while setting up db parameters");
return 1;
}
- sprintf(args[0], "dbname=%s", dbname);
if ((retval = krb5_db_open(test_context, args, KRB5_KDB_OPEN_RO))) {
com_err(pname, retval, "while initializing database");
diff --git a/src/tests/gss-threads/gss-client.c b/src/tests/gss-threads/gss-client.c
index 25c9d63ee..08bac92ca 100644
--- a/src/tests/gss-threads/gss-client.c
+++ b/src/tests/gss-threads/gss-client.c
@@ -66,6 +66,7 @@
#include "gss-misc.h"
#include "port-sockets.h"
#include "fake-addrinfo.h"
+#include "k5-platform.h"
static int verbose = 1;
@@ -606,12 +607,10 @@ static void parse_oid(char *mechanism, gss_OID *oid)
OM_uint32 maj_stat, min_stat;
if (isdigit((int) mechanism[0])) {
- mechstr = malloc(strlen(mechanism)+5);
- if (!mechstr) {
+ if (asprintf(&mechstr, "{ %s }", mechanism) < 0) {
fprintf(stderr, "Couldn't allocate mechanism scratch!\n");
return;
}
- sprintf(mechstr, "{ %s }", mechanism);
for (cp = mechstr; *cp; cp++)
if (*cp == '.')
*cp = ' ';
diff --git a/src/tests/hammer/kdc5_hammer.c b/src/tests/hammer/kdc5_hammer.c
index 5fd8d1c51..b1f5c179d 100644
--- a/src/tests/hammer/kdc5_hammer.c
+++ b/src/tests/hammer/kdc5_hammer.c
@@ -217,12 +217,12 @@ main(argc, argv)
again given a prefix and count to test the db lib and kdb */
ctmp[0] = '\0';
for (i = 1; i <= depth; i++) {
- (void) sprintf(ctmp2, "%s%s%d-DEPTH-%d", (i != 1) ? "/" : "",
- prefix, n, i);
+ (void) snprintf(ctmp2, sizeof(ctmp2), "%s%s%d-DEPTH-%d",
+ (i != 1) ? "/" : "", prefix, n, i);
ctmp2[sizeof(ctmp2) - 1] = '\0';
strncat(ctmp, ctmp2, sizeof(ctmp) - 1 - strlen(ctmp));
ctmp[sizeof(ctmp) - 1] = '\0';
- sprintf(client, "%s@%s", ctmp, cur_realm);
+ snprintf(client, sizeof(client), "%s@%s", ctmp, cur_realm);
if (get_tgt (test_context, client, &client_princ, ccache)) {
errors++;
@@ -233,12 +233,12 @@ main(argc, argv)
stmp[0] = '\0';
for (j = 1; j <= depth; j++) {
- (void) sprintf(stmp2, "%s%s%d-DEPTH-%d", (j != 1) ? "/" : "",
- prefix, n, j);
+ (void) snprintf(stmp2, sizeof(stmp2), "%s%s%d-DEPTH-%d",
+ (j != 1) ? "/" : "", prefix, n, j);
stmp2[sizeof (stmp2) - 1] = '\0';
strncat(stmp, stmp2, sizeof(stmp) - 1 - strlen(stmp));
stmp[sizeof(stmp) - 1] = '\0';
- sprintf(server, "%s@%s", stmp, cur_realm);
+ snprintf(server, sizeof(server), "%s@%s", stmp, cur_realm);
if (verify_cs_pair(test_context, client, client_princ,
stmp, cur_realm, n, i, j, ccache))
errors++;
@@ -343,9 +343,7 @@ int verify_cs_pair(context, p_client_str, p_client, service, hostname,
memset((char *)&creds, 0, sizeof(creds));
/* Do client side */
- sname = (char *) malloc(strlen(service)+strlen(hostname)+2);
- if (sname) {
- sprintf(sname, "%s@%s", service, hostname);
+ if (asprintf(&sname, "%s@%s", service, hostname) >= 0) {
retval = krb5_parse_name(context, sname, &creds.server);
free(sname);
}
diff --git a/src/tests/resolve/addrinfo-test.c b/src/tests/resolve/addrinfo-test.c
index 42e4dd62a..dff8df1dd 100644
--- a/src/tests/resolve/addrinfo-test.c
+++ b/src/tests/resolve/addrinfo-test.c
@@ -71,7 +71,7 @@ static const char *protoname (int p) {
X(COMP);
#endif
- sprintf(buf, " %-2d", p);
+ snprintf(buf, sizeof(buf), " %-2d", p);
return buf;
}
@@ -84,7 +84,7 @@ static const char *socktypename (int t) {
case SOCK_RDM: return "RDM";
case SOCK_SEQPACKET: return "SEQPACKET";
}
- sprintf(buf, " %-2d", t);
+ snprintf(buf, sizeof(buf), " %-2d", t);
return buf;
}
@@ -125,7 +125,7 @@ static const char *familyname (int f) {
static char buf[30];
switch (f) {
default:
- sprintf(buf, "AF %d", f);
+ snprintf(buf, sizeof(buf), "AF %d", f);
return buf;
case AF_INET: return "AF_INET";
#ifdef AF_INET6
diff --git a/src/tests/shlib/t_loader.c b/src/tests/shlib/t_loader.c
index cdc255055..6b2b1b7a5 100644
--- a/src/tests/shlib/t_loader.c
+++ b/src/tests/shlib/t_loader.c
@@ -109,7 +109,7 @@ static void do_close_1(void *libhandle, int line)
{
if (verbose) {
char pbuf[3*sizeof(libhandle)+4];
- sprintf(pbuf, "%p", libhandle);
+ snprintf(pbuf, sizeof(pbuf), "%p", libhandle);
printf("from line %d: do_close(%s)...%*s", line, pbuf,
HORIZ-1-strlen(pbuf), "");
}
diff --git a/src/tests/threads/t_rcache.c b/src/tests/threads/t_rcache.c
index 5d817defb..06f4d3b19 100644
--- a/src/tests/threads/t_rcache.c
+++ b/src/tests/threads/t_rcache.c
@@ -58,12 +58,14 @@ static void try_one (struct tinfo *t)
char buf[100], buf2[100];
krb5_rcache my_rcache;
- sprintf(buf, "host/all-in-one.mit.edu/%p@ATHENA.MIT.EDU", buf);
+ snprintf(buf, sizeof(buf), "host/all-in-one.mit.edu/%p@ATHENA.MIT.EDU",
+ buf);
r.server = buf;
r.client = (t->my_cusec & 7) + "abcdefgh@ATHENA.MIT.EDU";
if (t->now != t->my_ctime) {
if (t->my_ctime != 0) {
- sprintf(buf2, "%3d: %ld %5d\n", t->idx, t->my_ctime, t->my_cusec);
+ snprintf(buf2, sizeof(buf2), "%3d: %ld %5d\n", t->idx,
+ t->my_ctime, t->my_cusec);
printf("%s", buf2);
}
t->my_ctime = t->now;
diff --git a/src/tests/verify/kdb5_verify.c b/src/tests/verify/kdb5_verify.c
index b6d7b2332..92a375b55 100644
--- a/src/tests/verify/kdb5_verify.c
+++ b/src/tests/verify/kdb5_verify.c
@@ -97,6 +97,7 @@ main(argc, argv)
int num_to_check;
char principal_string[BUFSIZ];
char *suffix = 0;
+ size_t suffix_size;
int depth, errors;
krb5_init_context(&context);
@@ -122,6 +123,8 @@ main(argc, argv)
strncpy(principal_string, optarg, sizeof(principal_string) - 1);
principal_string[sizeof(principal_string) - 1] = '\0';
suffix = principal_string + strlen(principal_string);
+ suffix_size = sizeof(principal_string) -
+ (suffix - principal_string);
break;
case 'n': /* how many to check */
num_to_check = atoi(optarg);
@@ -182,13 +185,14 @@ main(argc, argv)
/* build the new principal name */
/* we can't pick random names because we need to generate all the names
again given a prefix and count to test the db lib and kdb */
- (void) sprintf(suffix, "%d", n);
- (void) sprintf(tmp, "%s-DEPTH-1", principal_string);
+ (void) snprintf(suffix, suffix_size, "%d", n);
+ (void) snprintf(tmp, sizeof(tmp), "%s-DEPTH-1", principal_string);
str_princ = tmp;
if (check_princ(context, str_princ)) errors++;
for (i = 2; i <= depth; i++) {
- (void) sprintf(tmp2, "/%s-DEPTH-%d", principal_string, i);
+ (void) snprintf(tmp2, sizeof(tmp2), "/%s-DEPTH-%d",
+ principal_string, i);
tmp2[sizeof(tmp2) - 1] = '\0';
strncat(tmp, tmp2, sizeof(tmp) - 1 - strlen(tmp));
str_princ = tmp;
@@ -234,7 +238,7 @@ check_princ(context, str_princ)
/* char *str_mod_name; */
char princ_name[4096];
- sprintf(princ_name, "%s@%s", str_princ, cur_realm);
+ snprintf(princ_name, sizeof(princ_name), "%s@%s", str_princ, cur_realm);
fprintf(stderr, "\t%s ...\n", princ_name);
@@ -405,12 +409,10 @@ set_dbname_help(context, pname, dbname)
}
/* Pathname is passed to db2 via 'args' parameter. */
args[1] = NULL;
- args[0] = malloc(sizeof("dbname=") + strlen(dbname));
- if (args[0] == NULL) {
+ if (asprintf(&args[0], "dbname=%s", dbname) < 0) {
com_err(pname, errno, "while setting up db parameters");
return 1;
}
- sprintf(args[0], "dbname=%s", dbname);
if ((retval = krb5_db_open(context, args, KRB5_KDB_OPEN_RO))) {
com_err(pname, retval, "while initializing database");