reko icon indicating copy to clipboard operation
reko copied to clipboard

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

Open toxamin opened this issue 9 months ago • 2 comments

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;
}

toxamin avatar Mar 11 '25 11:03 toxamin

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?

uxmal avatar Mar 12 '25 10:03 uxmal

tcc.zip

Compiled using tcc

toxamin avatar Mar 12 '25 12:03 toxamin