为什么我不能用字符串错误? [英] Why can't I use strerror?

查看:210
本文介绍了为什么我不能用字符串错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我移植了一些code到Windows,而微软编译器(的Visual C ++ 8)告诉我,字符串错误()是不安全的。

I'm porting some code to Windows, and the Microsoft compiler (Visual C++ 8) is telling me that strerror() is unsafe.

抛开烦恼的因素都来自微软安全的字符串的东西,我可以清楚地看到,一些去precated功能是很危险的。但我不明白这可能是错误的字符串错误()。它需要一个code( INT ),如果是code不知道返回相应的字符串或空字符串。

Putting aside the annoyance factor in all the safe string stuff from Microsoft, I can actually see that some of the deprecated functions are dangerous. But I can't understand what could be wrong with strerror(). It takes a code (int), and returns the corresponding string, or the empty string if that code is not known.

哪里是危险的?

有没有用C一个很好的选择?

Is there a good alternative in C?

有没有用C一个很好的选择+?

Is there a good alternative in C++?

有过一些很好的答案,现在了解了一些实现可能会疯狂到实际写入到一个共享缓存 - 不安全的重入单一线程中,线程之间没关系! - 我的问题不再是为什么我不能用它,有什么办法?以是否有C和/或C ++任何像样的,简洁的办法?

Having had some good answers, and now understanding that some implementations may be crazy enough to actually write to a common shared buffer - unsafe to reentrancy within a single-thread, never mind between threads! - my question stops being "Why can't I use it, and what are the alternatives?" to "Are there any decent, succinct alternatives in C and/or C++?"

在此先感谢

推荐答案

字符串错误是pcated因为它不是线程安全的德$ P $。 于strerror 工作在内部的静态缓冲器,其可以通过其他,并发线程被覆盖。您应该使用一种叫做安全的变种 strerror_s

strerror is deprecated because it's not thread-safe. strerror works on an internal static buffer, which may be overwritten by other, concurrent threads. You should use a secure variant called strerror_s.

的安全变体要求的缓冲区大小被传递给函数,以验证该缓冲器是足够大的写入它,帮助避免缓冲区溢出,可能使恶意code键执行之前。

The secure variant requires that the buffer size be passed to the function in order to validate that the buffer is large enough before writing to it, helping to avoid buffer overruns that could allow malicious code to execute.

这篇关于为什么我不能用字符串错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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