fesch / Structorizer.Desktop

Structorizer is a little tool which you can use to create Nassi-Schneiderman Diagrams (NSD).
https://structorizer.fisch.lu
GNU General Public License v3.0
65 stars 20 forks source link

Defective import of C `switch` instructions with conditional `break`s #1163

Closed codemanyak closed 7 months ago

codemanyak commented 7 months ago

The deficiency described in #1159 item 3 also applies to the C99 parser: Import of switch instructions may cause wrong structure if branches are conditionally terminated, as e.g. in the following example (case 0 and case 2):

#include <stdio.h>
#include <stdbool.h>

int test1163(int number, bool left) {
    switch (number) {
        case 0:
            if (left) {
                return 5;
            }
            else {
                number = 2;
                break;
            }
        case 1:
            printf("This should not be appended to case 0!");
            break;
        case 2:
            if (!left) {
                break;
            }
            number = -4;
        case 3:
            while (true) {
                if (--number == 0) break;
            }
        default:
            return number;
    }
    return -1;
}

Clearly, case 1 schould not be appended to case 0 because case 0 terminates in all cases, but break and return instructions are "hidden" within an alternative. C99 import is still too inchoate to find that out, instead an inapproprate "leave" element remains: grafik

codemanyak commented 7 months ago

C99 import now copes with all challenges of the example: grafik