[SERVER-83541] Ensure StringData::compare uses handle return values outside {-1, 0, 1} Created: 22/Nov/23  Updated: 29/Nov/23  Resolved: 29/Nov/23

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

Type: Bug Priority: Major - P3
Reporter: Dan Larkin-York Assignee: Billy Donahue
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
Backwards Compatibility: Fully Compatible
Operating System: ALL
Sprint: Service Arch 2023-11-27, Service Arch 2023-12-11
Participants:
Linked BF Score: 153

 Description   

SERVER-82604 recently changed the underlying implementation of StringData to rely on std::string_view, and simply pass through most functionality. However, the string_view::compare API contract is different than StringData, so we would still need to clamp the return values to -1, 0, 1 to provide the same compatibility.

Instead, we would like to audit the codebase for uses of StringData::compare to ensure that callers are not relying on -1/1 values specifically, but instead checking for negative/positive values.



 Comments   
Comment by Githook User [ 28/Nov/23 ]

Author:

{'name': 'Billy Donahue', 'email': 'billy.donahue@mongodb.com', 'username': 'BillyDonahue'}

Message: SERVER-83541 fix: accept StringData::compare wider range
Branch: master
https://github.com/mongodb/mongo/commit/96dee97140e43efba9fa704c3ca32ff79013fe04

Comment by Billy Donahue [ 22/Nov/23 ]

Until SERVER-55180, sbe was written in terms of std::string_view directly.
So really it couldn't have been relying on compare's clamped range until then.
So I think there won't be much to fix.

Comment by Dan Larkin-York [ 22/Nov/23 ]

Updated the title and description to take the path Billy suggested instead.

Comment by Billy Donahue [ 22/Nov/23 ]

We need to make callers tolerant of the range of StringData::compare.

The exact values of -1,0,1 are never part of compare contracts. That's not what strcmp or char_traits::compare returns. They just do the "negative, zero, positive" bins for efficiency. They're usually returning the arithmetic difference between the first differing characters.

The API of StringData has to match std::string_view exactly.

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