Bug 1001623 - clean up error handling of index returning methods, r=tbsaunde

This commit is contained in:
Alexander Surkov 2014-05-16 19:16:34 -04:00
parent e9b5f5f62e
commit 82bce51555
4 changed files with 69 additions and 25 deletions

View File

@ -90,6 +90,26 @@ struct GroupPos
int32_t setSize;
};
/**
* An index type. Assert if out of range value was attempted to be used.
*/
class index_t
{
public:
index_t(int32_t aVal) : mVal(aVal) {}
operator uint32_t() const
{
MOZ_ASSERT(mVal >= 0, "Attempt to use wrong index!");
return mVal;
}
bool IsValid() const { return mVal >= 0; }
private:
int32_t mVal;
};
typedef nsRefPtrHashtable<nsPtrHashKey<const void>, Accessible>
AccessibleHashtable;

View File

@ -21,15 +21,17 @@ namespace a11y {
inline bool
HyperTextAccessible::IsValidOffset(int32_t aOffset)
{
return ConvertMagicOffset(aOffset) <= CharacterCount();
index_t offset = ConvertMagicOffset(aOffset);
return offset.IsValid() && offset <= CharacterCount();
}
inline bool
HyperTextAccessible::IsValidRange(int32_t aStartOffset, int32_t aEndOffset)
{
uint32_t endOffset = ConvertMagicOffset(aEndOffset);
return ConvertMagicOffset(aStartOffset) <= endOffset &&
endOffset <= CharacterCount();
index_t startOffset = ConvertMagicOffset(aStartOffset);
index_t endOffset = ConvertMagicOffset(aEndOffset);
return startOffset.IsValid() && endOffset.IsValid() &&
startOffset <= endOffset && endOffset <= CharacterCount();
}
inline void
@ -110,7 +112,7 @@ HyperTextAccessible::PasteText(int32_t aPosition)
}
}
inline uint32_t
inline index_t
HyperTextAccessible::ConvertMagicOffset(int32_t aOffset) const
{
if (aOffset == nsIAccessibleText::TEXT_OFFSET_END_OF_TEXT)
@ -119,7 +121,7 @@ HyperTextAccessible::ConvertMagicOffset(int32_t aOffset) const
if (aOffset == nsIAccessibleText::TEXT_OFFSET_CARET)
return CaretOffset();
return aOffset < 0 ? std::numeric_limits<uint32_t>::max() : aOffset;
return aOffset;
}
inline uint32_t

View File

@ -191,8 +191,13 @@ HyperTextAccessible::TextSubstring(int32_t aStartOffset, int32_t aEndOffset,
{
aText.Truncate();
uint32_t startOffset = ConvertMagicOffset(aStartOffset);
uint32_t endOffset = ConvertMagicOffset(aEndOffset);
index_t startOffset = ConvertMagicOffset(aStartOffset);
index_t endOffset = ConvertMagicOffset(aEndOffset);
if (!startOffset.IsValid() || !endOffset.IsValid() ||
startOffset > endOffset || endOffset > CharacterCount()) {
NS_ERROR("Wrong in offset");
return;
}
int32_t startChildIdx = GetChildIndexAtOffset(startOffset);
if (startChildIdx == -1)
@ -608,9 +613,9 @@ HyperTextAccessible::TextBeforeOffset(int32_t aOffset,
*aStartOffset = *aEndOffset = 0;
aText.Truncate();
uint32_t convertedOffset = ConvertMagicOffset(aOffset);
if (convertedOffset == std::numeric_limits<uint32_t>::max()) {
NS_ERROR("Wrong given offset!");
index_t convertedOffset = ConvertMagicOffset(aOffset);
if (!convertedOffset.IsValid() || convertedOffset > CharacterCount()) {
NS_ERROR("Wrong in offset!");
return;
}
@ -744,9 +749,9 @@ HyperTextAccessible::TextAfterOffset(int32_t aOffset,
*aStartOffset = *aEndOffset = 0;
aText.Truncate();
uint32_t convertedOffset = ConvertMagicOffset(aOffset);
if (convertedOffset == std::numeric_limits<uint32_t>::max()) {
NS_ERROR("Wrong given offset!");
index_t convertedOffset = ConvertMagicOffset(aOffset);
if (!convertedOffset.IsValid() || convertedOffset > CharacterCount()) {
NS_ERROR("Wrong in offset!");
return;
}
@ -814,10 +819,15 @@ HyperTextAccessible::TextAttributes(bool aIncludeDefAttrs, int32_t aOffset,
// the attribute range itself can only stay the same or get smaller.
*aStartOffset = *aEndOffset = 0;
index_t offset = ConvertMagicOffset(aOffset);
if (!offset.IsValid() || offset > CharacterCount()) {
NS_ERROR("Wrong in offset!");
return nullptr;
}
nsCOMPtr<nsIPersistentProperties> attributes =
do_CreateInstance(NS_PERSISTENTPROPERTIES_CONTRACTID);
uint32_t offset = ConvertMagicOffset(aOffset);
Accessible* accAtOffset = GetChildAtOffset(offset);
if (!accAtOffset) {
// Offset 0 is correct offset when accessible has empty text. Include
@ -1029,11 +1039,14 @@ nsIntRect
HyperTextAccessible::TextBounds(int32_t aStartOffset, int32_t aEndOffset,
uint32_t aCoordType)
{
uint32_t startOffset = ConvertMagicOffset(aStartOffset);
uint32_t endOffset = ConvertMagicOffset(aEndOffset);
NS_ASSERTION(startOffset < endOffset &&
endOffset != std::numeric_limits<uint32_t>::max(),
"Wrong bad in!");
index_t startOffset = ConvertMagicOffset(aStartOffset);
index_t endOffset = ConvertMagicOffset(aEndOffset);
if (!startOffset.IsValid() || !endOffset.IsValid() ||
startOffset > endOffset || endOffset > CharacterCount()) {
NS_ERROR("Wrong in offset");
return nsIntRect();
}
int32_t childIdx = GetChildIndexAtOffset(startOffset);
if (childIdx == -1)
@ -1417,8 +1430,13 @@ HyperTextAccessible::SetSelectionBoundsAt(int32_t aSelectionNum,
int32_t aStartOffset,
int32_t aEndOffset)
{
uint32_t startOffset = ConvertMagicOffset(aStartOffset);
uint32_t endOffset = ConvertMagicOffset(aEndOffset);
index_t startOffset = ConvertMagicOffset(aStartOffset);
index_t endOffset = ConvertMagicOffset(aEndOffset);
if (!startOffset.IsValid() || !endOffset.IsValid() ||
startOffset > endOffset || endOffset > CharacterCount()) {
NS_ERROR("Wrong in offset");
return false;
}
dom::Selection* domSel = DOMSelection();
if (!domSel)
@ -1879,7 +1897,7 @@ HyperTextAccessible::GetSpellTextAttr(nsINode* aNode,
if (rangeCount <= 0)
return;
int32_t startOffset = 0, endOffset = 0;
uint32_t startOffset = 0, endOffset = 0;
for (int32_t idx = 0; idx < rangeCount; idx++) {
nsRange* range = domSel->GetRangeAt(idx);
if (range->Collapsed())

View File

@ -296,7 +296,11 @@ public:
* system.
*/
nsIntRect CharBounds(int32_t aOffset, uint32_t aCoordType)
{ return TextBounds(aOffset, aOffset + 1, aCoordType); }
{
int32_t endOffset = aOffset == static_cast<int32_t>(CharacterCount()) ?
aOffset : aOffset + 1;
return TextBounds(aOffset, endOffset, aCoordType);
}
/**
* Get/set caret offset, if no caret then -1.
@ -421,7 +425,7 @@ protected:
/**
* Transform magic offset into text offset.
*/
uint32_t ConvertMagicOffset(int32_t aOffset) const;
index_t ConvertMagicOffset(int32_t aOffset) const;
/**
* Adjust an offset the caret stays at to get a text by line boundary.