From 73c490c4d3f69ec38ebab86fd3eb8dd8ba186827 Mon Sep 17 00:00:00 2001 From: "rickg%netscape.com" Date: Sun, 19 Sep 1999 16:43:09 +0000 Subject: [PATCH] improved safety of CBufDescriptor --- string/obsolete/nsStr.cpp | 30 +++++++++++++++++++++++++++++ string/obsolete/nsStr.h | 8 +++++--- string/obsolete/nsString.cpp | 3 ++- string/obsolete/nsString2.cpp | 4 +++- xpcom/ds/nsStr.cpp | 30 +++++++++++++++++++++++++++++ xpcom/ds/nsStr.h | 8 +++++--- xpcom/ds/nsString.cpp | 3 ++- xpcom/ds/nsString2.cpp | 4 +++- xpcom/string/obsolete/nsStr.cpp | 30 +++++++++++++++++++++++++++++ xpcom/string/obsolete/nsStr.h | 8 +++++--- xpcom/string/obsolete/nsString.cpp | 3 ++- xpcom/string/obsolete/nsString2.cpp | 4 +++- 12 files changed, 120 insertions(+), 15 deletions(-) diff --git a/string/obsolete/nsStr.cpp b/string/obsolete/nsStr.cpp index 087c73f389d0..98d856c27ea6 100644 --- a/string/obsolete/nsStr.cpp +++ b/string/obsolete/nsStr.cpp @@ -600,6 +600,7 @@ CBufDescriptor::CBufDescriptor(char* aString,PRBool aStackBased,PRUint32 aCapaci mBuffer=aString; mCharSize=eOneByte; mStackBased=aStackBased; + mIsConst=PR_FALSE; mLength=mCapacity=0; if(aString && aCapacity>1) { mCapacity=aCapacity-1; @@ -609,11 +610,27 @@ CBufDescriptor::CBufDescriptor(char* aString,PRBool aStackBased,PRUint32 aCapaci } } +CBufDescriptor::CBufDescriptor(const char* aString,PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength) { + mBuffer=(char*)aString; + mCharSize=eOneByte; + mStackBased=aStackBased; + mIsConst=PR_TRUE; + mLength=mCapacity=0; + if(aString && aCapacity>1) { + mCapacity=aCapacity-1; + mLength=(-1==aLength) ? strlen(aString) : aLength; + if(mLength>PRInt32(mCapacity)) + mLength=mCapacity; + } +} + + CBufDescriptor::CBufDescriptor(PRUnichar* aString,PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength) { mBuffer=(char*)aString; mCharSize=eTwoByte; mStackBased=aStackBased; mLength=mCapacity=0; + mIsConst=PR_FALSE; if(aString && aCapacity>1) { mCapacity=aCapacity-1; mLength=(-1==aLength) ? nsCRT::strlen(aString) : aLength; @@ -622,5 +639,18 @@ CBufDescriptor::CBufDescriptor(PRUnichar* aString,PRBool aStackBased,PRUint32 aC } } +CBufDescriptor::CBufDescriptor(const PRUnichar* aString,PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength) { + mBuffer=(char*)aString; + mCharSize=eTwoByte; + mStackBased=aStackBased; + mLength=mCapacity=0; + mIsConst=PR_TRUE; + if(aString && aCapacity>1) { + mCapacity=aCapacity-1; + mLength=(-1==aLength) ? nsCRT::strlen(aString) : aLength; + if(mLength>PRInt32(mCapacity)) + mLength=mCapacity; + } +} //---------------------------------------------------------------------------------------- diff --git a/string/obsolete/nsStr.h b/string/obsolete/nsStr.h index daf814e797e2..5f194705a998 100644 --- a/string/obsolete/nsStr.h +++ b/string/obsolete/nsStr.h @@ -183,15 +183,17 @@ class nsIMemoryAgent; class NS_COM CBufDescriptor { public: - CBufDescriptor(char* aString, PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength=-1); - CBufDescriptor(PRUnichar* aString,PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength=-1); + CBufDescriptor(char* aString, PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength=-1); + CBufDescriptor(const char* aString, PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength=-1); + CBufDescriptor(PRUnichar* aString, PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength=-1); + CBufDescriptor(const PRUnichar* aString,PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength=-1); char* mBuffer; eCharSize mCharSize; PRUint32 mCapacity; PRInt32 mLength; PRBool mStackBased; - + PRBool mIsConst; }; //---------------------------------------------------------------------------------------- diff --git a/string/obsolete/nsString.cpp b/string/obsolete/nsString.cpp index b3a868dcca82..1b9109be5d66 100644 --- a/string/obsolete/nsString.cpp +++ b/string/obsolete/nsString.cpp @@ -1753,7 +1753,8 @@ nsCAutoString::nsCAutoString(const CBufDescriptor& aBuffer) : nsCString() { else { nsStr::Initialize(*this,aBuffer.mBuffer,aBuffer.mCapacity,aBuffer.mLength,aBuffer.mCharSize,!aBuffer.mStackBased); } - AddNullTerminator(*this); //this isn't really needed, but it guarantees that folks don't pass string constants. + if(!aBuffer.mIsConst) + AddNullTerminator(*this); //this isn't really needed, but it guarantees that folks don't pass string constants. } /** diff --git a/string/obsolete/nsString2.cpp b/string/obsolete/nsString2.cpp index 58efcd347103..2049c9d1b174 100644 --- a/string/obsolete/nsString2.cpp +++ b/string/obsolete/nsString2.cpp @@ -109,6 +109,7 @@ nsString::nsString(const nsString& aString) :mAgent(aString.mAgent) { * @param reference to a subsumeString */ nsString::nsString(nsSubsumeStr& aSubsumeStr) :mAgent(0) { + nsStr::Initialize(*this,eTwoByte); Subsume(*this,aSubsumeStr); } @@ -2149,7 +2150,8 @@ nsAutoString::nsAutoString(const CBufDescriptor& aBuffer) : nsString() { else { nsStr::Initialize(*this,aBuffer.mBuffer,aBuffer.mCapacity,aBuffer.mLength,aBuffer.mCharSize,!aBuffer.mStackBased); } - AddNullTerminator(*this); + if(!aBuffer.mIsConst) + AddNullTerminator(*this); } diff --git a/xpcom/ds/nsStr.cpp b/xpcom/ds/nsStr.cpp index 087c73f389d0..98d856c27ea6 100644 --- a/xpcom/ds/nsStr.cpp +++ b/xpcom/ds/nsStr.cpp @@ -600,6 +600,7 @@ CBufDescriptor::CBufDescriptor(char* aString,PRBool aStackBased,PRUint32 aCapaci mBuffer=aString; mCharSize=eOneByte; mStackBased=aStackBased; + mIsConst=PR_FALSE; mLength=mCapacity=0; if(aString && aCapacity>1) { mCapacity=aCapacity-1; @@ -609,11 +610,27 @@ CBufDescriptor::CBufDescriptor(char* aString,PRBool aStackBased,PRUint32 aCapaci } } +CBufDescriptor::CBufDescriptor(const char* aString,PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength) { + mBuffer=(char*)aString; + mCharSize=eOneByte; + mStackBased=aStackBased; + mIsConst=PR_TRUE; + mLength=mCapacity=0; + if(aString && aCapacity>1) { + mCapacity=aCapacity-1; + mLength=(-1==aLength) ? strlen(aString) : aLength; + if(mLength>PRInt32(mCapacity)) + mLength=mCapacity; + } +} + + CBufDescriptor::CBufDescriptor(PRUnichar* aString,PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength) { mBuffer=(char*)aString; mCharSize=eTwoByte; mStackBased=aStackBased; mLength=mCapacity=0; + mIsConst=PR_FALSE; if(aString && aCapacity>1) { mCapacity=aCapacity-1; mLength=(-1==aLength) ? nsCRT::strlen(aString) : aLength; @@ -622,5 +639,18 @@ CBufDescriptor::CBufDescriptor(PRUnichar* aString,PRBool aStackBased,PRUint32 aC } } +CBufDescriptor::CBufDescriptor(const PRUnichar* aString,PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength) { + mBuffer=(char*)aString; + mCharSize=eTwoByte; + mStackBased=aStackBased; + mLength=mCapacity=0; + mIsConst=PR_TRUE; + if(aString && aCapacity>1) { + mCapacity=aCapacity-1; + mLength=(-1==aLength) ? nsCRT::strlen(aString) : aLength; + if(mLength>PRInt32(mCapacity)) + mLength=mCapacity; + } +} //---------------------------------------------------------------------------------------- diff --git a/xpcom/ds/nsStr.h b/xpcom/ds/nsStr.h index daf814e797e2..5f194705a998 100644 --- a/xpcom/ds/nsStr.h +++ b/xpcom/ds/nsStr.h @@ -183,15 +183,17 @@ class nsIMemoryAgent; class NS_COM CBufDescriptor { public: - CBufDescriptor(char* aString, PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength=-1); - CBufDescriptor(PRUnichar* aString,PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength=-1); + CBufDescriptor(char* aString, PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength=-1); + CBufDescriptor(const char* aString, PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength=-1); + CBufDescriptor(PRUnichar* aString, PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength=-1); + CBufDescriptor(const PRUnichar* aString,PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength=-1); char* mBuffer; eCharSize mCharSize; PRUint32 mCapacity; PRInt32 mLength; PRBool mStackBased; - + PRBool mIsConst; }; //---------------------------------------------------------------------------------------- diff --git a/xpcom/ds/nsString.cpp b/xpcom/ds/nsString.cpp index b3a868dcca82..1b9109be5d66 100644 --- a/xpcom/ds/nsString.cpp +++ b/xpcom/ds/nsString.cpp @@ -1753,7 +1753,8 @@ nsCAutoString::nsCAutoString(const CBufDescriptor& aBuffer) : nsCString() { else { nsStr::Initialize(*this,aBuffer.mBuffer,aBuffer.mCapacity,aBuffer.mLength,aBuffer.mCharSize,!aBuffer.mStackBased); } - AddNullTerminator(*this); //this isn't really needed, but it guarantees that folks don't pass string constants. + if(!aBuffer.mIsConst) + AddNullTerminator(*this); //this isn't really needed, but it guarantees that folks don't pass string constants. } /** diff --git a/xpcom/ds/nsString2.cpp b/xpcom/ds/nsString2.cpp index 58efcd347103..2049c9d1b174 100644 --- a/xpcom/ds/nsString2.cpp +++ b/xpcom/ds/nsString2.cpp @@ -109,6 +109,7 @@ nsString::nsString(const nsString& aString) :mAgent(aString.mAgent) { * @param reference to a subsumeString */ nsString::nsString(nsSubsumeStr& aSubsumeStr) :mAgent(0) { + nsStr::Initialize(*this,eTwoByte); Subsume(*this,aSubsumeStr); } @@ -2149,7 +2150,8 @@ nsAutoString::nsAutoString(const CBufDescriptor& aBuffer) : nsString() { else { nsStr::Initialize(*this,aBuffer.mBuffer,aBuffer.mCapacity,aBuffer.mLength,aBuffer.mCharSize,!aBuffer.mStackBased); } - AddNullTerminator(*this); + if(!aBuffer.mIsConst) + AddNullTerminator(*this); } diff --git a/xpcom/string/obsolete/nsStr.cpp b/xpcom/string/obsolete/nsStr.cpp index 087c73f389d0..98d856c27ea6 100644 --- a/xpcom/string/obsolete/nsStr.cpp +++ b/xpcom/string/obsolete/nsStr.cpp @@ -600,6 +600,7 @@ CBufDescriptor::CBufDescriptor(char* aString,PRBool aStackBased,PRUint32 aCapaci mBuffer=aString; mCharSize=eOneByte; mStackBased=aStackBased; + mIsConst=PR_FALSE; mLength=mCapacity=0; if(aString && aCapacity>1) { mCapacity=aCapacity-1; @@ -609,11 +610,27 @@ CBufDescriptor::CBufDescriptor(char* aString,PRBool aStackBased,PRUint32 aCapaci } } +CBufDescriptor::CBufDescriptor(const char* aString,PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength) { + mBuffer=(char*)aString; + mCharSize=eOneByte; + mStackBased=aStackBased; + mIsConst=PR_TRUE; + mLength=mCapacity=0; + if(aString && aCapacity>1) { + mCapacity=aCapacity-1; + mLength=(-1==aLength) ? strlen(aString) : aLength; + if(mLength>PRInt32(mCapacity)) + mLength=mCapacity; + } +} + + CBufDescriptor::CBufDescriptor(PRUnichar* aString,PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength) { mBuffer=(char*)aString; mCharSize=eTwoByte; mStackBased=aStackBased; mLength=mCapacity=0; + mIsConst=PR_FALSE; if(aString && aCapacity>1) { mCapacity=aCapacity-1; mLength=(-1==aLength) ? nsCRT::strlen(aString) : aLength; @@ -622,5 +639,18 @@ CBufDescriptor::CBufDescriptor(PRUnichar* aString,PRBool aStackBased,PRUint32 aC } } +CBufDescriptor::CBufDescriptor(const PRUnichar* aString,PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength) { + mBuffer=(char*)aString; + mCharSize=eTwoByte; + mStackBased=aStackBased; + mLength=mCapacity=0; + mIsConst=PR_TRUE; + if(aString && aCapacity>1) { + mCapacity=aCapacity-1; + mLength=(-1==aLength) ? nsCRT::strlen(aString) : aLength; + if(mLength>PRInt32(mCapacity)) + mLength=mCapacity; + } +} //---------------------------------------------------------------------------------------- diff --git a/xpcom/string/obsolete/nsStr.h b/xpcom/string/obsolete/nsStr.h index daf814e797e2..5f194705a998 100644 --- a/xpcom/string/obsolete/nsStr.h +++ b/xpcom/string/obsolete/nsStr.h @@ -183,15 +183,17 @@ class nsIMemoryAgent; class NS_COM CBufDescriptor { public: - CBufDescriptor(char* aString, PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength=-1); - CBufDescriptor(PRUnichar* aString,PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength=-1); + CBufDescriptor(char* aString, PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength=-1); + CBufDescriptor(const char* aString, PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength=-1); + CBufDescriptor(PRUnichar* aString, PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength=-1); + CBufDescriptor(const PRUnichar* aString,PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength=-1); char* mBuffer; eCharSize mCharSize; PRUint32 mCapacity; PRInt32 mLength; PRBool mStackBased; - + PRBool mIsConst; }; //---------------------------------------------------------------------------------------- diff --git a/xpcom/string/obsolete/nsString.cpp b/xpcom/string/obsolete/nsString.cpp index b3a868dcca82..1b9109be5d66 100644 --- a/xpcom/string/obsolete/nsString.cpp +++ b/xpcom/string/obsolete/nsString.cpp @@ -1753,7 +1753,8 @@ nsCAutoString::nsCAutoString(const CBufDescriptor& aBuffer) : nsCString() { else { nsStr::Initialize(*this,aBuffer.mBuffer,aBuffer.mCapacity,aBuffer.mLength,aBuffer.mCharSize,!aBuffer.mStackBased); } - AddNullTerminator(*this); //this isn't really needed, but it guarantees that folks don't pass string constants. + if(!aBuffer.mIsConst) + AddNullTerminator(*this); //this isn't really needed, but it guarantees that folks don't pass string constants. } /** diff --git a/xpcom/string/obsolete/nsString2.cpp b/xpcom/string/obsolete/nsString2.cpp index 58efcd347103..2049c9d1b174 100644 --- a/xpcom/string/obsolete/nsString2.cpp +++ b/xpcom/string/obsolete/nsString2.cpp @@ -109,6 +109,7 @@ nsString::nsString(const nsString& aString) :mAgent(aString.mAgent) { * @param reference to a subsumeString */ nsString::nsString(nsSubsumeStr& aSubsumeStr) :mAgent(0) { + nsStr::Initialize(*this,eTwoByte); Subsume(*this,aSubsumeStr); } @@ -2149,7 +2150,8 @@ nsAutoString::nsAutoString(const CBufDescriptor& aBuffer) : nsString() { else { nsStr::Initialize(*this,aBuffer.mBuffer,aBuffer.mCapacity,aBuffer.mLength,aBuffer.mCharSize,!aBuffer.mStackBased); } - AddNullTerminator(*this); + if(!aBuffer.mIsConst) + AddNullTerminator(*this); }