From 31f5b8b5d2a830c62de3bcb344066f6914abf5b8 Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Wed, 26 Nov 2014 12:14:36 +0100 Subject: [PATCH] BView::BeginLineArray: leave object in consistent state We allow this method to throw bad_alloc exceptions as there is no other way to report errors. However we left the object in a broken state (maxCount set, but array not initialized) which would crash when calling either AddLine or EndLineArray. Initialize the count to 0 before throwing the exception so now EndLineArray can be called and operations resumed safely after an allocation failure. --- src/kits/interface/View.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/kits/interface/View.cpp b/src/kits/interface/View.cpp index a993b3b3a8..3c06a02285 100644 --- a/src/kits/interface/View.cpp +++ b/src/kits/interface/View.cpp @@ -3638,9 +3638,16 @@ BView::BeginLineArray(int32 count) // calls with a NULL fCommArray would drop into the debugger anyway, // we allow the possible std::bad_alloc exceptions here... fCommArray = new _array_data_; - fCommArray->maxCount = count; fCommArray->count = 0; + + // Make sure the fCommArray is initialized to reasonable values in cases of + // bad_alloc. At least the exception can be caught and EndLineArray won't + // crash. + fCommArray->array = NULL; + fCommArray->maxCount = 0; + fCommArray->array = new ViewLineArrayInfo[count]; + fCommArray->maxCount = count; }