diff options
author | ADAM David Alan Martin <adam.martin@10gen.com> | 2017-04-07 15:39:08 -0400 |
---|---|---|
committer | ADAM David Alan Martin <adam.martin@10gen.com> | 2017-04-07 15:39:08 -0400 |
commit | b3e59f004f541242e1778efcbbca704c9f174890 (patch) | |
tree | f07b850601c65e5bff1713e7731b7de72839d329 /src/mongo | |
parent | 15102b2ff08880d9ce18585d03b5839afecdd2c4 (diff) | |
download | mongo-b3e59f004f541242e1778efcbbca704c9f174890.tar.gz |
SERVER-28598 Fix ownership in `IndexCatalog`.
There were several temporaries and locals created on the heap that
were not properly managed in exception safe ways.
Diffstat (limited to 'src/mongo')
-rw-r--r-- | src/mongo/db/catalog/index_catalog.cpp | 66 | ||||
-rw-r--r-- | src/mongo/db/catalog/index_catalog.h | 2 |
2 files changed, 34 insertions, 34 deletions
diff --git a/src/mongo/db/catalog/index_catalog.cpp b/src/mongo/db/catalog/index_catalog.cpp index 378dacc1a0c..dab3e9d4cb0 100644 --- a/src/mongo/db/catalog/index_catalog.cpp +++ b/src/mongo/db/catalog/index_catalog.cpp @@ -113,10 +113,11 @@ Status IndexCatalog::init(OperationContext* opCtx) { } BSONObj keyPattern = spec.getObjectField("key"); - IndexDescriptor* descriptor = - new IndexDescriptor(_collection, _getAccessMethodName(opCtx, keyPattern), spec); + auto descriptor = stdx::make_unique<IndexDescriptor>( + _collection, _getAccessMethodName(opCtx, keyPattern), spec); const bool initFromDisk = true; - IndexCatalogEntry* entry = _setupInMemoryStructures(opCtx, descriptor, initFromDisk); + IndexCatalogEntry* entry = + _setupInMemoryStructures(opCtx, std::move(descriptor), initFromDisk); fassert(17340, entry->isReady(opCtx)); } @@ -132,11 +133,8 @@ Status IndexCatalog::init(OperationContext* opCtx) { return Status::OK(); } -IndexCatalogEntry* IndexCatalog::_setupInMemoryStructures(OperationContext* opCtx, - IndexDescriptor* descriptor, - bool initFromDisk) { - unique_ptr<IndexDescriptor> descriptorCleanup(descriptor); - +IndexCatalogEntry* IndexCatalog::_setupInMemoryStructures( + OperationContext* opCtx, std::unique_ptr<IndexDescriptor> descriptor, bool initFromDisk) { Status status = _isSpecOk(opCtx, descriptor->infoObj()); if (!status.isOK() && status != ErrorCodes::IndexAlreadyExists) { severe() << "Found an invalid index " << descriptor->infoObj() << " on the " @@ -144,10 +142,11 @@ IndexCatalogEntry* IndexCatalog::_setupInMemoryStructures(OperationContext* opCt fassertFailedNoTrace(28782); } + auto* const descriptorPtr = descriptor.get(); auto entry = stdx::make_unique<IndexCatalogEntry>(opCtx, _collection->ns().ns(), _collection->getCatalogEntry(), - std::move(descriptorCleanup), + std::move(descriptor), _collection->infoCache()); std::unique_ptr<IndexAccessMethod> accessMethod( _collection->dbce()->getIndex(opCtx, _collection->getCatalogEntry(), entry.get())); @@ -157,7 +156,7 @@ IndexCatalogEntry* IndexCatalog::_setupInMemoryStructures(OperationContext* opCt _entries.add(entry.release()); if (!initFromDisk) { - opCtx->recoveryUnit()->onRollback([this, opCtx, descriptor] { + opCtx->recoveryUnit()->onRollback([ this, opCtx, descriptor = descriptorPtr ] { // Need to preserve indexName as descriptor no longer exists after remove(). const std::string indexName = descriptor->indexName(); _entries.remove(descriptor); @@ -165,8 +164,8 @@ IndexCatalogEntry* IndexCatalog::_setupInMemoryStructures(OperationContext* opCt }); } - invariant(save == _entries.find(descriptor)); - invariant(save == _entries.find(descriptor->indexName())); + invariant(save == _entries.find(descriptorPtr)); + invariant(save == _entries.find(descriptorPtr->indexName())); return save; } @@ -354,7 +353,7 @@ IndexCatalog::IndexBuildBlock::IndexBuildBlock(OperationContext* opCtx, _catalog(collection->getIndexCatalog()), _ns(_catalog->_collection->ns().ns()), _spec(spec.getOwned()), - _entry(NULL), + _entry(nullptr), _opCtx(opCtx) { invariant(collection); } @@ -362,27 +361,27 @@ IndexCatalog::IndexBuildBlock::IndexBuildBlock(OperationContext* opCtx, Status IndexCatalog::IndexBuildBlock::init() { // need this first for names, etc... BSONObj keyPattern = _spec.getObjectField("key"); - IndexDescriptor* descriptor = - new IndexDescriptor(_collection, IndexNames::findPluginName(keyPattern), _spec); - unique_ptr<IndexDescriptor> descriptorCleaner(descriptor); + auto descriptor = stdx::make_unique<IndexDescriptor>( + _collection, IndexNames::findPluginName(keyPattern), _spec); _indexName = descriptor->indexName(); _indexNamespace = descriptor->indexNamespace(); /// ---------- setup on disk structures ---------------- - Status status = _collection->getCatalogEntry()->prepareForIndexBuild(_opCtx, descriptor); + Status status = _collection->getCatalogEntry()->prepareForIndexBuild(_opCtx, descriptor.get()); if (!status.isOK()) return status; + auto* const descriptorPtr = descriptor.get(); /// ---------- setup in memory structures ---------------- const bool initFromDisk = false; - _entry = _catalog->_setupInMemoryStructures(_opCtx, descriptorCleaner.release(), initFromDisk); + _entry = _catalog->_setupInMemoryStructures(_opCtx, std::move(descriptor), initFromDisk); // Register this index with the CollectionInfoCache to regenerate the cache. This way, updates // occurring while an index is being build in the background will be aware of whether or not // they need to modify any indexes. - _collection->infoCache()->addedIndex(_opCtx, descriptor); + _collection->infoCache()->addedIndex(_opCtx, descriptorPtr); return Status::OK(); } @@ -974,7 +973,7 @@ Status IndexCatalog::_dropIndex(OperationContext* opCtx, IndexCatalogEntry* entr opCtx->recoveryUnit()->registerChange( new IndexRemoveChange(opCtx, _collection, &_entries, entry)); _collection->infoCache()->droppedIndex(opCtx, indexName); - entry = NULL; + entry = nullptr; _deleteIndexFromDisk(opCtx, indexName, indexNamespace); _checkMagic(); @@ -1045,7 +1044,7 @@ int IndexCatalog::numIndexesReady(OperationContext* opCtx) const { } bool IndexCatalog::haveIdIndex(OperationContext* opCtx) const { - return findIdIndex(opCtx) != NULL; + return findIdIndex(opCtx) != nullptr; } IndexCatalog::IndexIterator::IndexIterator(OperationContext* opCtx, @@ -1056,20 +1055,20 @@ IndexCatalog::IndexIterator::IndexIterator(OperationContext* opCtx, _catalog(cat), _iterator(cat->_entries.begin()), _start(true), - _prev(NULL), - _next(NULL) {} + _prev(nullptr), + _next(nullptr) {} bool IndexCatalog::IndexIterator::more() { if (_start) { _advance(); _start = false; } - return _next != NULL; + return _next != nullptr; } IndexDescriptor* IndexCatalog::IndexIterator::next() { if (!more()) - return NULL; + return nullptr; _prev = _next; _advance(); return _prev->descriptor(); @@ -1086,7 +1085,7 @@ IndexCatalogEntry* IndexCatalog::IndexIterator::catalogEntry(const IndexDescript } void IndexCatalog::IndexIterator::_advance() { - _next = NULL; + _next = nullptr; while (_iterator != _catalog->_entries.end()) { IndexCatalogEntry* entry = _iterator->get(); @@ -1119,7 +1118,7 @@ IndexDescriptor* IndexCatalog::findIdIndex(OperationContext* opCtx) const { if (desc->isIdIndex()) return desc; } - return NULL; + return nullptr; } IndexDescriptor* IndexCatalog::findIndexByName(OperationContext* opCtx, @@ -1131,7 +1130,7 @@ IndexDescriptor* IndexCatalog::findIndexByName(OperationContext* opCtx, if (desc->indexName() == name) return desc; } - return NULL; + return nullptr; } IndexDescriptor* IndexCatalog::findIndexByKeyPatternAndCollationSpec( @@ -1148,7 +1147,7 @@ IndexDescriptor* IndexCatalog::findIndexByKeyPatternAndCollationSpec( return desc; } } - return NULL; + return nullptr; } void IndexCatalog::findIndexesByKeyPattern(OperationContext* opCtx, @@ -1168,7 +1167,7 @@ void IndexCatalog::findIndexesByKeyPattern(OperationContext* opCtx, IndexDescriptor* IndexCatalog::findShardKeyPrefixedIndex(OperationContext* opCtx, const BSONObj& shardKey, bool requireSingleKey) const { - IndexDescriptor* best = NULL; + IndexDescriptor* best = nullptr; IndexIterator ii = getIndexIterator(opCtx, false); while (ii.more()) { @@ -1245,10 +1244,11 @@ const IndexDescriptor* IndexCatalog::refreshEntry(OperationContext* opCtx, BSONObj keyPattern = spec.getObjectField("key"); // Re-register this index in the index catalog with the new spec. - IndexDescriptor* newDesc = - new IndexDescriptor(_collection, _getAccessMethodName(opCtx, keyPattern), spec); + auto newDesc = stdx::make_unique<IndexDescriptor>( + _collection, _getAccessMethodName(opCtx, keyPattern), spec); const bool initFromDisk = false; - const IndexCatalogEntry* newEntry = _setupInMemoryStructures(opCtx, newDesc, initFromDisk); + const IndexCatalogEntry* newEntry = + _setupInMemoryStructures(opCtx, std::move(newDesc), initFromDisk); invariant(newEntry->isReady(opCtx)); // Return the new descriptor. diff --git a/src/mongo/db/catalog/index_catalog.h b/src/mongo/db/catalog/index_catalog.h index a7d6f4923f1..968a5b0dba1 100644 --- a/src/mongo/db/catalog/index_catalog.h +++ b/src/mongo/db/catalog/index_catalog.h @@ -400,7 +400,7 @@ private: // initFromDisk: Avoids registering a change to undo this operation when set to true. // You must set this flag if calling this function outside of a UnitOfWork. IndexCatalogEntry* _setupInMemoryStructures(OperationContext* opCtx, - IndexDescriptor* descriptor, + std::unique_ptr<IndexDescriptor> descriptor, bool initFromDisk); // Apply a set of transformations to the user-provided index object 'spec' to make it |