简体   繁体   English

strtok-缓冲区溢出

[英]strtok - buffer overrun

c++ function, strtok() cplusplus.com c ++函数, strtok() cplusplus.com

Will this example suffer from buffer overrun if str is not terminated properly? 如果未正确终止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;
}

If str isn't terminated correctly with "\\0", isn't it possible for 如果str不能以“ \\ 0”正确终止,则不可能

 pch = strtok (NULL, " ,.-"); 

to walk the stack? 走栈?

Thanks! 谢谢!

Most string-handling functions will walk off the end if the string is not null-terminated. 如果字符串不是以空字符结尾的,那么大多数字符串处理函数都会走到最后。

However, in your code example, str is terminated. 但是,在您的代码示例中, str被终止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM