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

Structurer incorrectly converts branches to header to continue inside do while loops #1356

Open
toxamin opened this issue Mar 11, 2025 · 2 comments

Comments

@toxamin
Copy link

toxamin commented Mar 11, 2025

Original code:

#include <stdio.h>
int gg() {
    return 0;
}

int main(void) {
    int smth = gg();
    int nah = gg();
    int bozo = gg();

    while (1) {
        printf("hi\n");

        if (smth) {
            printf("a\n");
            if (!nah) {
                printf("heh\n");
            } else {
                continue; // this skips over the if (bozo) break check
            }
        }

        printf("aye!!\n");

        if (bozo) { // trick the decompiler into thinking this is a do-while
            break;
        }
    }

    return 0;
}

Decompiled code:

word32 fn00401000()
{
    return 0x00;
}


word32 fn00401011()
{
    word32 eax_10 = fn00401000();
    word32 eax_16 = fn00401000();
    word32 eax_22 = fn00401000();
    do
    {
        printf(&g_b402000);
        if (eax_10 != 0x00)
        {
            printf(&g_b402004);
            if (eax_16 != 0x00)
                continue; // this hits the do while check (WRONG BEHAVIOR)
            printf(&g_b402007);
        }
        printf(&g_b40200C);
    } while (eax_22 == 0x00);
    return 0x00;
}
@uxmal
Copy link
Owner

uxmal commented Mar 12, 2025

Thanks for reporting this. I'm not able to reproduce the error on my machine, probably because I'm using a different compiler than you are. Could you please provided the executable you're testing on, so that I can get the same starting point?

@toxamin
Copy link
Author

toxamin commented Mar 12, 2025

tcc.zip

Compiled using tcc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants