Revert "lib: Fix printing a short into portstr"

This reverts commit ccb63bb0aa, which was
unnecessary and broke tests/kdc/check-kadmin (and other things).

host->port happens to be an unsigned short, so that promotion to an integer in
the snprintf() call is safe in that the promoted value will still be
non-negative, and no larger than an unsigned short's maximum value.  We're
still assuming that 7 bytes is sufficient to hold the text representation of
that maximum value, which indeed it is, assuming sizeof(unsigned short) == 2
and CHAR_BIT == 8, which are fair assumptions here.  A better patch, if we
needed it, would be to just make portstr[] an array of 11 char, or perhaps make
it a VLA (but we can't yet use VLAs, I don't think, because of older Windows
systems that must be supported still).
This commit is contained in:
Nicolas Williams
2017-10-05 10:43:42 -05:00
parent 6d27e00489
commit b2f6ba0fff

View File

@@ -353,13 +353,13 @@ krb5_krbhst_format_string(krb5_context context, const krb5_krbhst_info *host,
char *hostname, size_t hostlen) char *hostname, size_t hostlen)
{ {
const char *proto = ""; const char *proto = "";
char portstr[7] = {0}; char portstr[7] = "";
if(host->proto == KRB5_KRBHST_TCP) if(host->proto == KRB5_KRBHST_TCP)
proto = "tcp/"; proto = "tcp/";
else if(host->proto == KRB5_KRBHST_HTTP) else if(host->proto == KRB5_KRBHST_HTTP)
proto = "http://"; proto = "http://";
if(host->port != host->def_port) if(host->port != host->def_port)
snprintf(portstr, sizeof(portstr), ":%hd", host->port); snprintf(portstr, sizeof(portstr), ":%d", host->port);
snprintf(hostname, hostlen, "%s%s%s", proto, host->hostname, portstr); snprintf(hostname, hostlen, "%s%s%s", proto, host->hostname, portstr);
return 0; return 0;
} }