How to make it so that after executing return go to any other part of the program instead of the function call point?

The answer can not even suggest: (

    3 answers 3

    This is one of the crazy assignments for interviews, when they offer to make complete nonsense and write code for which you should take your hands off in life, instead of checking out the real knowledge of the candidate?

    Alternatively, define return:

     #include <stdio.h> #define return goto ll void main() { return; printf("You shouldn't see this\n"); ll: printf("After return\n"); } 

    Option two is to replace the return address by simulating a buffer overflow (talking about the call stack, I offer this example to students). Remarks: (1) it works only on 32 bits, (2) after the output of the string, the program will of course collapse. Both shortcomings are easy to fix, I suggest to do it yourself.

     #include <stdio.h> void non_called_function() { printf("You shouldn't see this\n"); } void f() { int testArray[1]; // Замещаем адрес возврата в main адресом non_called_function testArray[2] = (int) non_called_function; } int main() { f(); return 0; } 
    • Perhaps this is a task from an interview, but I saw it on the Internet and did not find an answer for myself. - angry
    • one
      On 64-bit "you shouldn't see this" with small changes, too, "works", although the joke with \ #define amused me more. long testArray [1]; testArray [3] = (long) non_called_function; For 32-bit, you can also write a long (not tested, but it will work). So the difference between 32 and 64-bit in 1 element of the array. Interestingly, what is there before the return address falls on the stack at 64-bit? From gcc -S did not understand. @northerner, and without segmentation fault weakly? - avp
    • one
      In Linux, yes, you can write a long. But with long it does not work on 64-bit Windows, unlike Linux, there is LLP64 data model, sizeof (long) = 4, although the pointer is 64-bit, so you have to write long long or __int64 depending on the compiler. - northerner
    • one
      Then for portability you can take an array of pointers. But not so interesting. - Segmentation fault in 64-bit also overcame, main after non_called_function () works. - avp
    • one
      > I wonder what is there before the return address falls on the stack at 64-bit? On 64-bits, GCC by default allocates memory in blocks that are multiples of 16 bytes, so two array elements fit into the saved rbp register and the return address. In the assembly code, this can be seen if you score with some constants testArray [0] and testArray [1]. It seems like this behavior is regulated by the -mpreferred-stack-boundary switch, but I have not tried it. - northerner

    If you want the transition to take place exactly by return, this is one thing. If just at the end of the function, then look towards long_jmp . Although, in fact, I would deal with such things as a last resort.

    • Yes, it's just a joke (warm up for the tail). - avp
    • using jmp to exit incorrectly, because branch predictor will think that we are still in the subroutine - karmadro4

    And if you use an unconditional goto transition?