[SERVER-68822] Coverity analysis defect 122804: AUTO_CAUSES_COPY Created: 15/Aug/22  Updated: 14/Jul/23  Resolved: 14/Jul/23

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Coverity Collector User Assignee: Jennifer Wang (Inactive)
Resolution: Won't Do Votes: 0
Labels: coverity, neweng
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Assigned Teams:
Service Arch
Operating System: ALL
Sprint: Service Arch 2023-07-10, Service Arch 2023-07-24
Participants:

 Description   

AUTO_CAUSES_COPY

Unnecessary object copies can affect performance. Using the auto keyword without an & causes a copy.
/src/mongo/rpc/metadata.cpp:191: AUTO_CAUSES_COPY 122804 Using the "auto" keyword without an "&" causes the copy of an object of type BSONElement.



 Comments   
Comment by Eric Milkie [ 14/Jul/23 ]

kyle.suarez@mongodb.com is the new caretaker for Coverity.
In the 11 months since this ticket was created, we've already decided not to flag any AUTO_CAUSES_COPY defects that involve BSONElement, so there is nothing left to do here besides close this ticket as Wont-Do.

Comment by Jennifer Wang (Inactive) [ 14/Jul/23 ]

This is not a real issue. We'd prefer to silence Coverity rather than make unnecessary changes to avoid the false positive. milkie@mongodb.com Is there a way to silence Coverity?

Comment by Billy Donahue [ 13/Jul/23 ]

False positive. Adding reference quals to this `auto` has no effect, as the initializer expression is a prvalue.

    auto docSequenceElem = docSequenceIt == docSequenceFieldsForCommands.end()
        ? BSONElement()
        : cmdObj[docSequenceIt->second];

The compiler will do the right thing to produce a prvalue inside each branch of the conditional. There is no additional overhead in initializing the local variable by move construction from that prvalue.

https://gcc.godbolt.org/z/xbsqTc6qf

Coverity is flat-out incorrect that auto variables "cause" object copies.
I recommend rejecting issues generated by this Coverity rule.

Comment by Blake Oler [ 22/Aug/22 ]

If this was in a non-legacy part of the command path, I would treat this with higher priority.

Generated at Thu Feb 08 06:11:49 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.