[SERVER-67354] const getter returns garbage while non-const getter works correctly Created: 17/Jun/22  Updated: 27/Oct/23  Resolved: 21/Jun/22

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

Type: Bug Priority: Major - P3
Reporter: Sulabh Mahajan Assignee: Backlog - Storage Execution Team
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-64535 Add Clustered Index Information to co... Closed
Assigned Teams:
Storage Execution
Operating System: ALL
Participants:

 Description   

ClusteredIndexSpec is defined by the idl clustered_collections_options.idl:

    ClusteredIndexSpec:
        description: "The specifications for a given clusteredIndex"
        strict: true
        fields:
            v:
                description: 'Index spec version'
                type: safeInt
                default: 2
                unstable: false
            key:
                description: 'Key to index on'
                type: object_owned
                unstable: false
            name:
                description: 'Descriptive name for the index'
                type: string
                optional: true
                unstable: false
            unique:
                type: safeBool
                unstable: false

There are two getter methods that get defined for the indexSpec in build/opt/mongo/db/catalog/clustered_collection_options_gen.h:

const mongo::ClusteredIndexSpec& getIndexSpec() const { return _indexSpec; }
mongo::ClusteredIndexSpec& getIndexSpec() { return _indexSpec; }

When I try to extract the name, the following code prints garbage:

auto nameOptional = collection->getClusteredInfo()->getIndexSpec().getName();
if (nameOptional) {
    logd("--XYZ-- ------ {}", *nameOptional);
}

Whereas the following gives me the correct name:

auto indexSpec = collection->getClusteredInfo()->getIndexSpec();
auto nameOptional = indexSpec.getName();
if (nameOptional) {
    logd("--XYZ-- ------ {}", *nameOptional);
}

I don't understand why, and it seems like a bug.



 Comments   
Comment by Gregory Noma [ 21/Jun/22 ]

What's going on here is that getClusteredInfo returns by value, but getIndexSpec returns by reference. So after the line

auto nameOptional = collection->getClusteredInfo()->getIndexSpec().getName();

the object returned by getClusteredInfo gets destructed and we get a dangling reference.

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