binaryninja-api icon indicating copy to clipboard operation
binaryninja-api copied to clipboard

Decompilation should prioritize early returns

Open galenbwill opened this issue 1 year ago • 1 comments

What is the feature you'd like to have? The the structure of a function's decompilation could be improved by prioritizing early returns in cases like the following:

Current decompilation:

void foo(...) {
    if (argX != 0) {
        // here is whole function body
    }
    return
}

Preferred decompilation:

void foo(...) {
    if (argX == 0) {
        return
    }
    // function body
}

Additional Information: Question came from @mostobriv on the public slack, with the following text:

is restructuring occurs only when there is multiple expressions in if block? i've got bunch of examples when i've got nesting of the whole function instead of just early return, with the new option enabled of course.

galenbwill avatar Jun 26 '24 17:06 galenbwill

From Matt Wiseman on public slack:

Would it be feasible to extend this solution to a new context-menu option on conditionals that allows for inverting and reordering arbitrary conditional scopes, not just in the case of early returns?

galenbwill avatar Aug 13 '24 15:08 galenbwill

Early returns are already part of the optimization process, but there was a bug. This will be fixed soon.

D0ntPanic avatar Jun 02 '25 20:06 D0ntPanic

Fixed in 5.1.7555

D0ntPanic avatar Jun 03 '25 20:06 D0ntPanic