From 65c2e51849de0c58f6ca1a85aaa9d3b78c0ee04d Mon Sep 17 00:00:00 2001 From: Jeff Hill Date: Mon, 22 Sep 2008 20:24:39 +0000 Subject: [PATCH] committed a fix for mantis 312 --- src/ca/hostNameCache.cpp | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/ca/hostNameCache.cpp b/src/ca/hostNameCache.cpp index b4fb5d2d6..9522678ac 100644 --- a/src/ca/hostNameCache.cpp +++ b/src/ca/hostNameCache.cpp @@ -33,6 +33,9 @@ hostNameCache::hostNameCache ( const osiSockAddr & addr, ipAddrToAsciiEngine & engine ) : dnsTransaction ( engine.createTransaction() ), nameLength ( 0 ) { + sockAddrToDottedIP ( &addr.sa, hostNameBuf, sizeof ( hostNameBuf ) ); + hostNameBuf[ sizeof ( hostNameBuf ) - 1 ] = '\0'; + nameLength = strlen ( hostNameBuf ); this->dnsTransaction.ipAddrToAscii ( addr, *this ); } @@ -49,11 +52,14 @@ hostNameCache::~hostNameCache () void hostNameCache::transactionComplete ( const char * pHostNameIn ) { epicsGuard < epicsMutex > guard ( this->mutex ); - if ( this->nameLength == 0u ) { - strncpy ( this->hostNameBuf, pHostNameIn, sizeof ( this->hostNameBuf ) ); - this->hostNameBuf[ sizeof ( this->hostNameBuf ) - 1 ] = '\0'; - this->nameLength = strlen ( this->hostNameBuf ); - } + // a few legacy clients have a direct pointer to this buffer so we + // set the entrire string to nill terminators before we start copying + // in the name (this reduces the chance that another thread will see + // garbage characters). + strncpy ( this->hostNameBuf, "", sizeof ( this->hostNameBuf ) ); + strncpy ( this->hostNameBuf, pHostNameIn, sizeof ( this->hostNameBuf ) - 1 ); + this->hostNameBuf[ sizeof ( this->hostNameBuf ) - 1 ] = '\0'; + this->nameLength = strlen ( this->hostNameBuf ); } unsigned hostNameCache::getName (