2

After porting some obfuscated C code into C++ (namely Fairy-Max chess engine by Harm Geert Muller), I get lots of warnings similar to these:

suggest parentheses around comparison in operand of '&' [-Werror=parentheses]

suggest parentheses around '+' in operand of '&'

While turning off the warnings is not an option, the solution is to add parenthesis in expressions according to the operator precedence.

For example:

if(z&S&&!ab&K==INF&d>2&v>V&v<Beta){

needs to be transformed into this:

if((z&S) && ((!ab)&(K==INF)&(d>2)&(v>V)&(v<Beta))) {

But doing this manually is quite time-consuming.

I tried to use this deobfuscation tool, which uses clang-format internally, but it does not add parenthesis into expressions...

Question

Is there a tool (preferably online) that can take a C/C++ expression as an input and return a warnings-free equivalent expression as an output?

Community
  • 1
  • 1
Andriy Makukha
  • 7,580
  • 1
  • 38
  • 49
  • 1
    Facetiously, wouldn't it be simpler to learn your operator precedence tables? I was once advised to stick a copy on the rear of the door to my downstairs lavatory. Personally I find the "de-obfuscated" version more obfuscating. – Bathsheba Jun 03 '19 at 10:05
  • 1
    `But doing this manually is quite time-consuming` You can't do it automatically. The author of this code had an idea about the expression which might differ from the operator precedence. You need to review these warnings and make the logic explicit. – UmNyobe Jun 03 '19 at 10:11
  • @UmNyobe, the code works correctly, I don't need to review it. I just want to produce a warnings-free code. And I certainly can do it automatically. – Andriy Makukha Jun 03 '19 at 10:16
  • 2
    @Bathsheba, your comment is unrelated to my question. It is the compiler that complains about the expressions, and both me and compiler know the precedence tables. – Andriy Makukha Jun 03 '19 at 10:17
  • Operator precedence is part of the c++ language. Would you write `&(a.b)` or `(a*b)+1`. Maybe you could send a mail to your manager with an estimation of the cost of this non valuable task? – Oliv Jun 03 '19 at 11:51
  • 2
    I understand some people are voting to close this as off-topic under the “recommend a tool” reason. But that rule is because recommendations purportedly “attract opinionated answers and spam.” This answer just requests information about the **existence** of a tool (“Is there a tool”), and the answer lies clearly in the realm of fact: Either a tool properly transforms source by inserting explicit parentheses around the `+` and `&` operations where they appear with lower-precedence operators or it does not. No opinionated recommendation is requested. – Eric Postpischil Jun 03 '19 at 12:28
  • With `gcc -Wall` I got the same warning. This warning disappears when replacing `&` with the more *logical* `&&`, without adding parenthesis. It may help, but it does not answer your need ... – Damien Jun 03 '19 at 12:43
  • I would be surprised if such tool exists. The only option I see is to hack clang to actually insert the parenthesis (where it would normally suggest) instead of issuing a warning. Could be equally time consuming, but much more fun. – user58697 Jun 03 '19 at 18:32
  • This is technically off-topic, but _oh so useful_ so... :) – Lightness Races in Orbit Jun 05 '19 at 15:56

1 Answers1

7

Geordi can do it.

I've long wanted a web version, but last time I tried to get Geordi working on my VPS I failed miserably due to Haskell dependency gubbins. May give it another go one day.

Meanwhile, you can log onto Freenode IRC and /msg geordi --precedence *p->data (for example). You'll get a private message tab open up with the result (e.g. *(p->data)). Feel free to keep sending --precedence <expression> requests in that tab.

Screenshot of tool used on your case

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 1
    Thanks! The test sample from Georgi code `x---------x` = `((((x--)--)--)--)-x` is a perfect example of why such tool should exist and why it's useful in deobfuscation. – Andriy Makukha Jun 06 '19 at 10:49
  • 2
    @AndriyMakukha I've decided I'm going to look into porting it to a web version (just the precedence part) as I believe it would be very popular. – Lightness Races in Orbit Jun 06 '19 at 10:53