Tag: strncpy

为什么strncpy不能终止?

strncpy()可以防止缓冲区溢出。 但是,如果它阻止了没有null终止的溢出,那么所有的可能性都会导致后续的string操作溢出。 所以为了防止这个,我发现自己在做: strncpy( dest, src, LEN ); dest[LEN – 1] = '\0'; man strncpy给出: strncpy()函数是相似的,除了复制不超过n个字节的src。 因此,如果在src的前n个字节中没有空字节,结果将不会以空终止。 没有null终止一些看起来像无辜的东西: printf( "FOO: %s\n", dest ); …可能会崩溃。 有没有比strncpy()更安全的替代方法?

为什么strncpy不安全?

我正在寻找为什么strncpy被认为是不安全的。 有没有人有这样的文件或利用它的例子?

为什么strlcpy和strlcat认为不安全?

我知道strlcpy和strlcat被devise为strncpy和strncat安全替代品。 但是,有些人仍然认为他们是不安全的,只是造成不同types的问题 。 有人可以举一个例子,说明如何使用strlcpy或strlcat (即一个总是 null结束其string的函数)会导致安全问题? Ulrich Drepper和James Antill说这是真实的,但是从来没有提供实例或澄清这一点。

为什么要使用strncpy而不是strcpy?

编辑:我已经添加了该示例的源代码。 我遇到这个例子 : char source[MAX] = "123456789"; char source1[MAX] = "123456789"; char destination[MAX] = "abcdefg"; char destination1[MAX] = "abcdefg"; char *return_string; int index = 5; /* This is how strcpy works */ printf("destination is originally = '%s'\n", destination); return_string = strcpy(destination, source); printf("after strcpy, dest becomes '%s'\n\n", destination); /* This is how strncpy works */ […]