Skip to content

Constant HDC objects can be modified indirectly (may result in undefined behaviour) #3

Description

@maarten-ic

A function accepting a const HDC object can (accidentally) cast away const-ness as in the following example program.

#include <hdc.hpp>

void manipulate_const_hdc(HDC const &in) {
    HDC copy = in;
    copy["new member"] = 1;
}

int main() {
    HDC test;
    test["only member"] = 1;
    test.dump();
    manipulate_const_hdc(test);
    test.dump();
    return 0;
}

Expected behaviour:

The function manipulate_const_hdc gets a constant reference to a HDC object. It shouldn't be able to alter this object as doing so results in undefined behavior, see cppreference.com:

  • A const object is
    - an object whose type is const-qualified, or
    - a non-mutable subobject of a const object.
    Such object cannot be modified: attempt to do so directly is a compile-time error, and attempt to do so indirectly (e.g., by modifying the const object through a reference or pointer to non-const type) results in undefined behavior.

The copy assignment HDC copy = in; should either be prohibited, or it should create a (deep) copy of the HDC object such that the assignment copy["new member"] = 1; doesn't influence the original HDC object.

Actual behaviour:

Output of above test program:

{
        "only member" : 1
}
{
        "new member" : 1,
        "only member" : 1
}

Used version of the HDC library: 0.21.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions