2013-01-15 80 views

回答

0

您应该担心wcslen,您应该担心的是strlen。这些函数需要C字符串,即指向由空字节(0x00)终止的字符序列的指针。如果不包括空字节,他们会很乐意继续遍历(你传递给他们运行超出任何缓冲的限制)内存:

const char s1[] = { 'a', 'b', 'c' }; /* Array of three characters, no null-terminator */ 
strlen(s1);      /* Unsafe, anything may happen. */ 

const char s2[] = "abc";    /* Array of three characters plus null-terminator */ 
strlen(s2);      /* Safe, returns '3'. */ 
+0

也许值得一提的是'strlen_s'和类似的安全功能都可以用于在缓冲区的大小已知时防止此问题。 –