Here is the definition of my function: char*_ft_strdup(const char*s);
I'm trying to reimplement a strdup, but I get stuck with a segfault that occurs at malloc time.
I noticed that it doesn't segfault anymore when I put the pop rdi above malloc, but I don't understand this behavior, any idea?
.intel_syntax
extern malloc
extern _ft_strlen
section .text
global _ft_strdup
_ft_strdup:
push rbp ; prologue
mov rbp, rsp ; prologue
push rdi ; save rdi value (string src)
call _ft_strlen ; compute length of string src
mov rdi, rax ; set number of bytes to allocate
call malloc ; allocate x bytes
cmp rax, 0
je exit_prog
pop rdi ; retrieve string src
pop rbp ; epilogue
ret
exit_prog:
xor rax, rax
pop rbp
ret