需要找到strncpy的目标缓冲区大小 [英] Need to find size of destination buffer for strncpy

查看:88
本文介绍了需要找到strncpy的目标缓冲区大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我们正在使用strcpy在我们的应用中复制字符串。当目标缓冲区不够大时,这给我们带来了问题

。作为一种解决方法,我们

想用strncpy替换对strcpy的调用。也就是说,将调用

替换为strcpy,例如my_strcpy(dest,src),它将在内部找到

目标缓冲区长度。


为此,我们需要知道目标缓冲区大小。对于静态

分配的字符串sizeof正确返回数组的长度

,但不是malloced字符串(char *)。因此我们无法用bncpy替换strcpy的调用以及适当的长度

参数。


还有其他方法吗?出来吗?更改应用程序中的所有静态和动态

分配非常困难(大约15k

实例将不得不更改)。


问候,

佛罗多

Hi all,
We are using strcpy to copy strings in our app. This gave us problems
when the destination buffer is not large enough. As a workaround, we
wanted to replace calls to strcpy with strncpy. That is, replace calls
to strcpy with say, my_strcpy(dest,src) which will internally find the
destination buffer length.

For this we need to know the destination buffer size. For statically
allocated strings sizeof is returning the length of the array
correctly, but not for malloced strings (char*). Hence we are not able
to replace calls to strcpy with strncpy with appropriate length
parameter.

Is there any other way out? Changing all the static and dynamic
allocations in the application is very very difficult (around 15k
instances will have to be changed).

Regards,
Frodo

推荐答案

2006年12月27日星期三23:09:48 -0600,Frodo Baggins写了

(文章< 11 ********************** @ n51g2000cwc.googlegroups .com>) :
On Wed, 27 Dec 2006 23:09:48 -0600, Frodo Baggins wrote
(in article <11**********************@n51g2000cwc.googlegroups .com>):

大家好,

我们正在使用strcpy在我们的应用程序中复制字符串。当目标缓冲区不够大时,这给我们带来了问题

。作为一种解决方法,我们

想用strncpy替换对strcpy的调用。也就是说,将调用

替换为strcpy,例如my_strcpy(dest,src),它将在内部找到

目标缓冲区长度。


为此,我们需要知道目标缓冲区大小。对于静态

分配的字符串sizeof正确返回数组的长度

,但不是malloced字符串(char *)。因此,我们无法用strncpy替换对strcpy的调用,并使用适当的长度

参数。
Hi all,
We are using strcpy to copy strings in our app. This gave us problems
when the destination buffer is not large enough. As a workaround, we
wanted to replace calls to strcpy with strncpy. That is, replace calls
to strcpy with say, my_strcpy(dest,src) which will internally find the
destination buffer length.

For this we need to know the destination buffer size. For statically
allocated strings sizeof is returning the length of the array
correctly, but not for malloced strings (char*). Hence we are not able
to replace calls to strcpy with strncpy with appropriate length
parameter.


man strlen

-

兰迪霍华德(2个删除FOOBAR)

精确观察的力量被那些没有得到它的人称为玩世不恭。 - George Bernard Shaw

man strlen
--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw




Randy Howard写道:

Randy Howard wrote:

2006年12月27日星期三23:09:48 -0600,Frodo Baggins写了

(文章< 11 ****************** ****@n51g​​2000cwc.googlegroups .com>):
On Wed, 27 Dec 2006 23:09:48 -0600, Frodo Baggins wrote
(in article <11**********************@n51g2000cwc.googlegroups .com>):

大家好,

我们正在使用strcpy在我们的应用中复制字符串。当目标缓冲区不够大时,这给我们带来了问题

。作为一种解决方法,我们

想用strncpy替换对strcpy的调用。也就是说,将调用

替换为strcpy,例如my_strcpy(dest,src),它将在内部找到

目标缓冲区长度。


为此,我们需要知道目标缓冲区大小。对于静态

分配的字符串sizeof正确返回数组的长度

,但不是malloced字符串(char *)。因此,我们无法用strncpy替换对strcpy的调用,并使用适当的长度

参数。
Hi all,
We are using strcpy to copy strings in our app. This gave us problems
when the destination buffer is not large enough. As a workaround, we
wanted to replace calls to strcpy with strncpy. That is, replace calls
to strcpy with say, my_strcpy(dest,src) which will internally find the
destination buffer length.

For this we need to know the destination buffer size. For statically
allocated strings sizeof is returning the length of the array
correctly, but not for malloced strings (char*). Hence we are not able
to replace calls to strcpy with strncpy with appropriate length
parameter.



这篇关于需要找到strncpy的目标缓冲区大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆