[SERVER-61608] Unreachable code in topology_coordinator.cpp Created: 18/Nov/21  Updated: 29/Oct/23  Resolved: 13/Dec/21

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

Type: Improvement Priority: Major - P3
Reporter: Alexey Anisimov (Inactive) Assignee: Frederic Vitzikam
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible
Sprint: Replication 2021-12-13, Replication 2021-12-27
Participants:

 Description   

`readPreference == ReadPreference::PrimaryOnly` on line 291 will never evaluate to false because there is exactly the same parent `if` check. Which means the `else` block is unreachable.

https://github.com/mongodb/mongo/blob/0cceba6f04b95f3652de84c2f1f4ab2a644dba6e/src/mongo/db/repl/topology_coordinator.cpp#L291 

First, the code was introduced with this PR https://github.com/mongodb/mongo/commit/7296a460f826c8a618147e09606c5f1935c482a4 The outer check has two ORed conditions: 

    if (readPreference == ReadPreference::PrimaryOnly ||
        (chainingPreference == ChainingPreference::kUseConfiguration &&
         !_rsConfig.isChainingAllowed()))

Which means the inner check can evaluate false.

Later, with this change https://github.com/mongodb/mongo/commit/a7ff5838b5a4bcb4881a08c5dafce671c3b54940 the condition in the outer `if` statement is changed and the inner `if` can never be false anymore.

The issue was found by an external user trungtechnews@gmail.com.



 Comments   
Comment by Githook User [ 13/Dec/21 ]

Author:

{'name': 'Frederic Vitzikam', 'email': 'frederic.vitzikam@mongodb.com', 'username': 'fredvitz'}

Message: SERVER-61608 Remove unreachable code in topology_coordinator.cpp
Branch: master
https://github.com/mongodb/mongo/commit/09d330def86f4467559cebbf0e91ecf038c296e6

Generated at Thu Feb 08 05:52:52 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.