The following issues were found

src/mongo/db/pipeline/document_source_replace_root_test.cpp
1 issues
syntax error
Error

Line: 69

              
// Verify that sending $newRoot a field path that contains an object in the document results
// in the replacement of the root with that object.
TEST_F(ReplaceRootBasics, FieldPathAsNewRootPromotesSubdocument) {
    auto replaceRoot = createReplaceRoot(BSON("newRoot"
                                              << "$a"));
    Document subdoc = Document{{"b", 1}, {"c", "hello"_sd}, {"d", Document{{"e", 2}}}};
    auto mock = DocumentSourceMock::createForTest(Document{{"a", subdoc}}, getExpCtx());
    replaceRoot->setSource(mock.get());

            

Reported by Cppcheck.

src/mongo/db/pipeline/document_source_sample.cpp
1 issues
Shifting by a negative value is undefined behaviour
Error

Line: 139 CWE codes: 758

                  // just used to communicate ascending/descending information. A pattern like {$meta: "randVal"}
    // is neither ascending nor descending, and so will not be useful when constructing the merging
    // logic.
    logic.inputSortPattern = BSON("$rand" << -1);
    return logic;
}
}  // namespace mongo

            

Reported by Cppcheck.

src/mongo/db/pipeline/document_source_sample_test.cpp
1 issues
syntax error
Error

Line: 136

              /**
 * A sample of size 0 should return 0 results.
 */
TEST_F(SampleBasics, ZeroSize) {
    loadDocuments(2);
    checkResults(0, 0);
}

/**

            

Reported by Cppcheck.

src/mongo/db/pipeline/document_source_sequential_document_cache_test.cpp
1 issues
syntax error
Error

Line: 50

              
const long long kDefaultMaxCacheSize = internalDocumentSourceLookupCacheSizeBytes.load();

TEST_F(DocumentSourceSequentialDocumentCacheTest, ReturnsEOFOnSubsequentCallsAfterSourceExhausted) {
    SequentialDocumentCache cache(kDefaultMaxCacheSize);
    auto documentCache = DocumentSourceSequentialDocumentCache::create(getExpCtx(), &cache);

    auto source = DocumentSourceMock::createForTest({"{a: 1, b: 2}", "{a: 3, b: 4}"}, getExpCtx());
    documentCache->setSource(source.get());

            

Reported by Cppcheck.

src/mongo/db/pipeline/document_source_set_window_fields_test.cpp
1 issues
syntax error
Error

Line: 85

                      40415);
}

TEST_F(DocumentSourceSetWindowFieldsTest, FailsToParseIfArgumentsAreRepeated) {
    auto spec = fromjson(R"(
        {$_internalSetWindowFields: {partitionBy: '$state', sortBy: {city: 1}, output: {mySum:
        {$sum: '$pop', $max: '$pop', window: {documents: [-10, 0]}}}}})");
    ASSERT_THROWS_CODE(
        DocumentSourceInternalSetWindowFields::createFromBson(spec.firstElement(), getExpCtx()),

            

Reported by Cppcheck.

src/mongo/db/pipeline/document_source_skip_test.cpp
1 issues
syntax error
Error

Line: 71

                  ASSERT_TRUE(skip->getNext().isEOF());
}

TEST_F(DocumentSourceSkipTest, SkipsChainedTogetherShouldNotOverFlowWhenOptimizing) {
    // $skip should not optimize if combining the two values of skips would overflow a long long.
    auto skipShort = DocumentSourceSkip::create(getExpCtx(), 1);
    auto skipLong = DocumentSourceSkip::create(getExpCtx(), std::numeric_limits<long long>::max());
    Pipeline::SourceContainer overflowContainer;
    overflowContainer.push_back(skipShort);

            

Reported by Cppcheck.

src/mongo/db/pipeline/document_source_sort_by_count.cpp
1 issues
Shifting by a negative value is undefined behaviour
Error

Line: 76 CWE codes: 758

                  groupExprBuilder.append("count", BSON("$sum" << 1));

    BSONObj groupObj = BSON("$group" << groupExprBuilder.obj());
    BSONObj sortObj = BSON("$sort" << BSON("count" << -1));

    auto groupSource = DocumentSourceGroup::createFromBson(groupObj.firstElement(), pExpCtx);
    auto sortSource = DocumentSourceSort::createFromBson(sortObj.firstElement(), pExpCtx);

    return {groupSource, sortSource};

            

Reported by Cppcheck.

src/mongo/db/pipeline/document_source_sort_by_count_test.cpp
1 issues
syntax error
Error

Line: 88

                  }
};

TEST_F(SortByCountReturnsGroupAndSort, ExpressionFieldPathSpec) {
    BSONObj spec = BSON("$sortByCount"
                        << "$x");
    Value expectedGroupExplain =
        Value{Document{{"_id", "$x"_sd}, {"count", Document{{"$sum", Document{{"$const", 1}}}}}}};
    testCreateFromBsonResult(spec, expectedGroupExplain);

            

Reported by Cppcheck.

src/mongo/db/pipeline/document_source_sort_test.cpp
1 issues
syntax error
Error

Line: 98

              };


TEST_F(DocumentSourceSortTest, RejectsNonObjectSpec) {
    BSONObj spec = BSON("$sort" << 1);
    BSONElement specElement = spec.firstElement();
    ASSERT_THROWS(DocumentSourceSort::createFromBson(specElement, getExpCtx()), AssertionException);
}


            

Reported by Cppcheck.

src/mongo/db/pipeline/document_source_union_with_test.cpp
1 issues
syntax error
Error

Line: 96

                  ASSERT_TRUE(unionWithTwo.getNext().isEOF());
}

TEST_F(DocumentSourceUnionWithTest, BasicNestedUnions) {
    const auto docs = std::array{Document{{"a", 1}}, Document{{"b", 1}}, Document{{"c", 1}}};
    const auto mock = DocumentSourceMock::createForTest(docs[0], getExpCtx());
    const auto mockDequeOne = std::deque<DocumentSource::GetNextResult>{Document{docs[1]}};
    const auto mockDequeTwo = std::deque<DocumentSource::GetNextResult>{Document{docs[2]}};
    const auto mockCtxOne = getExpCtx()->copyWith({});

            

Reported by Cppcheck.