标签: strncpy

为什么要使用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 */
printf( "destination1 is originally = '%s'\n", destination1 );
return_string = strncpy( destination1, source1, index );
printf( "After strncpy, destination1 becomes '%s'\n", destination1 );
Run Code Online (Sandbox Code Playgroud)

哪个产生了这个输出: …

c buffer-overflow c89 strcpy strncpy

77
推荐指数
5
解决办法
17万
查看次数

为什么strlcpy和strlcat被认为是不安全的?

据我所知,strlcpystrlcat被设计为安全的替代品strncpystrncat.然而,有些人仍然认为他们不安全,只是造成不同类型的问题.

有人可以举例说明如何使用strlcpystrlcat(即一个总是 null终止其字符串的函数)会导致安全问题吗?

Ulrich Drepper和James Antill表示这是事实,但从未提供实例或澄清这一点.

c security strncpy strlcpy

77
推荐指数
6
解决办法
6万
查看次数

为什么strncpy不为null终止?

strncpy()据说可以防止缓冲区溢出.但是如果它在没有null终止的情况下防止溢出,那么随后的字符串操作将会溢出.所以为了防止这种情况,我发现自己在做:

strncpy( dest, src, LEN );
dest[LEN - 1] = '\0';
Run Code Online (Sandbox Code Playgroud)

man strncpy 得到:

strncpy()函数类似,只是复制了不超过n个字节的src.因此,如果src的前n个字节中没有空字节,则结果将不会以空值终止.

没有null终止看似无辜的东西,如:

   printf( "FOO: %s\n", dest );
Run Code Online (Sandbox Code Playgroud)

......可能会崩溃


是否有更好,更安全的替代品strncpy()

c strncpy

71
推荐指数
7
解决办法
8万
查看次数

为什么strncpy不安全?

我想找出为什么strncpy被认为是不安全的.有没有人有关于此的任何文档或使用它的漏洞利用示例?

c c++ string strncpy

65
推荐指数
4
解决办法
5万
查看次数

strncpy和memcpy之间的区别?

我如何才能获得s[7]s

我没有发现strncpy和之间的任何区别memcpy.如果我想打印输出s,以及s[7](像qwertyA),我必须在以下代码中进行哪些更改:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    char s[10] = "qwerty", str[10], str1[10];
    s[7] = 'A';
    printf("%s\n",s);
    strncpy(str,s,8);
    printf("%s\n",str);
    memcpy(str1,s,8);
    printf("%s\n",str1);
    return 0;
}
/*
O/P
qwerty
qwerty
qwerty
*/
Run Code Online (Sandbox Code Playgroud)

c memcpy strncpy

18
推荐指数
2
解决办法
3万
查看次数

将零填充字节转换为UTF-8字符串

我正在压缩包含's'来自C的类型字段的几个结构.字段包含strncpy在C代码中处理的零填充UTF-8字符串(注意此函数的残留行为).如果我解码字节,我会得到一个包含大量NUL字符的unicode字符串.

>>> b'hiya\0\0\0'.decode('utf8')
'hiya\x00\x00\x00'
Run Code Online (Sandbox Code Playgroud)

我的印象是尾随零字节是UTF-8的一部分,并会自动删除.

删除零字节的正确方法是什么?

python unicode byte utf-8 strncpy

18
推荐指数
2
解决办法
2万
查看次数

在我的情况下strncpy或strlcpy

我应该怎么用,当我要复制src_strdst_arr,为什么?

char dst_arr[10];
char *src_str = "hello";
Run Code Online (Sandbox Code Playgroud)

PS:我的头正在读的东西很多关于如何纺纱后比我的电脑硬盘快strncpystrlcpy.

注意:我知道strlcpy无处不在.这不是问题所在.

c copy c-strings strncpy

17
推荐指数
2
解决办法
3万
查看次数

'strncpy'对''sprintf'

我可以sprintf在我的应用程序中看到许多用于复制字符串的内容.

我有一个字符数组:

char myarray[10];
const char *str = "mystring";
Run Code Online (Sandbox Code Playgroud)

现在,如果我想要将字符串复制strmyarray,最好使用:

sprintf(myarray, "%s", str);
Run Code Online (Sandbox Code Playgroud)

要么

strncpy(myarray, str, 8);
Run Code Online (Sandbox Code Playgroud)

c printf strncpy

15
推荐指数
1
解决办法
3万
查看次数

未初始化的值由堆分配创建

我一直在追逐这个bug,我只是不明白.我忘了一些基本的C或什么?

==28357== Conditional jump or move depends on uninitialised value(s)
==28357==    at 0x4C261E8: strlen (mc_replace_strmem.c:275)
==28357==    by 0x4E9280A: puts (ioputs.c:36)
==28357==    by 0x400C21: handlePath (myshell.c:105)
==28357==    by 0x400B17: handleInput (myshell.c:69)
==28357==    by 0x400AAD: acceptInput (myshell.c:60)
==28357==    by 0x4009CF: main (myshell.c:33)
==28357==  Uninitialised value was created by a heap allocation
==28357==    at 0x4C25153: malloc (vg_replace_malloc.c:195)
==28357==    by 0x400BDE: handlePath (myshell.c:99)
==28357==    by 0x400B17: handleInput (myshell.c:69)
==28357==    by 0x400AAD: acceptInput (myshell.c:60)
==28357==    by 0x4009CF: main (myshell.c:33)
==28357==

(095) void handlePath(char *input) { …
Run Code Online (Sandbox Code Playgroud)

c malloc valgrind pointers strncpy

13
推荐指数
1
解决办法
1万
查看次数

memcpy()和strncpy()有什么区别,因为后者很容易替代前者?

memcpy()和之间有什么重大区别strncpy()?我问这个是因为我们可以轻松地改变strncpy()以复制我们想要的任何类型的数据,而不仅仅是字符,只需将前两个非char*参数转换char*为第三个参数,并将第三个参数更改为非char类型的大小的倍数.在下面的程序中,我已经成功地使用它来将整数数组的一部分复制到其他程序中,并且它的效果一样好memcpy().

 #include <stdio.h>
    #include <string.h>


    int main ()
    {
    int arr[2]={20,30},brr[2]={33,44};
    //memcpy(arr,brr,sizeof(int)*1);
    strncpy((char*)arr,(char*)brr,sizeof(int)*1);
    printf("%d,%d",arr[0],arr[1]);

    }
Run Code Online (Sandbox Code Playgroud)

同样,我们可以使其适用于float其他数据类型.那么有什么重大区别memcpy()呢?

PS:另外,我想知道为什么memcpy()string.h头文件中,几乎所有的库函数都与字符串有关,而memcpy()在本质上更通用.任何原因?

c memcpy strncpy string.h

12
推荐指数
2
解决办法
9016
查看次数