From b2f6ba0fff60aba5af96cdd0c7e26047e0adf396 Mon Sep 17 00:00:00 2001 From: Nicolas Williams Date: Thu, 5 Oct 2017 10:43:42 -0500 Subject: [PATCH] Revert "lib: Fix printing a short into portstr" This reverts commit ccb63bb0aae60f4d2286dc0bd01656602a087308, 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). --- lib/krb5/krbhst.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/krb5/krbhst.c b/lib/krb5/krbhst.c index 313080d5e..f53512883 100644 --- a/lib/krb5/krbhst.c +++ b/lib/krb5/krbhst.c @@ -353,13 +353,13 @@ krb5_krbhst_format_string(krb5_context context, const krb5_krbhst_info *host, char *hostname, size_t hostlen) { const char *proto = ""; - char portstr[7] = {0}; + char portstr[7] = ""; if(host->proto == KRB5_KRBHST_TCP) proto = "tcp/"; else if(host->proto == KRB5_KRBHST_HTTP) proto = "http://"; 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); return 0; }