Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on memcmp(3) memory containing invalid values

Parent

memcmp(3) memory containing invalid values

+9
−0

What does it mean that we can use memcmp(3) on invalid values?

ISO C allows comparing an invalid value through memcmp(3), because it doesn't read the value, but rather its representation, and "reading the representation is never Undefined Behavior" (or so I've been told).

The following code invokes UB:

int foo(void)
{
    int32_t  x;

    if (x == 0)  // <- UB
        return 0;
    return 1;
}

However, the following seems to be allowed:

int bar(void)
{
    int32_t  x;

    if (memcmp(&x, "\0\0\0\0", sizeof(x)) == 0) {
        /*
         * Could we do the following?
         *
         *     return x;
         */
        return 0;
    }
    return 1;
}

But does that memcmp(3) call give us any guarantees about the value stored in x? That is, if we know that the representation is 0, can we assume that the value of x is precisely 0? Or is it still UB to use the value after knowing its representation?

And if it's still UB to read the variable even when we know that the representation is a valid one, what's the point in allowing one to memcmp(3) a variable with an invalid value, such as an uninitialized local variable, or a freed pointer?

Being able to know the representation of an invalid value doesn't match very well with the concept of the abstract machine, it seems to me. It's like being able to look outside of the cave, but not being allowed to use that information.


Moreover, while the following is UB:

bool foo2(void)
{
    int32_t  x;

    return (x == x);  // <- UB
}

the following would have defined behavior, I guess:

bool bar2(void)
{
    int32_t  x;

    return (memcmp(&x, &x, sizeof(x)) == 0);
}

The above should always return true, I guess, since the representation of a value, even if it's invalid, is the same, independent of how many times you read it (so far, I don't think ISO C considers quantum variables ;), as long as we don't invoke UB.


This question came to my mind while discussing about use-after-free issues with realloc(3): https://inbox.sourceware.org/gcc/3098fd18-9dbf-b4e9-bae5-62ec6fea74cd@opteya.com/T/

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

Post
+3
−0

MEMCMP simply compares the memory bits between two locations. This has nothing to do with whatever those bits might mean.

Your first example, on the other hand, compares the contents of variable X with the value 0. Doing a MEMCMP with an area of memory with all bits 0 is the same thing, only if you know the representation of the value 0 in X is all bits 0.

The distinction becomes more obvious when using a value other than 0. Let's say you know X is a 32 bit integer and you want to check whether its value is 7. The expression (x == 7) will always work, since it's directly asking what you want to know. It does not suppose a particular representation for the value 7. Using MEMCMP, do you compare the bits of X with the byte sequence 00 00 00 07, or 07 00 00 00? Both are quite possible on common modern machines. The difference in this example is whether the machine is little or big endian.

I used endianess as an example only. There are other possible reasons that make this kind of use of MEMCMP machine and possibly compiler specific. For example, the representation of 1.0 in a common 32 bit floating point format is 3F800000h. And that's only the value of the raw bits expressed as a 32 bit integer. Endianess still applies.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

4 comment threads

Optimizations/observable behavior (1 comment)
Ignore endianness (1 comment)
bits vs bytes (1 comment)
Uninitialized variable (1 comment)
Ignore endianness
alx‭ wrote almost 2 years ago

I chose 0 on purpose to avoid endianness issues, and also to make sure I know the translation from representation to value. I could have also chosen maybe 0x1111 or something similar which isn't affected by endianness, but 0 was just simpler.

I'm almost sure representation 0 has to correspond to value 0 in integers in C (but I didn't check). In C23 we'll have that for granted, since it will mandate 2's complement.

Does knowing the representation, and the fact that a given representation corresponds to an exact value, guarantee that the variable will hold the value if we read its value? Or is it still UB just because UB is UB?

This is funny because if it stops being UB, it would be a bit of a Schroedinger's cat, where the value of an uninitialized variable is undefined, until you "measure" its representation, at which point you would have tied it to a specific value just by reading it. :D