From 5bdd0a03d1b80fcc783cb0f2c22bbfcd8b491c20 Mon Sep 17 00:00:00 2001 From: Michael Ow Date: Mon, 14 Jan 2008 23:25:13 +0000 Subject: [PATCH] ICU-6132 Check null pointer allocation and perform appropriate actions in regexst.cpp, remtrans.cpp, and repattrn.cpp. X-SVN-Rev: 23235 --- icu4c/source/i18n/regexst.cpp | 59 +++++++++++++++++++++++++++++++++- icu4c/source/i18n/remtrans.cpp | 4 +-- icu4c/source/i18n/repattrn.cpp | 12 +++++-- 3 files changed, 70 insertions(+), 5 deletions(-) diff --git a/icu4c/source/i18n/regexst.cpp b/icu4c/source/i18n/regexst.cpp index d624766becd..cbc413d863c 100644 --- a/icu4c/source/i18n/regexst.cpp +++ b/icu4c/source/i18n/regexst.cpp @@ -1,7 +1,7 @@ // // regexst.h // -// Copyright (C) 2004-2007, International Business Machines Corporation and others. +// Copyright (C) 2004-2008, International Business Machines Corporation and others. // All Rights Reserved. // // This file contains class RegexStaticSets @@ -167,6 +167,20 @@ fRuleDigitsAlias(NULL) fPropSets[URX_GC_T] = new UnicodeSet(UnicodeString(TRUE, gGC_TPattern, -1), *status); fPropSets[URX_GC_LV] = new UnicodeSet(UnicodeString(TRUE, gGC_LVPattern, -1), *status); fPropSets[URX_GC_LVT] = new UnicodeSet(UnicodeString(TRUE, gGC_LVTPattern, -1), *status); + + // Check for null pointers + if (fPropSets[URX_ISWORD_SET] == NULL || + fPropSets[URX_ISSPACE_SET] == NULL || + fPropSets[URX_GC_EXTEND] == NULL || + fPropSets[URX_GC_CONTROL] == NULL || + fPropSets[URX_GC_L] == NULL || + fPropSets[URX_GC_V] == NULL || + fPropSets[URX_GC_T] == NULL || + fPropSets[URX_GC_LV] == NULL || + fPropSets[URX_GC_LVT] == NULL ) { + goto ExitConstrDeleteAll; + + } if (U_FAILURE(*status)) { // Bail out if we were unable to create the above sets. // The rest of the initialization needs them, so we cannot proceed. @@ -185,6 +199,10 @@ fRuleDigitsAlias(NULL) // when finding grapheme cluster boundaries. // fPropSets[URX_GC_NORMAL] = new UnicodeSet(0, UnicodeSet::MAX_VALUE); + // Null pointer check + if (fPropSets[URX_GC_NORMAL] == NULL) { + goto ExitConstrDeleteAll; + } fPropSets[URX_GC_NORMAL]->remove(0xac00, 0xd7a4); fPropSets[URX_GC_NORMAL]->removeAll(*fPropSets[URX_GC_CONTROL]); fPropSets[URX_GC_NORMAL]->removeAll(*fPropSets[URX_GC_L]); @@ -203,12 +221,47 @@ fRuleDigitsAlias(NULL) // Sets used while parsing rules, but not referenced from the parse state table fRuleSets[kRuleSet_rule_char-128] = new UnicodeSet(UnicodeString(TRUE, gRuleSet_rule_char_pattern, -1), *status); fRuleSets[kRuleSet_digit_char-128] = new UnicodeSet(UnicodeString(TRUE, gRuleSet_digit_char_pattern, -1), *status); + //Check for null pointers + if (fRuleSets[kRuleSet_rule_char-128] == NULL || + fRuleSets[kRuleSet_digit_char-128] == NULL) { + goto ExitConstrDeleteAll; + } fRuleDigitsAlias = fRuleSets[kRuleSet_digit_char-128]; for (i=0; i<(int32_t)(sizeof(fRuleSets)/sizeof(fRuleSets[0])); i++) { if (fRuleSets[i]) { fRuleSets[i]->compact(); } } + return; // If we reached this point, everything is fine so just exit + +ExitConstrDeleteAll: // Remove fPropSets and fRuleSets and return error + delete fPropSets[URX_ISWORD_SET]; + delete fPropSets[URX_ISSPACE_SET]; + delete fPropSets[URX_GC_EXTEND]; + delete fPropSets[URX_GC_CONTROL]; + delete fPropSets[URX_GC_L]; + delete fPropSets[URX_GC_V]; + delete fPropSets[URX_GC_T]; + delete fPropSets[URX_GC_LV]; + delete fPropSets[URX_GC_LVT]; + delete fPropSets[URX_GC_NORMAL]; + fPropSets[URX_ISWORD_SET] = NULL; + fPropSets[URX_ISSPACE_SET] = NULL; + fPropSets[URX_GC_EXTEND] = NULL; + fPropSets[URX_GC_CONTROL] = NULL; + fPropSets[URX_GC_L] = NULL; + fPropSets[URX_GC_V] = NULL; + fPropSets[URX_GC_T] = NULL; + fPropSets[URX_GC_LV] = NULL; + fPropSets[URX_GC_LVT] = NULL; + fPropSets[URX_GC_NORMAL] = NULL; + + delete fRuleSets[kRuleSet_rule_char-128]; + delete fRuleSets[kRuleSet_digit_char-128]; + fRuleSets[kRuleSet_rule_char-128] = NULL; + fRuleSets[kRuleSet_digit_char-128] = NULL; + + *status = U_MEMORY_ALLOCATION_ERROR; } @@ -252,6 +305,10 @@ void RegexStaticSets::initGlobals(UErrorCode *status) { UMTX_CHECK(NULL, gStaticSets, p); if (p == NULL) { p = new RegexStaticSets(status); + if (p == NULL) { + *status = U_MEMORY_ALLOCATION_ERROR; + return; + } if (U_FAILURE(*status)) { delete p; return; diff --git a/icu4c/source/i18n/remtrans.cpp b/icu4c/source/i18n/remtrans.cpp index d4e616e1dfc..c21c79b4f94 100644 --- a/icu4c/source/i18n/remtrans.cpp +++ b/icu4c/source/i18n/remtrans.cpp @@ -1,6 +1,6 @@ /* ********************************************************************** -* Copyright (c) 2001-2005, International Business Machines +* Copyright (c) 2001-2008, International Business Machines * Corporation and others. All Rights Reserved. ********************************************************************** * Date Name Description @@ -47,7 +47,7 @@ RemoveTransliterator::~RemoveTransliterator() {} Transliterator* RemoveTransliterator::clone(void) const { Transliterator* result = new RemoveTransliterator(); - if (getFilter() != 0) { + if (result != NULL && getFilter() != 0) { result->adoptFilter((UnicodeFilter*)(getFilter()->clone())); } return result; diff --git a/icu4c/source/i18n/repattrn.cpp b/icu4c/source/i18n/repattrn.cpp index 51e406b4cbc..b8c0120f51f 100644 --- a/icu4c/source/i18n/repattrn.cpp +++ b/icu4c/source/i18n/repattrn.cpp @@ -3,7 +3,7 @@ // /* *************************************************************************** -* Copyright (C) 2002-2007 International Business Machines Corporation * +* Copyright (C) 2002-2008 International Business Machines Corporation * * and others. All rights reserved. * *************************************************************************** */ @@ -99,6 +99,10 @@ RegexPattern &RegexPattern::operator = (const RegexPattern &other) { int32_t i; int32_t numSets = other.fSets->size(); fSets8 = new Regex8BitSet[numSets]; + if (fSets8 == NULL) { + fDeferredStatus = U_MEMORY_ALLOCATION_ERROR; + return *this; + } for (i=1; i