Skip to content

UB with union using #12173

@safocl

Description

@safocl

Operating System Info

Other

Other OS

all OS

OBS Studio Version

Git

OBS Studio Version (Other)

No response

OBS Studio Log URL

https://eel.is/c++draft/defns.undefined

OBS Studio Crash Log URL

No response

Expected Behavior

defined behavior

Current Behavior

[defns.undefined]

3.65 [defns.undefined] undefined behavior
behavior for which this document imposes no requirements
[Note 1: Undefined behavior may be expected when this document omits any explicit definition of behavior or when a program uses an incorrect construct or invalid data. Permissible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message ([defns.diagnostic])), to terminating a translation or execution (with the issuance of a diagnostic message). Many incorrect program constructs do not engender undefined behavior; they are required to be diagnosed. Evaluation of a constant expression ([expr.const]) never exhibits behavior explicitly specified as undefined in [intro] through [cpp]. — end note]

these lines cause undefined behavior:

out = util_add128(out, util_lshift64_internal_32(m));

out = util_add128(out, util_lshift64_internal_64(m));

here output is union with uint32_t i32[4]; active member,

out.i32[3] = (uint32_t)val;

but read as

struct {
			uint64_t low;
			uint64_t high;
};

here:

val = (a.low & 0xFFFFFFFFULL) + (b.low & 0xFFFFFFFFULL);

uint32_t i32[4]; and struct { uint64_t low; uint64_t high; }; are not same standard-layout!

https://eel.is/c++draft/basic.life#2.sentence-3
https://eel.is/c++draft/class.union#general-2
https://eel.is/c++draft/basic.life#8
https://en.cppreference.com/w/cpp/language/union

other links:
https://gitlab.com/libeigen/eigen/-/issues/2898
arduino/ArduinoCore-API#225

Steps to Reproduce

compile and using

Anything else we should know?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions