Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested Casts Don't Match 010 Behavior #122

Open
d0c-s4vage opened this issue Jan 20, 2020 · 0 comments
Open

Nested Casts Don't Match 010 Behavior #122

d0c-s4vage opened this issue Jan 20, 2020 · 0 comments
Assignees
Labels
bug concrete-issue An actionable, concrete issue that includes stack trace & technical details. Usually post-triage

Comments

@d0c-s4vage
Copy link
Owner

This is a tricky one that came up during the work on #120. I decided to make it a separate issue.

Describe the bug

Nested casts do not match gcc behavior - is the "correct" behavior here undefined in the C standard? (still tracking that down). I thought it seemed clear, but I need to verify.

Consider the 010 template code below:

local ubyte a_byte = 0xff;
local int final = (uint)((ushort)(a_byte << 4));
Printf("%08x", final);

The output of the template above when run in 010 Editor is 000000f0.

Now consider this small C program:

#include <stdio.h>

typedef unsigned char ubyte;
typedef unsigned short ushort;
typedef unsigned int uint;

int main(int argc, const char *argv[])
{
    unsigned char a_byte = 0xff;
    int final = (uint)((ushort)(a_byte << 4));
    printf("%08x", final);

    return 0;
}

When compiled and run with gcc, the output is 00000ff0, which is what pfp is currently mirroring.

Which is correct? Is it a bug in 010 Editor, or undefined behavior in the C standard?

If it's undefined behavior, we need to mirror 010 Editor. If it's a bug in 010 Editor, we should match the C standard.

To Reproduce

Run the provided template script and C programs and compare their output.

Expected Behavior

010 Editor, pfp, and gcc should have the same output, 00000ff0.

Implementation/Fix Notes/Thoughts

Here's a test that demonstrates the problem:

    def test_nested_casts(self):
        dom = self._test_parse_build(
            "",
            """
                local ubyte a_byte = 0xff;
                local int final = (uint)((ushort)(a_byte << 4));
                Printf("%08x", final);
            """,
            # stdout="00000ff0", # matches gcc
            stdout="000000f0", # matches 010 editor
        )
@d0c-s4vage d0c-s4vage added bug concrete-issue An actionable, concrete issue that includes stack trace & technical details. Usually post-triage labels Jan 20, 2020
@d0c-s4vage d0c-s4vage self-assigned this Jan 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug concrete-issue An actionable, concrete issue that includes stack trace & technical details. Usually post-triage
Projects
None yet
Development

No branches or pull requests

1 participant