strcpy_s是否安全 [英] strcpy_s is it secure

查看:201
本文介绍了strcpy_s是否安全的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我们知道strcpy_s是作为strcpy的安全版本引入的.
它的签名是:

Hi we know strcpy_s has been introduced as a secure version of strcpy.
It''s signature is:

errno_t strcpy_s(
   char *strDestination,
   size_t numberOfElements,
   const char *strSource 
);


但是我感觉它仍然不安全.尽管我们指定了目标缓冲区的长度(例如numberOfElements),但请设想一下,如果有人指定了虚假的numberOfElements值,并将其设置为100,那么说说,实际上目标的大小是50吗?在这种情况下会发生什么?

你有什么意见?因此,我正在考虑评估其中包含


But I have a feeling it is still not secure. Although we specify length of destination buffer (e.g., numberOfElements) -- imagine if someone specifies false numberOfElements value, and sets it to 100 say, when the size of destination in reality, is 50? What happens in such a case?

What is your opinion? So I am thinking to evaluate code which contains

strcpy 

函数并且必须提供一些建议的代码.

functions inside it and must provide some recommendation.

推荐答案

只能避免这么大的愚蠢....这样说,它不能阻止您编写错误的代码,它可以防止您覆盖已定义的缓冲区...因此您应该知道大小(即是目标字符串或缓冲区的大小,而不是源字符串的大小).此功能只能防止较大的输入字符串溢出目标字符串.
You can only prevent so much stupidity.... with that said, it can''t stop you from writing bad code, it prevents you from overwriting a buffer which you''ve defined... so you should know the size (i.e. it''s the size of the destination string or buffer, not the originating string). This function only prevents a large input string from overflowing a destination string.


如席勒所说:反对愚蠢,众神本人徒然竞争."

对于无效输入,strcpy_s是不安全的.仍然值得使用它来代替旧的strcpy,因为它的更多是安全的.

没有绝对安全性可以防范所有可构想的无效参数,因此它不是安全的".它是更安全的".
As Schiller said: "Against stupidity the gods themselves contend in vain."

strcpy_s is not secure against invalid inputs. It''s still worth using instead of the old strcpy, since it''s more secure.

There''s no absolute security that guards against all concievable invalid parameters, so it''s not "secure". It''s "more secure".


事实上,标准的C库(功能等效)功能(即 ^ ]),它不是广告"为安全的.
As matter of fact, the standard C library (functionally equivalent) function (namely strncpy[^]) it is not ''advertised'' as secure.


这篇关于strcpy_s是否安全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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