June 05

On Wednesday, 5 June 2024 at 01:18:06 UTC, Paul Backus wrote:

>

On Tuesday, 4 June 2024 at 16:58:50 UTC, Basile B. wrote:

>
void main(string[] args)
{
    ushort a = 0b1111111111111111;
    bool* b = cast(bool*)&a;
    setIt(*b);
    assert(a == 0b1111111100000000); // what actually happens
    assert(a == 0b1111111111111110); // what would be safe
}

[...]

>

Do I corrupt memory here or not ?
Is that a safety violation ?

cast(bool*)&a is a safety violation.

The only safe values for a bool are 0 (false) and 1 (true). By creating a bool* that points to a different value, you have violated the language's safety invariants. Because of this, operations that would normally be safe (reading or writing through the bool*) may now result in undefined behavior.

Obviously the topic was created because of the recent move D made. Sorry for the "catchy" aspect BTW. Now I remember that D safety is unrelated to undefined behaviors.

June 06

On Wednesday, 5 June 2024 at 18:31:12 UTC, Basile B. wrote:

>

On Wednesday, 5 June 2024 at 01:18:06 UTC, Paul Backus wrote:

>

On Tuesday, 4 June 2024 at 16:58:50 UTC, Basile B. wrote:

>
void main(string[] args)
{
    ushort a = 0b1111111111111111;
    bool* b = cast(bool*)&a;
    setIt(*b);
    assert(a == 0b1111111100000000); // what actually happens
    assert(a == 0b1111111111111110); // what would be safe
}

[...]

>

Do I corrupt memory here or not ?
Is that a safety violation ?

cast(bool*)&a is a safety violation.

The only safe values for a bool are 0 (false) and 1 (true). By creating a bool* that points to a different value, you have violated the language's safety invariants. Because of this, operations that would normally be safe (reading or writing through the bool*) may now result in undefined behavior.

Obviously the topic was created because of the recent move D made. Sorry for the "catchy" aspect BTW. Now I remember that D safety is unrelated to undefined behaviors.

I don’t think there’s any meaningful difference. If a program has UB, it can do anything, including corrupt memory. If a program corrupts memory, that’s UB. @safe means UB-free, which includes free of memory corruption.

1 2
Next ›   Last »