From b5fafa1f87dda6f8773c5a8a1a5e7776d4d94da7 Mon Sep 17 00:00:00 2001 From: Bernard Gorman Date: Tue, 27 Oct 2020 20:01:45 +0000 Subject: SERVER-51853 Always initialize the numInserted out-parameter in AbstractIndexAccessMethod::insertKeys (cherry picked from commit 8177334703d89a19f38246c5ca95cbf3e5470d91) --- src/mongo/db/index/index_access_method.cpp | 4 ++++ src/mongo/db/index/index_access_method.h | 4 +++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/mongo/db/index/index_access_method.cpp b/src/mongo/db/index/index_access_method.cpp index f88d721ae16..b37c38e713c 100644 --- a/src/mongo/db/index/index_access_method.cpp +++ b/src/mongo/db/index/index_access_method.cpp @@ -183,6 +183,10 @@ Status AbstractIndexAccessMethod::insertKeys(OperationContext* opCtx, const InsertDeleteOptions& options, KeyHandlerFn&& onDuplicateKey, int64_t* numInserted) { + // Initialize the 'numInserted' out-parameter to zero in case the caller did not already do so. + if (numInserted) { + *numInserted = 0; + } // Add all new keys into the index. The RecordId for each is already encoded in the KeyString. for (const auto& keyString : keys) { bool unique = _descriptor->unique(); diff --git a/src/mongo/db/index/index_access_method.h b/src/mongo/db/index/index_access_method.h index 63be0cf3186..3a58f512159 100644 --- a/src/mongo/db/index/index_access_method.h +++ b/src/mongo/db/index/index_access_method.h @@ -109,7 +109,9 @@ public: /** * Inserts the specified keys into the index. Does not attempt to determine whether the - * insertion of these keys should cause the index to become multikey. + * insertion of these keys should cause the index to become multikey. The 'numInserted' output + * parameter, if non-nullptr, will be reset to the number of keys inserted by this function + * call, or to zero in the case of either a non-OK return Status or an empty 'keys' argument. */ virtual Status insertKeys(OperationContext* opCtx, const KeyStringSet& keys, -- cgit v1.2.1