Replace strcpy/strcat/sprintf uses in a couple of sample code files
authorGreg Hudson <ghudson@mit.edu>
Wed, 5 Nov 2008 16:09:22 +0000 (16:09 +0000)
committerGreg Hudson <ghudson@mit.edu>
Wed, 5 Nov 2008 16:09:22 +0000 (16:09 +0000)
with strncpy/strncat.  Since this is sample code, we can't rely on
build system support for asprintf/strlcpy/strlcat.

ticket: 6200
status: open

git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@21000 dc483132-0cff-0310-8789-dd5450dbe970

src/appl/sample/sclient/sclient.c
src/plugins/authdata/greet/greet_auth.c

index 6ad305a880c936ea57e67d288b4062032072a03a..bd9c4e889788bcde0c2059ae172e4f3be88cc47e 100644 (file)
@@ -159,11 +159,16 @@ main(int argc, char *argv[])
        if (getnameinfo(ap->ai_addr, ap->ai_addrlen, abuf, sizeof(abuf),
                        pbuf, sizeof(pbuf), NI_NUMERICHOST | NI_NUMERICSERV)) {
            memset(abuf, 0, sizeof(abuf));
+           memset(pbuf, 0, sizeof(pbuf));
            strncpy(abuf, "[error, cannot print address?]",
                    sizeof(abuf)-1);
-           strcpy(pbuf, "[?]");
+           strncpy(pbuf, "[?]", sizeof(pbuf)-1);
        }
-       sprintf(mbuf, "error contacting %s port %s", abuf, pbuf);
+       memset(mbuf, 0, sizeof(mbuf));
+       strncpy(mbuf, "error contacting ", sizeof(mbuf)-1);
+       strncat(mbuf, abuf, sizeof(mbuf) - strlen(mbuf) - 1);
+       strncat(mbuf, " port ", sizeof(mbuf) - strlen(mbuf) - 1);
+       strncat(mbuf, pbuf, sizeof(mbuf) - strlen(mbuf) - 1);
        sock = socket(ap->ai_family, SOCK_STREAM, 0);
        if (sock < 0) {
            fprintf(stderr, "%s: socket: %s\n", mbuf, strerror(errno));
index a9d359eaa66dbc360ea8fd8bba7383143eed267a..f7b6dd733c43c28f991af84701cd4850a2e7aa43 100644 (file)
@@ -65,7 +65,7 @@ greet_authdata(krb5_context ctx, krb5_db_entry *client,
        free(a);
        return ENOMEM;
     }
-    strcpy(p, "hello there");
+    strncpy(p, "hello there", GREET_SIZE-1);
     a->magic = KV5M_AUTHDATA;
     a->ad_type = -42;
     a->length = GREET_SIZE;