Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(80)

Side by Side Diff: base/observer_list_unittest.cc

Issue 7127001: Fix user-after-free error with ObserverList. The problem is that if an ObserverListBase::Iterato... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/observer_list.h" 5 #include "base/observer_list.h"
6 #include "base/observer_list_threadsafe.h" 6 #include "base/observer_list_threadsafe.h"
7 7
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/memory/ref_counted.h" 10 #include "base/memory/ref_counted.h"
(...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after
415 AddInClearObserve a(&observer_list); 415 AddInClearObserve a(&observer_list);
416 416
417 observer_list.AddObserver(&a); 417 observer_list.AddObserver(&a);
418 418
419 FOR_EACH_OBSERVER(Foo, observer_list, Observe(1)); 419 FOR_EACH_OBSERVER(Foo, observer_list, Observe(1));
420 EXPECT_TRUE(a.added()); 420 EXPECT_TRUE(a.added());
421 EXPECT_EQ(0, a.adder().total) 421 EXPECT_EQ(0, a.adder().total)
422 << "Adder should not observe, so sum should still be 0."; 422 << "Adder should not observe, so sum should still be 0.";
423 } 423 }
424 424
425 class ListDestructor : public Foo {
426 public:
427 explicit ListDestructor(ObserverList<Foo>* list) : list_(list) {}
428 virtual void Observe(int x) {
429 delete list_;
430 }
431 virtual ~ListDestructor() { }
432 int total;
433 private:
434 ObserverList<Foo>* list_;
435 };
436
437
438 TEST(ObserverListTest, IteratorOutlivesList) {
439 ObserverList<Foo>* observer_list = new ObserverList<Foo>;
440 ListDestructor a(observer_list);
441 observer_list->AddObserver(&a);
442
443 FOR_EACH_OBSERVER(Foo, *observer_list, Observe(0));
444 // If this test fails, there'll be Valgrind errors when this function goes out
445 // of scope.
446 }
447
425 } // namespace 448 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698