2

I have very limited experience with C++ but I do have plenty of experience with Java and NodeJS.

I've decompiled and tried to make sense of a small C++ compiled file and I've come across something that I do not understand. To me this looks like a value being assigned to an expression which seems weird to me.

What is actually happening and does it even makes sense? (Considering some magic has been done by the decompilation tool to show "C++" code)

*reinterpret_cast<signed char*>(reinterpret_cast<int64_t>(rax5) + rsi7 * 4 + reinterpret_cast<int32_t>(ecx8)) = *reinterpret_cast<signed char*>(&esi10);

Formatted

*reinterpret_cast<signed char*>(
    reinterpret_cast<int64_t>(rax5)
    + rsi7
    * 4
    + reinterpret_cast<int32_t>(ecx8)
) = *reinterpret_cast<signed char*>(&esi10);
Kilian Foth
  • 107,706
  • 45
  • 295
  • 310
E. Sundin
  • 131
  • 4
  • note that all the variables in that expression look like register names, so the casts are there to tell you what the types of the variables that have been enregistered are – Caleth Apr 28 '16 at 13:09

1 Answers1

7

The value isn't assigned to the complicated expression. The expression is evaluated to a pointer, and the value is assigned to the location of that pointer (via the dereference operator *). The entire thing is quite legit, it just looks really weird because of the complicated casts.

Kilian Foth
  • 107,706
  • 45
  • 295
  • 310
  • I don't get it. Unless it's a comparsion, shouldn't the `*_cast` be always present only on the right side of the expression? How would the code in question even compile? Are you able to assign a value to the `reinterpret_cast` function? – Andy Apr 28 '16 at 13:35
  • 2
    @DavidPacker Note the `*` before `reinterpret_cast(...) =`; since the left-side expression evaluates to a pointer we are able to dereference it - and the right-side expression's value is assigned to that dereferenced pointer. In other words - left side evaluates to an address, and right side's value is put under that address. – Mael Apr 28 '16 at 13:56
  • @Mael So you're effecitively completely ignoring any variables, which could have any place in the memory the compiler finds suitable, and put the value from the right side under specific memory which you actually know where it is, perhaps so you can access using it the same hack in another process? Do I understand that correctly? If that's so, it seems like an awful practice for general software development, although I can see it be used in a low level environment with little operation memory where introducing extra variables is a constraint. – Andy Apr 28 '16 at 14:02
  • 1
    @DavidPacker Exactly. Keep in mind, though, that the question includes *decompiled* code. Original source probably looks much better. – Mael Apr 28 '16 at 14:05
  • @Mael Oh, I completely ignored that part of the questions. Thanks for the explanation. – Andy Apr 28 '16 at 14:12