Mac OS X和Linux中的安全字符串函数 [英] Safe String Functions In Mac OS X and Linux

查看:84
本文介绍了Mac OS X和Linux中的安全字符串函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Mac OSX和Linux中是否有与Windows中相同的安全字符串函数(如strcpy_s,strncpy_s..etc)?

are there are equivalent secure string functions in Mac OSX and Linux just like the ones in Windows (strcpy_s,strncpy_s..etc) ?

在多字节和宽字符之间转换的函数呢?

What about functions that convert between multi-byte and wide characters?

推荐答案

安全的字符串操作有两种策略. Linux/ glibc 维护人员拒绝添加安全功能,理由是您应保留字符串的长度并使用memcpy.

There are two strategies for safe string manipulation. The Linux / glibc maintainers refuse to add safe functions, arguing that you should keep the length of your strings at hand and use memcpy.

另一方面,Mac OSX包含BSD中的strlcpystrlcat. snprintf

On the other hand, Mac OSX includes strlcpy and strlcat from BSD. snprintf and asprintf can be used on both platforms to much the same effect:

size_t strlcpy(char *d, char const *s, size_t n)
{
    return snprintf(d, n, "%s", s);
}

size_t strlcat(char *d, char const *s, size_t n)
{
    return snprintf(d, n, "%s%s", d, s);
}

您还可以考虑使用BSD实现在此处找到.如果您的代码将在多个平台上编译,则可以使用预定义的库宏:

You could also consider using the BSD implementation found here. If your code will be compiled on multiple platforms, you can test for the presence of glibc using pre-defined library macros:

#if defined __GNU_LIBRARY__ || defined __GLIBC__

    size_t strlcpy(char *, char const *, size_t);
    size_t strlcat(char *, char const *, size_t);

#endif 

使用最容易处理字符编码之间的转换iconv 界面.

这篇关于Mac OS X和Linux中的安全字符串函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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