安全的sprintf是啥? 是snprintf!!!可对比《基于C语言sprintf函数的深入理解》

https://blog.youkuaiyun.com/unsv29/article/details/83273398 这个链接是《基于C语言sprintf函数的深入理解》

可是不够安全,可能会指针溢出。网上有不少介绍,我也遇到过,所以找了keil里面的函数,找到了

如下:

 

    extern _ARMABI int snprintf(char *__restrict /*s*/, size_t /*n*/,
                                const char *__restrict /*format*/, ...) __attribute__((__nonnull__(3)));
    /*
     * is equivalent to fprintf, except that the argument s specifies an array
     * into which the generated output is to be written, rather than to a
     * stream. The argument n specifies the size of the output array, so as to
     * avoid overflowing the buffer.
     * A null character is written at the end of the characters written, even
     * if the formatting was not completed; it is not counted as part of the
     * returned sum. At most n characters of the output buffer are used,
     * _including_ the null character.
     * Returns: the number of characters that would have been written to the
     *          array, not counting the terminating null character, if the
     *          array had been big enough. So if the return is >=0 and <n, then
     *          the entire string was successfully formatted; if the return is
     *          >=n, the string was truncated (but there is still a null char
     *          at the end of what was written); if the return is <0, there was
     *          an error.
     */

这个函数不错。所以得将程序中的sprintf改为snprintf

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值