diff --git a/lib/isc/include/isc/sockaddr.h b/lib/isc/include/isc/sockaddr.h index 3a9615c161..70ef21cd5d 100644 --- a/lib/isc/include/isc/sockaddr.h +++ b/lib/isc/include/isc/sockaddr.h @@ -76,11 +76,12 @@ isc_sockaddr_totext(const isc_sockaddr_t *sockaddr, isc_buffer_t *target); /* * Append a text representation of 'sockaddr' to the buffer 'target'. * The text will include both the IP address (v4 or v6) and the port. - * The text is not null terminated. + * The text is null terminated, but the terminating null is not + * part of the buffer's used region. * * Returns: * ISC_R_SUCCESS - * ISC_R_NOSPACE The text did not fit. + * ISC_R_NOSPACE The text or the null termination did not fit. */ ISC_LANG_ENDDECLS diff --git a/lib/isc/sockaddr.c b/lib/isc/sockaddr.c index 1ac08dbfb6..53ed193a0a 100644 --- a/lib/isc/sockaddr.c +++ b/lib/isc/sockaddr.c @@ -135,13 +135,18 @@ isc_sockaddr_totext(const isc_sockaddr_t *sockaddr, isc_buffer_t *target) { plen = strlen(pbuf); isc_buffer_available(target, &avail); - if (alen + 1 + plen < avail.length) + if (alen + 1 + plen + 1 < avail.length) return (ISC_R_NOSPACE); isc_buffer_putmem(target, abuf, alen); isc_buffer_putmem(target, "#", 1); isc_buffer_putmem(target, pbuf, plen); + /* Null terminate after used region. */ + isc_buffer_available(target, &avail); + INSIST(avail.length >= 1); + avail.base[0] = '\0'; + return (ISC_R_SUCCESS); }