#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* make sure all paths are below the current directory
*/
void sanitize(char *str)
{
char *src, *dest;
src = dest = str;
while (*src) {
if (strncmp(src, "/../", 4) == 0) {
src += 3;
} else if (strncmp(src, "//", 2) == 0) {
src ++;
} else {
*dest++ = *src++;
}
}
*dest = '\0';
if (*str == '/')
strcpy(str, str + 1);
if (str[0] == '\0' || strcmp(str, "./") == 0
|| strcmp(str, "./..") == 0) {
strcpy(str, ".");
}
}
int main() {
char buf[] = "/../status";
sanitize(buf);
printf("%s\n", buf);
}
上面的代码在执行strcpy(str, str + 1);
的时候报错了 什么情况?
$ ./sanitize.out
[1] 19007 abort ./sanitize.out
注意 : src和dest不能重叠。