strtok - 缓冲区溢出

发布于 2024-09-16 18:20:30 字数 786 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北风几吹夏 2024-09-23 18:20:30

如果字符串不是以 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文