From 57d5f3ee1318678332652342e18072230bfaea45 Mon Sep 17 00:00:00 2001 From: Henri Sivonen Date: Tue, 21 Aug 2018 14:20:48 +0000 Subject: [PATCH] Bug 1484984 - Avoid writing past the logical length of a string in networking code. r=valentin MozReview-Commit-ID: IIffoxnF6KS Differential Revision: https://phabricator.services.mozilla.com/D3884 --HG-- extra : moz-landing-system : lando --- netwerk/base/Dashboard.cpp | 2 +- netwerk/base/nsNetAddr.cpp | 4 ++-- netwerk/base/nsSocketTransport2.cpp | 2 +- netwerk/protocol/http/Http2Compression.cpp | 2 +- netwerk/protocol/http/HttpBaseChannel.cpp | 4 ++-- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/netwerk/base/Dashboard.cpp b/netwerk/base/Dashboard.cpp index 4868b52e701d..5f0c77d286d5 100644 --- a/netwerk/base/Dashboard.cpp +++ b/netwerk/base/Dashboard.cpp @@ -886,7 +886,7 @@ HttpConnInfo::SetHTTP2ProtocolVersion(SpdyVersion pv) NS_IMETHODIMP Dashboard::GetLogPath(nsACString &aLogPath) { - aLogPath.SetCapacity(2048); + aLogPath.SetLength(2048); uint32_t len = LogModule::GetLogFile(aLogPath.BeginWriting(), 2048); aLogPath.SetLength(len); return NS_OK; diff --git a/netwerk/base/nsNetAddr.cpp b/netwerk/base/nsNetAddr.cpp index 19eb4bd597bc..01c496ec94de 100644 --- a/netwerk/base/nsNetAddr.cpp +++ b/netwerk/base/nsNetAddr.cpp @@ -45,12 +45,12 @@ NS_IMETHODIMP nsNetAddr::GetAddress(nsACString & aAddress) switch(mAddr.raw.family) { /* PR_NetAddrToString can handle INET and INET6, but not LOCAL. */ case AF_INET: - aAddress.SetCapacity(kIPv4CStrBufSize); + aAddress.SetLength(kIPv4CStrBufSize); NetAddrToString(&mAddr, aAddress.BeginWriting(), kIPv4CStrBufSize); aAddress.SetLength(strlen(aAddress.BeginReading())); break; case AF_INET6: - aAddress.SetCapacity(kIPv6CStrBufSize); + aAddress.SetLength(kIPv6CStrBufSize); NetAddrToString(&mAddr, aAddress.BeginWriting(), kIPv6CStrBufSize); aAddress.SetLength(strlen(aAddress.BeginReading())); break; diff --git a/netwerk/base/nsSocketTransport2.cpp b/netwerk/base/nsSocketTransport2.cpp index e606e571007f..df155e4c5f96 100644 --- a/netwerk/base/nsSocketTransport2.cpp +++ b/netwerk/base/nsSocketTransport2.cpp @@ -1344,7 +1344,7 @@ nsSocketTransport::InitiateSocket() IsIPAddrLocal(&mNetAddr)) { if (SOCKET_LOG_ENABLED()) { nsAutoCString netAddrCString; - netAddrCString.SetCapacity(kIPv6CStrBufSize); + netAddrCString.SetLength(kIPv6CStrBufSize); if (!NetAddrToString(&mNetAddr, netAddrCString.BeginWriting(), kIPv6CStrBufSize)) diff --git a/netwerk/protocol/http/Http2Compression.cpp b/netwerk/protocol/http/Http2Compression.cpp index 4ee353eec693..08e6934e53c4 100644 --- a/netwerk/protocol/http/Http2Compression.cpp +++ b/netwerk/protocol/http/Http2Compression.cpp @@ -1092,8 +1092,8 @@ Http2Compressor::EncodeHeaderBlock(const nsCString &nvInput, { mSetInitialMaxBufferSizeAllowed = false; mOutput = &output; - output.SetCapacity(1024); output.Truncate(); + output.SetCapacity(1024); mParsedContentLength = -1; // first thing's first - context size updates (if necessary) diff --git a/netwerk/protocol/http/HttpBaseChannel.cpp b/netwerk/protocol/http/HttpBaseChannel.cpp index 31221da0d64e..099d10398a19 100644 --- a/netwerk/protocol/http/HttpBaseChannel.cpp +++ b/netwerk/protocol/http/HttpBaseChannel.cpp @@ -2591,7 +2591,7 @@ HttpBaseChannel::GetLocalAddress(nsACString& addr) if (mSelfAddr.raw.family == PR_AF_UNSPEC) return NS_ERROR_NOT_AVAILABLE; - addr.SetCapacity(kIPv6CStrBufSize); + addr.SetLength(kIPv6CStrBufSize); NetAddrToString(&mSelfAddr, addr.BeginWriting(), kIPv6CStrBufSize); addr.SetLength(strlen(addr.BeginReading())); @@ -2700,7 +2700,7 @@ HttpBaseChannel::GetRemoteAddress(nsACString& addr) if (mPeerAddr.raw.family == PR_AF_UNSPEC) return NS_ERROR_NOT_AVAILABLE; - addr.SetCapacity(kIPv6CStrBufSize); + addr.SetLength(kIPv6CStrBufSize); NetAddrToString(&mPeerAddr, addr.BeginWriting(), kIPv6CStrBufSize); addr.SetLength(strlen(addr.BeginReading()));