Skip to content

Commit

Permalink
Bug 96108: Inline ElementAt(), add SafeElementAt(), voidarray usage f…
Browse files Browse the repository at this point in the history
…ixes,

assertions for bad voidarray indexes.  r=jst, sr=waterson
  • Loading branch information
rjesup%wgate.com committed Dec 21, 2001
1 parent 8da65d8 commit 2e680f1
Show file tree
Hide file tree
Showing 78 changed files with 1,107 additions and 677 deletions.
7 changes: 3 additions & 4 deletions content/base/src/nsContentList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ NS_IMETHODIMP
nsBaseContentList::Item(PRUint32 aIndex, nsIDOMNode** aReturn)
{
nsISupports *tmp = NS_REINTERPRET_CAST(nsISupports *,
mElements.ElementAt(aIndex));

mElements.SafeElementAt(aIndex));
if (!tmp) {
*aReturn = nsnull;

Expand Down Expand Up @@ -410,8 +409,8 @@ nsContentList::Item(PRUint32 aIndex, nsIDOMNode** aReturn, PRBool aDoFlush)
}

nsISupports *element = NS_STATIC_CAST(nsISupports *,
mElements.ElementAt(aIndex));

mElements.SafeElementAt(aIndex));
if (element) {
result = CallQueryInterface(element, aReturn);
}
Expand Down
56 changes: 37 additions & 19 deletions content/base/src/nsDocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -488,8 +488,10 @@ nsDocument::~nsDocument()
mInDestructor = PR_TRUE;
PRInt32 indx;
for (indx = 0; indx < mObservers.Count(); indx++) {
// XXX Should this be a kungfudeathgrip?!!!!
nsIDocumentObserver* observer = (nsIDocumentObserver*)mObservers.ElementAt(indx);
observer->DocumentWillBeDestroyed(this);
// Test to see if the observer was removed
if (observer != (nsIDocumentObserver*)mObservers.ElementAt(indx)) {
indx--;
}
Expand Down Expand Up @@ -1102,8 +1104,9 @@ PRInt32 nsDocument::GetNumberOfShells()
NS_IMETHODIMP
nsDocument::GetShellAt(PRInt32 aIndex, nsIPresShell** aShell)
{
*aShell = (nsIPresShell*) mPresShells.ElementAt(aIndex);
*aShell = (nsIPresShell*) mPresShells.SafeElementAt(aIndex);
NS_IF_ADDREF(*aShell);

return NS_OK;
}

Expand Down Expand Up @@ -1143,8 +1146,9 @@ nsDocument::GetNumberOfSubDocuments(PRInt32* aCount)
NS_IMETHODIMP
nsDocument::GetSubDocumentAt(PRInt32 aIndex, nsIDocument** aSubDoc)
{
*aSubDoc = (nsIDocument*) mSubDocuments.ElementAt(aIndex);
*aSubDoc = (nsIDocument*) mSubDocuments.SafeElementAt(aIndex);
NS_IF_ADDREF(*aSubDoc);

return NS_OK;
}

Expand Down Expand Up @@ -1207,8 +1211,9 @@ nsDocument::GetNumberOfStyleSheets(PRInt32* aCount)
NS_IMETHODIMP
nsDocument::GetStyleSheetAt(PRInt32 aIndex, nsIStyleSheet** aSheet)
{
*aSheet = (nsIStyleSheet*)mStyleSheets.ElementAt(aIndex);
*aSheet = (nsIStyleSheet*)mStyleSheets.SafeElementAt(aIndex);
NS_IF_ADDREF(*aSheet);

return NS_OK;
}

Expand Down Expand Up @@ -1256,6 +1261,7 @@ void nsDocument::AddStyleSheet(nsIStyleSheet* aSheet)
for (PRInt32 indx = 0; indx < mObservers.Count(); indx++) {
nsIDocumentObserver* observer = (nsIDocumentObserver*)mObservers.ElementAt(indx);
observer->StyleSheetAdded(this, aSheet);
// handle the observer removing itself!
if (observer != (nsIDocumentObserver*)mObservers.ElementAt(indx)) {
indx--;
}
Expand Down Expand Up @@ -1293,6 +1299,7 @@ void nsDocument::RemoveStyleSheet(nsIStyleSheet* aSheet)
for (PRInt32 indx = 0; indx < mObservers.Count(); indx++) {
nsIDocumentObserver* observer = (nsIDocumentObserver*)mObservers.ElementAt(indx);
observer->StyleSheetRemoved(this, aSheet);
// handle the observer removing itself!
if (observer != (nsIDocumentObserver*)mObservers.ElementAt(indx)) {
indx--;
}
Expand Down Expand Up @@ -1352,6 +1359,7 @@ nsDocument::UpdateStyleSheets(nsISupportsArray* aOldSheets, nsISupportsArray* aN
for (PRInt32 indx = 0; indx < mObservers.Count(); indx++) {
nsIDocumentObserver* observer = (nsIDocumentObserver*)mObservers.ElementAt(indx);
observer->StyleSheetRemoved(this, sheet);
// handle the observer removing itself!
if (observer != (nsIDocumentObserver*)mObservers.ElementAt(indx)) {
indx--;
}
Expand Down Expand Up @@ -1396,6 +1404,7 @@ nsDocument::InsertStyleSheetAt(nsIStyleSheet* aSheet, PRInt32 aIndex, PRBool aNo
for (indx = 0; indx < mObservers.Count(); indx++) {
nsIDocumentObserver* observer = (nsIDocumentObserver*)mObservers.ElementAt(indx);
observer->StyleSheetAdded(this, aSheet);
// handle the observer removing itself!
if (observer != (nsIDocumentObserver*)mObservers.ElementAt(indx)) {
indx--;
}
Expand Down Expand Up @@ -1433,6 +1442,7 @@ void nsDocument::SetStyleSheetDisabledState(nsIStyleSheet* aSheet,
for (indx = 0; indx < mObservers.Count(); indx++) {
nsIDocumentObserver* observer = (nsIDocumentObserver*)mObservers.ElementAt(indx);
observer->StyleSheetDisabledStateChanged(this, aSheet, aDisabled);
// handle the observer removing itself!
if (observer != (nsIDocumentObserver*)mObservers.ElementAt(indx)) {
indx--;
}
Expand Down Expand Up @@ -2328,6 +2338,7 @@ nsDocument::GetDefaultView(nsIDOMAbstractView** aDefaultView)
NS_ENSURE_ARG_POINTER(aDefaultView);
*aDefaultView = nsnull;

NS_ENSURE_TRUE(mPresShells.Count() != 0, NS_OK);
nsIPresShell *shell = NS_STATIC_CAST(nsIPresShell *,
mPresShells.ElementAt(0));
NS_ENSURE_TRUE(shell, NS_OK);
Expand Down Expand Up @@ -2361,6 +2372,7 @@ nsDocument::GetPlugins(nsIDOMPluginArray** aPlugins)
*aPlugins = nsnull;

// XXX Could also get this through mScriptGlobalObject
NS_ENSURE_TRUE(mPresShells.Count() != 0, NS_OK);
nsIPresShell *shell = NS_STATIC_CAST(nsIPresShell *,
mPresShells.ElementAt(0));
NS_ENSURE_TRUE(shell, NS_OK);
Expand Down Expand Up @@ -2564,7 +2576,7 @@ NS_IMETHODIMP
nsDocument::GetDir(nsAWritableString& aDirection)
{
#ifdef IBMBIDI
nsIPresShell* shell = (nsIPresShell*) mPresShells.ElementAt(0);
nsIPresShell* shell = (nsIPresShell*) mPresShells.SafeElementAt(0);
if (shell) {
nsCOMPtr<nsIPresContext> context;
shell->GetPresContext(getter_AddRefs(context) );
Expand Down Expand Up @@ -2594,22 +2606,24 @@ NS_IMETHODIMP
nsDocument::SetDir(const nsAReadableString& aDirection)
{
#ifdef IBMBIDI
nsIPresShell* shell = (nsIPresShell*) mPresShells.ElementAt(0);
if (shell) {
nsCOMPtr<nsIPresContext> context;
shell->GetPresContext(getter_AddRefs(context) );
if (context) {
PRUint32 options;
context->GetBidi(&options);
for (const DirTable* elt = dirAttributes; elt->mName; elt++) {
if (aDirection == NS_ConvertASCIItoUCS2(elt->mName) ) {
if (GET_BIDI_OPTION_DIRECTION(options) != elt->mValue) {
SET_BIDI_OPTION_DIRECTION(options, elt->mValue);
context->SetBidi(options, PR_TRUE);
if (mPresShells.Count() != 0) {
nsIPresShell* shell = (nsIPresShell*) mPresShells.ElementAt(0);
if (shell) {
nsCOMPtr<nsIPresContext> context;
shell->GetPresContext(getter_AddRefs(context) );
if (context) {
PRUint32 options;
context->GetBidi(&options);
for (const DirTable* elt = dirAttributes; elt->mName; elt++) {
if (aDirection == NS_ConvertASCIItoUCS2(elt->mName) ) {
if (GET_BIDI_OPTION_DIRECTION(options) != elt->mValue) {
SET_BIDI_OPTION_DIRECTION(options, elt->mValue);
context->SetBidi(options, PR_TRUE);
}
break;
}
break;
}
} // for
} // for
}
}
}
#endif // IBMBIDI
Expand Down Expand Up @@ -3161,6 +3175,8 @@ nsDocument::DispatchEvent(nsIDOMEvent* aEvent, PRBool *_retval)

nsCOMPtr<nsIPresShell> shell;
GetShellAt(0, getter_AddRefs(shell));
if (!shell)
return NS_ERROR_FAILURE;

// Retrieve the context
nsCOMPtr<nsIPresContext> presContext;
Expand Down Expand Up @@ -3189,6 +3205,8 @@ nsDocument::CreateEvent(const nsAReadableString& aEventType,

nsCOMPtr<nsIPresShell> shell;
GetShellAt(0, getter_AddRefs(shell));
if (!shell)
return NS_ERROR_FAILURE;

// Retrieve the context
nsCOMPtr<nsIPresContext> presContext;
Expand Down
46 changes: 26 additions & 20 deletions content/base/src/nsDocumentEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -660,9 +660,14 @@ nsDocumentEncoder::SerializeRangeNodes(nsIDOMRange* aRange,

// get start and end nodes for this recursion level
nsCOMPtr<nsIContent> startNode, endNode;
startNode = NS_STATIC_CAST(nsIContent *, mStartNodes[mStartRootIndex - aDepth]);
endNode = NS_STATIC_CAST(nsIContent *, mEndNodes[mEndRootIndex - aDepth]);

PRInt32 start = mStartRootIndex - aDepth;
if (start >= 0 && start <= mStartNodes.Count())
startNode = NS_STATIC_CAST(nsIContent *, mStartNodes[start]);

PRInt32 end = mEndRootIndex - aDepth;
if (end >= 0 && end <= mEndNodes.Count())
endNode = NS_STATIC_CAST(nsIContent *, mEndNodes[end]);

if ((startNode != content) && (endNode != content))
{
// node is completely contained in range. Serialize the whole subtree
Expand Down Expand Up @@ -713,9 +718,9 @@ nsDocumentEncoder::SerializeRangeNodes(nsIDOMRange* aRange,
nsCOMPtr<nsIContent> child;
nsCOMPtr<nsIDOMNode> childAsNode;
PRInt32 startOffset = 0, endOffset = -1;
if (startNode == content)
if (startNode == content && mStartRootIndex >= aDepth)
startOffset = NS_PTR_TO_INT32(mStartOffsets[mStartRootIndex - aDepth]);
if (endNode == content)
if (endNode == content && mEndRootIndex >= aDepth)
endOffset = NS_PTR_TO_INT32(mEndOffsets[mEndRootIndex - aDepth]) ;
// generated content will cause offset values of -1 to be returned.
PRInt32 j, childCount=0;
Expand Down Expand Up @@ -769,7 +774,7 @@ nsDocumentEncoder::SerializeRangeContextStart(const nsVoidArray& aAncestorArray,
PRInt32 i = aAncestorArray.Count();
nsresult rv = NS_OK;

while (i) {
while (i > 0) {
nsIDOMNode *node = (nsIDOMNode *)aAncestorArray.ElementAt(--i);

if (!node)
Expand All @@ -791,9 +796,10 @@ nsDocumentEncoder::SerializeRangeContextEnd(const nsVoidArray& aAncestorArray,
nsAWritableString& aString)
{
PRInt32 i = 0;
PRInt32 count = aAncestorArray.Count();
nsresult rv = NS_OK;

while (1) {
while (i < count) {
nsIDOMNode *node = (nsIDOMNode *)aAncestorArray.ElementAt(i++);

if (!node)
Expand Down Expand Up @@ -1222,33 +1228,33 @@ nsHTMLCopyEncoder::EncodeToStringWithContext(nsAWritableString& aEncodedString,
// where all the cells are in the same table.

// leaf of ancestors might be text node. If so discard it.
PRInt32 count = mCommonAncestors.Count();
PRInt32 i;
nsCOMPtr<nsIDOMNode> node;
node = NS_STATIC_CAST(nsIDOMNode *, mCommonAncestors.ElementAt(0));
if (count > 0)
node = NS_STATIC_CAST(nsIDOMNode *, mCommonAncestors.ElementAt(0));

if (node && IsTextNode(node))
{
mCommonAncestors.RemoveElementAt(0);
// don't forget to adjust range depth info
if (mStartDepth) mStartDepth--;
if (mEndDepth) mEndDepth--;
// and the count
count--;
}

PRInt32 i = mCommonAncestors.Count();

node = NS_STATIC_CAST(nsIDOMNode *, mCommonAncestors.ElementAt(--i));

while (node)
i = count;
while (i > 0)
{
SerializeNodeStart(node, 0, -1, aContextString);
node = NS_STATIC_CAST(nsIDOMNode *, mCommonAncestors.ElementAt(--i));
SerializeNodeStart(node, 0, -1, aContextString);
}

i = 0;
node = NS_STATIC_CAST(nsIDOMNode *, mCommonAncestors.ElementAt(i));

while (node)
//i = 0; guaranteed by above
while (i < count)
{
node = NS_STATIC_CAST(nsIDOMNode *, mCommonAncestors.ElementAt(i++));
SerializeNodeEnd(node, aContextString);
node = NS_STATIC_CAST(nsIDOMNode *, mCommonAncestors.ElementAt(++i));
}

// encode range info : the start and end depth of the selection, where the depth is
Expand Down
4 changes: 2 additions & 2 deletions content/base/src/nsGenericDOMDataNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,15 @@ nsGenericDOMDataNode::GetPreviousSibling(nsIDOMNode** aPrevSibling)
if (parent_weak) {
PRInt32 pos;
parent_weak->IndexOf(this, pos);
if (pos > -1 ) {
if (pos > 0 ) {
parent_weak->ChildAt(--pos, *getter_AddRefs(sibling));
}
} else if (mDocument) {
// Nodes that are just below the document (their parent is the
// document) need to go to the document to find their next sibling.
PRInt32 pos;
mDocument->IndexOf(this, pos);
if (pos > -1 ) {
if (pos > 0 ) {
mDocument->ChildAt(--pos, *getter_AddRefs(sibling));
}
}
Expand Down
Loading

0 comments on commit 2e680f1

Please sign in to comment.