Issue 19256 - std.json: JSONValue allows violating constness
Summary: std.json: JSONValue allows violating constness
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86_64 Linux
: P3 normal
Assignee: No Owner
URL:
Keywords: pull
Depends on:
Blocks:
 
Reported: 2018-09-21 08:50 UTC by FeepingCreature
Modified: 2024-12-01 16:34 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description FeepingCreature 2018-09-21 08:50:32 UTC
Consider the following code:

import std.json;

@safe unittest {
    const JSONValue innerObj = JSONValue(["foo": JSONValue(1)]);

    assert(innerObj["foo"] == JSONValue(1));

    // Why can I do this??
    JSONValue value = innerObj;

    value["foo"] = JSONValue(2);

    assert(innerObj["foo"] == JSONValue(1));
}

innerObj is changed, even though we access it through a const variable.

This should not be allowed.
Comment 1 FeepingCreature 2018-09-21 09:03:49 UTC
See also bug 12885. https://issues.dlang.org/show_bug.cgi?id=12885

This bug is still relevant, as it happens in @safe code, not just @system - std.json violates @trusted by allowing bad things to happen.
Comment 2 Walter Bright 2018-12-16 10:33:14 UTC
Is this a duplicate of 12885 then?
Comment 3 FeepingCreature 2018-12-16 13:36:22 UTC
I'd call it a consequence or subset of 12885, in that fixing 12885 also fixes this.

But if 12885 was closed wontfix, this one would still need a separate fix.
Comment 4 Dlang Bot 2019-05-17 05:14:44 UTC
@FeepingCreature updated dlang/phobos pull request #6716 "Fix issue 19256: prevent JSONValue const conversion" fixing this issue:

- std.json: Fix issue 19256, prevent auto JSONValue = const(JSONValue)
  Reorder JSONValue's union members to make dmd realize that JSONValue contains const references and disallows reassigning const(JSONValue) to JSONValue, which would break constness.
  This works around https://issues.dlang.org/show_bug.cgi?id=12885 .

https://github.com/dlang/phobos/pull/6716
Comment 5 FeepingCreature 2024-10-27 12:44:42 UTC
Fixed by https://github.com/dlang/dmd/pull/16594 when `-preview=fixImmutableConv` is enabled.
Comment 6 dlangBugzillaToGithub 2024-12-01 16:34:11 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/phobos/issues/10350

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB