ICU-6132 Add check for memory allocation error in constructors and throughout search.cpp.

X-SVN-Rev: 23239
This commit is contained in:
Michael Ow 2008-01-15 23:43:10 +00:00
parent 2363fc3235
commit d7afd7c71d

View file

@ -1,6 +1,6 @@
/*
**********************************************************************
* Copyright (C) 2001-2005 IBM and others. All rights reserved.
* Copyright (C) 2001-2008 IBM and others. All rights reserved.
**********************************************************************
* Date Name Description
* 03/22/2000 helena Creation.
@ -25,14 +25,17 @@ SearchIterator::SearchIterator(const SearchIterator &other)
{
m_breakiterator_ = other.m_breakiterator_;
m_text_ = other.m_text_;
m_search_ = (USearch *)uprv_malloc(sizeof(USearch));
m_search_->breakIter = other.m_search_->breakIter;
m_search_->isCanonicalMatch = other.m_search_->isCanonicalMatch;
m_search_->isOverlap = other.m_search_->isOverlap;
m_search_->matchedIndex = other.m_search_->matchedIndex;
m_search_->matchedLength = other.m_search_->matchedLength;
m_search_->text = other.m_search_->text;
m_search_->textLength = other.m_search_->textLength;
m_search_ = (USearch *)uprv_malloc(sizeof(USearch));
// Null pointer check.
if (m_search_ != NULL) {
m_search_->breakIter = other.m_search_->breakIter;
m_search_->isCanonicalMatch = other.m_search_->isCanonicalMatch;
m_search_->isOverlap = other.m_search_->isOverlap;
m_search_->matchedIndex = other.m_search_->matchedIndex;
m_search_->matchedLength = other.m_search_->matchedLength;
m_search_->text = other.m_search_->text;
m_search_->textLength = other.m_search_->textLength;
}
}
SearchIterator::~SearchIterator()
@ -48,6 +51,11 @@ void SearchIterator::setAttribute(USearchAttribute attribute,
USearchAttributeValue value,
UErrorCode &status)
{
// This is due to memory allocation error in the constructor.
if (m_search_ == NULL) {
status = U_MEMORY_ALLOCATION_ERROR;
return;
}
if (U_SUCCESS(status)) {
switch (attribute)
{
@ -69,6 +77,10 @@ void SearchIterator::setAttribute(USearchAttribute attribute,
USearchAttributeValue SearchIterator::getAttribute(
USearchAttribute attribute) const
{
// This is due to memory allocation error in the constructor.
if (m_search_ == NULL) {
return USEARCH_DEFAULT;
}
switch (attribute) {
case USEARCH_OVERLAP :
return (m_search_->isOverlap == TRUE ? USEARCH_ON : USEARCH_OFF);
@ -82,16 +94,28 @@ USearchAttributeValue SearchIterator::getAttribute(
int32_t SearchIterator::getMatchedStart() const
{
// This is due to memory allocation error in the constructor.
if (m_search_ == NULL) {
return USEARCH_DONE;
}
return m_search_->matchedIndex;
}
int32_t SearchIterator::getMatchedLength() const
{
// This is due to memory allocation error in the constructor.
if (m_search_ == NULL) {
return USEARCH_DONE;
}
return m_search_->matchedLength;
}
void SearchIterator::getMatchedText(UnicodeString &result) const
{
// This is due to memory allocation error in the constructor.
if (m_search_ == NULL) {
return;
}
int32_t matchedindex = m_search_->matchedIndex;
int32_t matchedlength = m_search_->matchedLength;
if (matchedindex != USEARCH_DONE && matchedlength != 0) {
@ -105,6 +129,11 @@ void SearchIterator::getMatchedText(UnicodeString &result) const
void SearchIterator::setBreakIterator(BreakIterator *breakiter,
UErrorCode &status)
{
// This is due to memory allocation error in the constructor.
if (m_search_ == NULL) {
status = U_MEMORY_ALLOCATION_ERROR;
return;
}
if (U_SUCCESS(status)) {
m_search_->breakIter = NULL;
// the c++ breakiterator may not make use of ubreakiterator.
@ -120,6 +149,11 @@ const BreakIterator * SearchIterator::getBreakIterator(void) const
void SearchIterator::setText(const UnicodeString &text, UErrorCode &status)
{
// This is due to memory allocation error in the constructor.
if (m_search_ == NULL) {
status = U_MEMORY_ALLOCATION_ERROR;
return;
}
if (U_SUCCESS(status)) {
if (text.length() == 0) {
status = U_ILLEGAL_ARGUMENT_ERROR;
@ -152,6 +186,10 @@ UBool SearchIterator::operator==(const SearchIterator &that) const
if (this == &that) {
return TRUE;
}
// This is due to memory allocation error in the constructor.
if (m_search_ == NULL) {
return FALSE;
}
return (m_breakiterator_ == that.m_breakiterator_ &&
m_search_->isCanonicalMatch == that.m_search_->isCanonicalMatch &&
m_search_->isOverlap == that.m_search_->isOverlap &&
@ -186,7 +224,11 @@ int32_t SearchIterator::following(int32_t position,
int32_t SearchIterator::last(UErrorCode &status)
{
if (U_FAILURE(status)) {
// This is due to memory allocation error in the constructor.
if (m_search_ == NULL) {
status = U_MEMORY_ALLOCATION_ERROR;
}
if (U_FAILURE(status)) {
return USEARCH_DONE;
}
setOffset(m_search_->textLength, status);
@ -205,6 +247,10 @@ int32_t SearchIterator::preceding(int32_t position,
int32_t SearchIterator::next(UErrorCode &status)
{
// This is due to memory allocation error in the constructor.
if (m_search_ == NULL) {
status = U_MEMORY_ALLOCATION_ERROR;
}
if (U_SUCCESS(status)) {
int32_t offset = getOffset();
int32_t matchindex = m_search_->matchedIndex;
@ -250,6 +296,10 @@ int32_t SearchIterator::next(UErrorCode &status)
int32_t SearchIterator::previous(UErrorCode &status)
{
// This is due to memory allocation error in the constructor.
if (m_search_ == NULL) {
status = U_MEMORY_ALLOCATION_ERROR;
}
if (U_SUCCESS(status)) {
int32_t offset;
if (m_search_->reset) {
@ -292,6 +342,10 @@ int32_t SearchIterator::previous(UErrorCode &status)
void SearchIterator::reset()
{
// This is due to memory allocation error in the constructor.
if (m_search_ == NULL) {
return;
}
UErrorCode status = U_ZERO_ERROR;
setMatchNotFound();
setOffset(0, status);
@ -306,16 +360,19 @@ void SearchIterator::reset()
SearchIterator::SearchIterator()
{
m_search_ = (USearch *)uprv_malloc(sizeof(USearch));
m_search_->breakIter = NULL;
m_search_->isOverlap = FALSE;
m_search_->isCanonicalMatch = FALSE;
m_search_->isForwardSearching = TRUE;
m_search_->reset = TRUE;
m_search_->matchedIndex = USEARCH_DONE;
m_search_->matchedLength = 0;
m_search_->text = NULL;
m_search_->textLength = 0;
m_breakiterator_ = NULL;
// Null pointer check
if (m_search_ != NULL) {
m_search_->breakIter = NULL;
m_search_->isOverlap = FALSE;
m_search_->isCanonicalMatch = FALSE;
m_search_->isForwardSearching = TRUE;
m_search_->reset = TRUE;
m_search_->matchedIndex = USEARCH_DONE;
m_search_->matchedLength = 0;
m_search_->text = NULL;
m_search_->textLength = 0;
m_breakiterator_ = NULL;
}
}
SearchIterator::SearchIterator(const UnicodeString &text,
@ -324,15 +381,18 @@ SearchIterator::SearchIterator(const UnicodeString &text,
m_text_(text)
{
m_search_ = (USearch *)uprv_malloc(sizeof(USearch));
m_search_->breakIter = NULL;
m_search_->isOverlap = FALSE;
m_search_->isCanonicalMatch = FALSE;
m_search_->isForwardSearching = TRUE;
m_search_->reset = TRUE;
m_search_->matchedIndex = USEARCH_DONE;
m_search_->matchedLength = 0;
m_search_->text = m_text_.getBuffer();
m_search_->textLength = text.length();
// Null pointer check
if (m_search_ != NULL) {
m_search_->breakIter = NULL;
m_search_->isOverlap = FALSE;
m_search_->isCanonicalMatch = FALSE;
m_search_->isForwardSearching = TRUE;
m_search_->reset = TRUE;
m_search_->matchedIndex = USEARCH_DONE;
m_search_->matchedLength = 0;
m_search_->text = m_text_.getBuffer();
m_search_->textLength = text.length();
}
}
SearchIterator::SearchIterator(CharacterIterator &text,
@ -340,17 +400,20 @@ SearchIterator::SearchIterator(CharacterIterator &text,
m_breakiterator_(breakiter)
{
m_search_ = (USearch *)uprv_malloc(sizeof(USearch));
m_search_->breakIter = NULL;
m_search_->isOverlap = FALSE;
m_search_->isCanonicalMatch = FALSE;
m_search_->isForwardSearching = TRUE;
m_search_->reset = TRUE;
m_search_->matchedIndex = USEARCH_DONE;
m_search_->matchedLength = 0;
text.getText(m_text_);
m_search_->text = m_text_.getBuffer();
m_search_->textLength = m_text_.length();
m_breakiterator_ = breakiter;
// Null pointer check.
if (m_search_ != NULL) {
m_search_->breakIter = NULL;
m_search_->isOverlap = FALSE;
m_search_->isCanonicalMatch = FALSE;
m_search_->isForwardSearching = TRUE;
m_search_->reset = TRUE;
m_search_->matchedIndex = USEARCH_DONE;
m_search_->matchedLength = 0;
text.getText(m_text_);
m_search_->text = m_text_.getBuffer();
m_search_->textLength = m_text_.length();
m_breakiterator_ = breakiter;
}
}
// protected methods ------------------------------------------------------
@ -373,11 +436,19 @@ SearchIterator & SearchIterator::operator=(const SearchIterator &that)
void SearchIterator::setMatchLength(int32_t length)
{
// This is due to memory allocation error in the constructor.
if (m_search_ == NULL) {
return;
}
m_search_->matchedLength = length;
}
void SearchIterator::setMatchStart(int32_t position)
{
// This is due to memory allocation error in the constructor.
if (m_search_ == NULL) {
return;
}
m_search_->matchedIndex = position;
}
@ -386,6 +457,10 @@ void SearchIterator::setMatchNotFound()
setMatchStart(USEARCH_DONE);
setMatchLength(0);
UErrorCode status = U_ZERO_ERROR;
// This is due to memory allocation error in the constructor.
if (m_search_ == NULL) {
return;
}
// by default no errors should be returned here since offsets are within
// range.
if (m_search_->isForwardSearching) {