From e2000f571ffe13fc8fd385226ff185bb9a720e0d Mon Sep 17 00:00:00 2001 From: Jan Steemann Date: Thu, 16 Apr 2015 15:04:11 +0200 Subject: [PATCH] try to satisfy scan-build --- arangod/Aql/OptimizerRules.cpp | 4 +- arangod/FulltextIndex/fulltext-handles.cpp | 6 +- arangod/V8Server/v8-collection.cpp | 2 +- arangod/V8Server/v8-vocbase.cpp | 12 +- lib/ShapedJson/shaped-json.cpp | 223 +++++++-------------- 5 files changed, 79 insertions(+), 168 deletions(-) diff --git a/arangod/Aql/OptimizerRules.cpp b/arangod/Aql/OptimizerRules.cpp index 770698c906..960175a51d 100644 --- a/arangod/Aql/OptimizerRules.cpp +++ b/arangod/Aql/OptimizerRules.cpp @@ -3131,7 +3131,6 @@ int triagens::aql::interchangeAdjacentEnumerationsRule (Optimizer* opt, if (! starts.empty()) { nextPermutationTuple(permTuple, starts); // will never return false - bool ok = true; do { // Clone the plan: @@ -3173,7 +3172,6 @@ int triagens::aql::interchangeAdjacentEnumerationsRule (Optimizer* opt, // OK, the new plan is ready, let's report it: if (! opt->addPlan(newPlan, rule->level, true)) { // have enough plans. stop permutations - ok = false; break; } } @@ -3183,7 +3181,7 @@ int triagens::aql::interchangeAdjacentEnumerationsRule (Optimizer* opt, } } - while (ok && nextPermutationTuple(permTuple, starts)); + while (nextPermutationTuple(permTuple, starts)); } return TRI_ERROR_NO_ERROR; diff --git a/arangod/FulltextIndex/fulltext-handles.cpp b/arangod/FulltextIndex/fulltext-handles.cpp index 521b45b67a..dab8e2a1ca 100644 --- a/arangod/FulltextIndex/fulltext-handles.cpp +++ b/arangod/FulltextIndex/fulltext-handles.cpp @@ -60,11 +60,15 @@ static void FreeSlot (TRI_fulltext_handle_slot_t* slot) { static bool AllocateSlot (TRI_fulltext_handles_t* const handles, const uint32_t slotNumber) { + if (handles == nullptr) { + return false; + } + if (handles->_slots[slotNumber] != nullptr) { return true; } - TRI_fulltext_handle_slot_t* slot = static_cast(TRI_Allocate(TRI_UNKNOWN_MEM_ZONE, sizeof(TRI_fulltext_handle_slot_t), false)); + auto slot = static_cast(TRI_Allocate(TRI_UNKNOWN_MEM_ZONE, sizeof(TRI_fulltext_handle_slot_t), false)); if (slot == nullptr) { return false; diff --git a/arangod/V8Server/v8-collection.cpp b/arangod/V8Server/v8-collection.cpp index 6840aff61a..744a32420c 100644 --- a/arangod/V8Server/v8-collection.cpp +++ b/arangod/V8Server/v8-collection.cpp @@ -1687,7 +1687,7 @@ static TRI_doc_collection_info_t* GetFigures (TRI_vocbase_col_t* collection) { TRI_document_collection_t* document = collection->_collection; TRI_doc_collection_info_t* info = document->figures(document); - res = trx.finish(res); + trx.finish(res); // READ-LOCK end return info; diff --git a/arangod/V8Server/v8-vocbase.cpp b/arangod/V8Server/v8-vocbase.cpp index 06b8c29dd6..a3bd681946 100644 --- a/arangod/V8Server/v8-vocbase.cpp +++ b/arangod/V8Server/v8-vocbase.cpp @@ -607,20 +607,10 @@ static void JS_Debug (const v8::FunctionCallbackInfo& args) { if (console != nullptr) { MUTEX_LOCKER(triagens::arango::serverConsoleMutex); if (serverConsole.load() != nullptr) { - // Check again if console was withdrawn - bool userAborted = false; - while (! userAborted) { + while (true) { char* input = console->prompt("debug> "); - if (userAborted) { - if (input != nullptr) { - TRI_FreeString(TRI_UNKNOWN_MEM_ZONE, input); - } - break; - } - if (input == nullptr) { - userAborted = true; break; } diff --git a/lib/ShapedJson/shaped-json.cpp b/lib/ShapedJson/shaped-json.cpp index ad44a98513..1954d59b89 100644 --- a/lib/ShapedJson/shaped-json.cpp +++ b/lib/ShapedJson/shaped-json.cpp @@ -463,31 +463,19 @@ static bool FillShapeValueList (TRI_shaper_t* shaper, TRI_json_t const* json, size_t level, bool create) { - size_t i, n; - uint64_t total; - - TRI_shape_value_t* values; - TRI_shape_value_t* p; - TRI_shape_value_t* e; - - bool hs; - bool hl; - TRI_shape_sid_t s; TRI_shape_sid_t l; TRI_shape_size_t* offsets; TRI_shape_size_t offset; - TRI_shape_t const* found; - char* ptr; // sanity checks TRI_ASSERT(json->_type == TRI_JSON_ARRAY); // check for special case "empty list" - n = json->_value._objects._length; + size_t const n = json->_value._objects._length; if (n == 0) { dst->_type = TRI_SHAPE_LIST; @@ -504,30 +492,39 @@ static bool FillShapeValueList (TRI_shaper_t* shaper, return true; } + + // helper function to free shape values + auto freeShapeValues = [] (TRI_memory_zone_t* zone, TRI_shape_value_t* values, TRI_shape_value_t* end) { + TRI_shape_value_t* p = values; + TRI_shape_value_t* e = end; + + for (; p < e; ++p) { + if (p->_value != nullptr) { + TRI_Free(zone, p->_value); + } + } + + TRI_Free(zone, values); + }; + // convert into TRI_shape_value_t array - p = (values = static_cast(TRI_Allocate(shaper->_memoryZone, sizeof(TRI_shape_value_t) * n, true))); + TRI_shape_value_t* values = static_cast(TRI_Allocate(shaper->_memoryZone, sizeof(TRI_shape_value_t) * n, true)); - if (p == nullptr) { + if (values == nullptr) { return false; } - total = 0; - e = values + n; + uint64_t total = 0; - for (i = 0; i < n; ++i, ++p) { + TRI_shape_value_t* p = values; + + for (size_t i = 0; i < n; ++i, ++p) { TRI_json_t const* el = static_cast(TRI_AtVector(&json->_value._objects, i)); bool ok = FillShapeValueJson(shaper, p, el, level + 1, create); if (! ok) { - for (e = p, p = values; p < e; ++p) { - if (p->_value != nullptr) { - TRI_Free(shaper->_memoryZone, p->_value); - } - } - - TRI_Free(shaper->_memoryZone, values); - + freeShapeValues(shaper->_memoryZone, values, p); return false; } @@ -535,12 +532,14 @@ static bool FillShapeValueList (TRI_shaper_t* shaper, } // check if this list is homogeneous - hs = true; - hl = true; + bool hs = true; + bool hl = true; s = values[0]._sid; l = values[0]._size; + p = values; + TRI_shape_value_t* const e = values + n; // end does not change for (; p < e; ++p) { if (p->_sid != s) { @@ -558,14 +557,7 @@ static bool FillShapeValueList (TRI_shaper_t* shaper, TRI_homogeneous_sized_list_shape_t* shape = static_cast(TRI_Allocate(shaper->_memoryZone, sizeof(TRI_homogeneous_sized_list_shape_t), true)); if (shape == nullptr) { - for (p = values; p < e; ++p) { - if (p->_value != nullptr) { - TRI_Free(shaper->_memoryZone, p->_value); - } - } - - TRI_Free(shaper->_memoryZone, values); - + freeShapeValues(shaper->_memoryZone, values, e); return false; } @@ -576,18 +568,11 @@ static bool FillShapeValueList (TRI_shaper_t* shaper, shape->_sizeEntry = l; // note: if 'found' is not a nullptr, the shaper will have freed variable 'shape'! - found = shaper->findShape(shaper, &shape->base, create); + TRI_shape_t const* found = shaper->findShape(shaper, &shape->base, create); if (found == nullptr) { - for (p = values; p < e; ++p) { - if (p->_value != nullptr) { - TRI_Free(shaper->_memoryZone, p->_value); - } - } - - TRI_Free(shaper->_memoryZone, values); + freeShapeValues(shaper->_memoryZone, values, e); TRI_Free(shaper->_memoryZone, shape); - return false; } @@ -601,14 +586,7 @@ static bool FillShapeValueList (TRI_shaper_t* shaper, dst->_value = (ptr = static_cast(TRI_Allocate(shaper->_memoryZone, dst->_size, true))); if (dst->_value == nullptr) { - for (p = values; p < e; ++p) { - if (p->_value != nullptr) { - TRI_Free(shaper->_memoryZone, p->_value); - } - } - - TRI_Free(shaper->_memoryZone, values); - + freeShapeValues(shaper->_memoryZone, values, e); return false; } @@ -627,14 +605,7 @@ static bool FillShapeValueList (TRI_shaper_t* shaper, TRI_homogeneous_list_shape_t* shape = static_cast(TRI_Allocate(shaper->_memoryZone, sizeof(TRI_homogeneous_list_shape_t), true)); if (shape == nullptr) { - for (p = values; p < e; ++p) { - if (p->_value != nullptr) { - TRI_Free(shaper->_memoryZone, p->_value); - } - } - - TRI_Free(shaper->_memoryZone, values); - + freeShapeValues(shaper->_memoryZone, values, e); return false; } @@ -644,18 +615,11 @@ static bool FillShapeValueList (TRI_shaper_t* shaper, shape->_sidEntry = s; // note: if 'found' is not a nullptr, the shaper will have freed variable 'shape'! - found = shaper->findShape(shaper, &shape->base, create); + TRI_shape_t const* found = shaper->findShape(shaper, &shape->base, create); if (found == nullptr) { - for (p = values; p < e; ++p) { - if (p->_value != nullptr) { - TRI_Free(shaper->_memoryZone, p->_value); - } - } - - TRI_Free(shaper->_memoryZone, values); + freeShapeValues(shaper->_memoryZone, values, e); TRI_Free(shaper->_memoryZone, shape); - return false; } @@ -671,14 +635,7 @@ static bool FillShapeValueList (TRI_shaper_t* shaper, dst->_value = (ptr = static_cast(TRI_Allocate(shaper->_memoryZone, dst->_size, true))); if (dst->_value == nullptr) { - for (p = values; p < e; ++p) { - if (p->_value != nullptr) { - TRI_Free(shaper->_memoryZone, p->_value); - } - } - - TRI_Free(shaper->_memoryZone, values); - + freeShapeValues(shaper->_memoryZone, values, e); return false; } @@ -715,13 +672,7 @@ static bool FillShapeValueList (TRI_shaper_t* shaper, dst->_value = (ptr = static_cast(TRI_Allocate(shaper->_memoryZone, dst->_size, true))); if (dst->_value == nullptr) { - for (p = values; p < e; ++p) { - if (p->_value != nullptr) { - TRI_Free(shaper->_memoryZone, p->_value); - } - } - - TRI_Free(shaper->_memoryZone, values); + freeShapeValues(shaper->_memoryZone, values, e); return false; } @@ -749,13 +700,7 @@ static bool FillShapeValueList (TRI_shaper_t* shaper, } // free TRI_shape_value_t array - for (p = values; p < e; ++p) { - if (p->_value != nullptr) { - TRI_Free(shaper->_memoryZone, p->_value); - } - } - - TRI_Free(shaper->_memoryZone, values); + freeShapeValues(shaper->_memoryZone, values, e); return true; } @@ -768,26 +713,12 @@ static bool FillShapeValueArray (TRI_shaper_t* shaper, TRI_json_t const* json, size_t level, bool create) { - size_t i, n; - uint64_t total; - - size_t f; - size_t v; - - TRI_shape_value_t* values; - TRI_shape_value_t* p; - TRI_shape_value_t* e; - - TRI_array_shape_t* a; - TRI_shape_sid_t* sids; TRI_shape_aid_t* aids; TRI_shape_size_t* offsetsF; TRI_shape_size_t* offsetsV; TRI_shape_size_t offset; - TRI_shape_t const* found; - char* ptr; // sanity checks @@ -795,20 +726,38 @@ static bool FillShapeValueArray (TRI_shaper_t* shaper, TRI_ASSERT(json->_value._objects._length % 2 == 0); // number of attributes - n = json->_value._objects._length / 2; + size_t n = json->_value._objects._length / 2; // convert into TRI_shape_value_t array - p = (values = static_cast(TRI_Allocate(shaper->_memoryZone, n * sizeof(TRI_shape_value_t), true))); + TRI_shape_value_t* values = static_cast(TRI_Allocate(shaper->_memoryZone, n * sizeof(TRI_shape_value_t), true)); - if (p == nullptr) { + if (values == nullptr) { return false; } + + + // helper function to free shape values + auto freeShapeValues = [] (TRI_memory_zone_t* zone, TRI_shape_value_t* values, TRI_shape_value_t* end) { + TRI_shape_value_t* p = values; + TRI_shape_value_t* e = end; - total = 0; - f = 0; - v = 0; + for (; p < e; ++p) { + if (p->_value != nullptr) { + TRI_Free(zone, p->_value); + } + } - for (i = 0; i < n; ++i, ++p) { + TRI_Free(zone, values); + }; + + + uint64_t total = 0; + size_t f = 0; + size_t v = 0; + + TRI_shape_value_t* p = values; + + for (size_t i = 0; i < n; ++i, ++p) { TRI_json_t const* key = static_cast(TRI_AtVector(&json->_value._objects, 2 * i)); TRI_ASSERT(key != nullptr); TRI_ASSERT(key->_type == TRI_JSON_STRING); @@ -851,13 +800,7 @@ static bool FillShapeValueArray (TRI_shaper_t* shaper, } if (! ok) { - for (e = p, p = values; p < e; ++p) { - if (p->_value != nullptr) { - TRI_Free(shaper->_memoryZone, p->_value); - } - } - - TRI_Free(shaper->_memoryZone, values); + freeShapeValues(shaper->_memoryZone, values, p); return false; } @@ -893,30 +836,21 @@ static bool FillShapeValueArray (TRI_shaper_t* shaper, #endif // generate shape structure - i = + size_t byteSize = sizeof(TRI_array_shape_t) + n * sizeof(TRI_shape_sid_t) + n * sizeof(TRI_shape_aid_t) + (f + 1) * sizeof(TRI_shape_size_t); - a = reinterpret_cast(ptr = static_cast(TRI_Allocate(shaper->_memoryZone, i, true))); + TRI_array_shape_t* a = reinterpret_cast(ptr = static_cast(TRI_Allocate(shaper->_memoryZone, byteSize, true))); if (ptr == nullptr) { - e = values + n; - - for (p = values; p < e; ++p) { - if (p->_value != nullptr) { - TRI_Free(shaper->_memoryZone, p->_value); - } - } - - TRI_Free(shaper->_memoryZone, values); - + freeShapeValues(shaper->_memoryZone, values, values + n); return false; } a->base._type = TRI_SHAPE_ARRAY; - a->base._size = i; + a->base._size = byteSize; a->base._dataSize = (v == 0) ? total : TRI_SHAPE_SIZE_VARIABLE; a->_fixedEntries = f; @@ -944,17 +878,8 @@ static bool FillShapeValueArray (TRI_shaper_t* shaper, dst->_value = (ptr = static_cast(TRI_Allocate(shaper->_memoryZone, dst->_size, true))); if (ptr == nullptr) { - e = values + n; - - for (p = values; p < e; ++p) { - if (p->_value != nullptr) { - TRI_Free(shaper->_memoryZone, p->_value); - } - } - - TRI_Free(shaper->_memoryZone, values); + freeShapeValues(shaper->_memoryZone, values, values + n); TRI_Free(shaper->_memoryZone, a); - return false; } @@ -963,7 +888,7 @@ static bool FillShapeValueArray (TRI_shaper_t* shaper, ptr += (v + 1) * sizeof(TRI_shape_size_t); // and fill in attributes - e = values + n; + TRI_shape_value_t* const e = values + n; for (p = values; p < e; ++p) { *aids++ = p->_aid; @@ -987,16 +912,10 @@ static bool FillShapeValueArray (TRI_shaper_t* shaper, } // free TRI_shape_value_t array - for (p = values; p < e; ++p) { - if (p->_value != nullptr) { - TRI_Free(shaper->_memoryZone, p->_value); - } - } - - TRI_Free(shaper->_memoryZone, values); + freeShapeValues(shaper->_memoryZone, values, e); // lookup this shape - found = shaper->findShape(shaper, &a->base, create); + TRI_shape_t const* found = shaper->findShape(shaper, &a->base, create); if (found == nullptr) { TRI_Free(shaper->_memoryZone, dst->_value);