C++ strtok-缓冲区溢出
c++函数,strtok() 如果str未正确终止,此示例是否会出现缓冲区溢出C++ strtok-缓冲区溢出,c++,strtok,C++,Strtok,c++函数,strtok() 如果str未正确终止,此示例是否会出现缓冲区溢出 /* strtok example */ /* source - cplusplus.com (see link below) */ #include <stdio.h> #include <string.h> int main () { char str[] ="- This, a sample string."; char * pch; printf ("Splitting
/* strtok example */
/* source - cplusplus.com (see link below) */
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] ="- This, a sample string.";
char * pch;
printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str," ,.-");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ,.-"); // walk the stack?
}
return 0;
}
走那一堆
谢谢 如果字符串不是以null结尾的,则大多数字符串处理函数都将离开末尾
但是,在您的代码示例中,
str
被终止。如果字符串不是以null终止的,则大多数字符串处理函数将从末尾退出
但是,在您的代码示例中,
str
被终止。。我的示例显示了这一点,但如果我使用的函数没有字符串终止,则可能会发生缓冲区溢出。谢谢收到。我的示例显示了这一点,但如果我使用的函数没有字符串终止,则可能会发生缓冲区溢出。谢谢考虑这一点的简单方法是,如果str
没有正确终止,那么它就不是C样式的字符串。只是一些角色。当然,将C字符串以外的任何内容传递给strtok是个坏消息。这是针对某个白帽黑客的吗?简单地说,如果str
没有正确终止,那么它就不是C样式的字符串。只是一些角色。当然,将C字符串以外的任何东西传递给strtok是个坏消息。这是针对一些白帽黑客的吗?
pch = strtok (NULL, " ,.-");