I'm studying the code of the function strtok from bsd's libc, when I ran it in my machine,
the program received signal SIGSEGV
in s[-1] = 0
.
Here's the link to the code.
Is s[-1] = 0
right?
This is my code:
#include <stdio.h>
#include <stdlib.h>
#include "strtok.c"
int main(int argc, char* argv[]) {
char* str = "xxxx xxxyy fdffd";
const char* s = " ";
char* token = strtok(str, s);
while (token != NULL) {
printf("%s\n", token);
token = strtok(NULL, s);
}
return 0;
}
s[-1]
Is expanded to:
*( s - 1 )
Therefore, if the result points to valid memory, the code is defined.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With