strtok - 缓冲区溢出
C++ 函数,strtok() cplusplus.com
如果 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 string \"%s\" into tokens:\n",str);
pch = strtok (str," ,.-");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ,.-"); // walk the stack?
}
return 0;
}
如果 str 没有以“\0”正确终止,是不是不可能
pch = strtok(NULL, " ,.-");
遍历堆栈?
谢谢!
c++ function, strtok() cplusplus.com
Will this example suffer from buffer overrun if str is not terminated properly?
/* 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;
}
If str isn't terminated correctly with "\0", isn't it possible for
pch = strtok (NULL, " ,.-");
to walk the stack?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果字符串不是以 null 结尾,大多数字符串处理函数都会离开末尾。
但是,在您的代码示例中,
str
已终止。Most string-handling functions will walk off the end if the string is not null-terminated.
However, in your code example,
str
is terminated.