ICU-6132 Add null pointer check and memory leak avoidance measures to rbbinode.cpp and rbbirb.cpp.

X-SVN-Rev: 23270
This commit is contained in:
Michael Ow 2008-01-19 00:09:54 +00:00
parent 44ee923138
commit 02df5f103d
2 changed files with 24 additions and 12 deletions

View file

@ -1,6 +1,6 @@
/*
***************************************************************************
* Copyright (C) 2002-2006 International Business Machines Corporation *
* Copyright (C) 2002-2008 International Business Machines Corporation *
* and others. All rights reserved. *
***************************************************************************
*/
@ -149,13 +149,16 @@ RBBINode *RBBINode::cloneTree() {
n = this;
} else {
n = new RBBINode(*this);
if (fLeftChild != NULL) {
n->fLeftChild = fLeftChild->cloneTree();
n->fLeftChild->fParent = n;
}
if (fRightChild != NULL) {
n->fRightChild = fRightChild->cloneTree();
n->fRightChild->fParent = n;
// Check for null pointer.
if (n != NULL) {
if (fLeftChild != NULL) {
n->fLeftChild = fLeftChild->cloneTree();
n->fLeftChild->fParent = n;
}
if (fRightChild != NULL) {
n->fRightChild = fRightChild->cloneTree();
n->fRightChild->fParent = n;
}
}
}
return n;

View file

@ -1,7 +1,7 @@
//
// file: rbbirb.cpp
//
// Copyright (C) 2002-2005, International Business Machines Corporation and others.
// Copyright (C) 2002-2008, International Business Machines Corporation and others.
// All Rights Reserved.
//
// This file contains the RBBIRuleBuilder class implementation. This is the main class for
@ -262,6 +262,14 @@ RBBIRuleBuilder::createRuleBasedBreakIterator( const UnicodeString &rules,
builder.fSafeFwdTables == NULL || builder.fSafeRevTables == NULL))
{
status = U_MEMORY_ALLOCATION_ERROR;
}
// Before building the tables, check to make sure the status is ok.
if (U_FAILURE(status)) {
delete builder.fForwardTables; builder.fForwardTables = NULL;
delete builder.fReverseTables; builder.fReverseTables = NULL;
delete builder.fSafeFwdTables; builder.fSafeFwdTables = NULL;
delete builder.fSafeRevTables; builder.fSafeRevTables = NULL;
return NULL;
}
@ -269,9 +277,6 @@ RBBIRuleBuilder::createRuleBasedBreakIterator( const UnicodeString &rules,
builder.fReverseTables->build();
builder.fSafeFwdTables->build();
builder.fSafeRevTables->build();
if (U_FAILURE(status)) {
return NULL;
}
#ifdef RBBI_DEBUG
if (builder.fDebugEnv && uprv_strstr(builder.fDebugEnv, "states")) {
@ -284,6 +289,10 @@ RBBIRuleBuilder::createRuleBasedBreakIterator( const UnicodeString &rules,
// in the run-time format.
//
RBBIDataHeader *data = builder.flattenData(); // returns NULL if error
if (data == NULL) {
status = U_MEMORY_ALLOCATION_ERROR;
return NULL;
}
//