简体   繁体   中英

*** stack smashing detected ***: ./asem terminated Segmentation fault (core dumped)

I'm compiling my program and I'm having error: * stack smashing detected * : ./asem terminated Segmentation fault (core dumped) I don't know what I'm doing wrong. My part was to write in zadanie1 with no %0,%1 and %2.

#include <stdio.h>

  int main()

  {
     char *x= "abcabab xxabc";
     char *y= "ab";
     char bufor[4];
    asm volatile(
    ".intel_syntax noprefix;"
    "mov ebx,%1;"
    "push ebx;"
    "mov ebx,%2;"
    "push ebx;"
    "mov ebx,%0;"
    "call zadanie1;"
    "jmp wyjscie;"
    "zadanie1:"
    "mov ah,[ebx];"
    "cmp ah,0;"
    "jz wyjscie;"

    "push ebp;"
    "mov ebp,esp;"
    "mov edx,[ebp+8];"
    "mov al,[edx];"

    "compare:"
    "cmp ah,al;"
    "jnz diff;"

    "inc ebx;"
    "mov ah,[ebx];"
    "cmp ah,0;"
    "jz wyjscie;"

    "inc edx;"
    "mov al,[edx];"
    "cmp al,0;"
    "jnz diff;"
    "inc ecx;"
    "mov edx,[ebp+8];"
    "jmp compare;"

    "diff:"
    "inc ebx;"
    "mov ah,[ebx];"
    "cmp ah,0;"
    "jz wyjscie;"
    "inc edx;"
    "mov al,[edx];"
    "jmp compare;"



    "wyjscie:"

    "pop ebx;"
    "mov [ebx],ecx;"
    "pop ebx;"
    "pop ebp;"
    ".att_syntax prefix;"
    :
    :"r"(x),"r"(y), "r"(bufor)
    :"ebx"
);

return 0;

}

You need to construct the code properly so you can see where the pushes and the pops don't balance. In the 4th line of "zadanie1:" you do another "push ebp;" yet you still arrive at "wyjscie:" to pop the same number of items from the stack as when you skipped that push. Also, do a proper return from a call instead of popping the return address if you want to keep track of the stack maintenance, and gcc has spotted all this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM