From 7b939088a95829cf33b52438abca844d21b11737 Mon Sep 17 00:00:00 2001 From: George Rhoten Date: Thu, 30 Mar 2006 09:12:35 +0000 Subject: [PATCH] ICU-4703 addListener should return when detecting bad arguments. X-SVN-Rev: 19481 --- icu4c/source/common/servnotf.cpp | 102 ++++++++++++++++--------------- 1 file changed, 52 insertions(+), 50 deletions(-) diff --git a/icu4c/source/common/servnotf.cpp b/icu4c/source/common/servnotf.cpp index eb4a90164c6..6adf52ece2e 100644 --- a/icu4c/source/common/servnotf.cpp +++ b/icu4c/source/common/servnotf.cpp @@ -1,6 +1,6 @@ /** ******************************************************************************* - * Copyright (C) 2001-2004, International Business Machines Corporation and * + * Copyright (C) 2001-2006, International Business Machines Corporation and * * others. All Rights Reserved. * ******************************************************************************* */ @@ -20,7 +20,7 @@ EventListener::~EventListener() {} UOBJECT_DEFINE_RTTI_IMPLEMENTATION(EventListener) ICUNotifier::ICUNotifier(void) - : notifyLock(0), listeners(NULL) +: notifyLock(0), listeners(NULL) { umtx_init(¬ifyLock); } @@ -38,75 +38,77 @@ ICUNotifier::~ICUNotifier(void) { void ICUNotifier::addListener(const EventListener* l, UErrorCode& status) { - if (U_SUCCESS(status)) { - if (l == NULL) { - status = U_ILLEGAL_ARGUMENT_ERROR; - } - - if (acceptsListener(*l)) { - Mutex lmx(¬ifyLock); - if (listeners == NULL) { - listeners = new UVector(5, status); - } else { - for (int i = 0, e = listeners->size(); i < e; ++i) { - const EventListener* el = (const EventListener*)(listeners->elementAt(i)); - if (l == el) { + if (U_SUCCESS(status)) { + if (l == NULL) { + status = U_ILLEGAL_ARGUMENT_ERROR; return; - } } - } - listeners->addElement((void*)l, status); // cast away const - } else { + if (acceptsListener(*l)) { + Mutex lmx(¬ifyLock); + if (listeners == NULL) { + listeners = new UVector(5, status); + } else { + for (int i = 0, e = listeners->size(); i < e; ++i) { + const EventListener* el = (const EventListener*)(listeners->elementAt(i)); + if (l == el) { + return; + } + } + } + + listeners->addElement((void*)l, status); // cast away const + } #ifdef NOTIFIER_DEBUG - fprintf(stderr, "Listener invalid for this notifier."); - exit(1); + else { + fprintf(stderr, "Listener invalid for this notifier."); + exit(1); + } #endif } - } } void ICUNotifier::removeListener(const EventListener *l, UErrorCode& status) { - if (U_SUCCESS(status)) { - if (l == NULL) { - status = U_ILLEGAL_ARGUMENT_ERROR; - return; - } - - { - Mutex lmx(¬ifyLock); - if (listeners != NULL) { - // identity equality check - for (int i = 0, e = listeners->size(); i < e; ++i) { - const EventListener* el = (const EventListener*)listeners->elementAt(i); - if (l == el) { - listeners->removeElementAt(i); - if (listeners->size() == 0) { - delete listeners; - listeners = NULL; - } + if (U_SUCCESS(status)) { + if (l == NULL) { + status = U_ILLEGAL_ARGUMENT_ERROR; return; - } } - } + + { + Mutex lmx(¬ifyLock); + if (listeners != NULL) { + // identity equality check + for (int i = 0, e = listeners->size(); i < e; ++i) { + const EventListener* el = (const EventListener*)listeners->elementAt(i); + if (l == el) { + listeners->removeElementAt(i); + if (listeners->size() == 0) { + delete listeners; + listeners = NULL; + } + return; + } + } + } + } } - } } void ICUNotifier::notifyChanged(void) { - if (listeners != NULL) { - Mutex lmx(¬ifyLock); if (listeners != NULL) { - for (int i = 0, e = listeners->size(); i < e; ++i) { - EventListener* el = (EventListener*)listeners->elementAt(i); - notifyListener(*el); - } + Mutex lmx(¬ifyLock); + if (listeners != NULL) { + for (int i = 0, e = listeners->size(); i < e; ++i) { + EventListener* el = (EventListener*)listeners->elementAt(i); + notifyListener(*el); + } + } } - } } U_NAMESPACE_END