Reverse Engineering for Beginners

(avery) #1

CHAPTER 11. GOTO OPERATOR CHAPTER 11. GOTO OPERATOR


Chapter 11


GOTO operator


The GOTO operator is generally considered as anti-pattern. [Dij68], Nevertheless, it can be used reasonably [Knu74], [Yur13,
p. 1.3.2].


Here is a very simple example:


#include <stdio.h>


int main()
{
printf ("begin\n");
goto exit;
printf ("skip me!\n");
exit:
printf ("end\n");
};


Here is what we have got in MSVC 2012:


Listing 11.1: MSVC 2012

$SG2934 DB 'begin', 0aH, 00H
$SG2936 DB 'skip me!', 0aH, 00H
$SG2937 DB 'end', 0aH, 00H


_main PROC
push ebp
mov ebp, esp
push OFFSET $SG2934 ; 'begin'
call _printf
add esp, 4
jmp SHORT $exit$3
push OFFSET $SG2936 ; 'skip me!'
call _printf
add esp, 4
$exit$3:
push OFFSET $SG2937 ; 'end'
call _printf
add esp, 4
xor eax, eax
pop ebp
ret 0
_main ENDP


Thegotostatement has been simply replaced by aJMPinstruction, which has the same effect: unconditional jump to another
place. The secondprintf()could be executed only with human intervention, by using a debugger or by patching the code.

Free download pdf