C語言snprintf函數

from:  http://blog.csdn.net/ccccdddxxx/article/details/7370169


int snprintf(char *restrict buf, size_t n, const char * restrict  format, ...);

函數說明:最多從源串中拷貝n1個字符到目標串中,然後再在後面加一個0。所以如果目標串的大小爲n

                 的話,將不會溢出。

函數返回值:若成功則返回欲寫入的字符串長度,若出錯則返回負值。

Result1(推薦的用法)

#include <stdio.h>
#include <stdlib.h>

int main()
{
     char str[10]={0,};
     snprintf(str, sizeof(str), "0123456789012345678");
     printf("str=%s\n", str);
     return 0;
}

root] /root/lindatest
$ ./test
str=012345678

Result2:(不推薦使用)

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char str[10]={0, };
    snprintf(str, 18, "0123456789012345678");
    printf("str=%s\n", str);
    return 0;
}

root] /root/lindatest
$ ./test
str=01234567890123456

snprintf函數返回值的測試:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char str1[10] ={0, };
    char str2[10] ={0, };
    int ret1=0,ret2=0;
    ret1=snprintf(str1, sizeof(str1), "%s", "abc");
    ret2=snprintf(str2, 4, "%s", "aaabbbccc");
    printf("aaabbbccc length=%d\n", strlen("aaabbbccc"));
    printf("str1=%s,ret1=%d\n", str1, ret1);
    printf("str2=%s,ret2=%d\n", str2, ret2);
    return 0;
}

[root] /root/lindatest
$ ./test
aaabbbccc length=9
str1=abc,ret1=3
str2=aaa,ret2=9

***************************************************************************

關於sprintf和snprintf的正確使用。

考慮以下有缺陷的例子:
void f(const char *p)
{
    char buf[11]={0};
    sprintf(buf,"%10s",p); // very dangerous
    printf("%sn",buf);
}

不要讓格式標記“%10s”誤導你。如果p的長度大於10個字符,那麼sprintf() 的寫操作就會越過buf的邊界,從而產生一個緩衝區溢出。
檢測這類缺陷並不容易,因爲它們只在 p 的長度大於10個字符的時候纔會發生。黑客通常利用這類脆弱的代碼來入侵看上去安全的系統。

要修正這一缺陷,可以使用函數snprintf()代替函數sprintf()。

函數原型:int snprintf(char *dest, size_t n, const char *fmt, ...);
函數說明: 最多從源串中拷貝n-1個字符到目標串中,然後再在後面加一個0。所以如果目標串的大小爲n的話,將不會溢出。
函數返回值: 若成功則返回存入數組的字符數,若編碼出錯則返回負值。

推薦的用法:
void f(const char *p)
{
    char buf[11]={0};
    snprintf(buf, sizeof(buf), "%10s", p); // 注意:這裏第2個參數應當用sizeof(str),而不要使用硬編碼11,也不應當使用sizeof(str)-1或10
    printf("%sn",buf);

**************************************************************************

衆所周知,sprintf不能檢查目標字符串的長度,可能造成衆多安全問題,所以都會推薦使用snprintf.

snprintf(_snprintf)的聲明是這樣的

int _snprintf(
   char *buffer,
   size_t count,
   const char *format [,
      argument] ...
);

If len < count, thenlen characters are stored in buffer, a null-terminator is appended, and len is returned.

If len = count, thenlen characters are stored in buffer, no null-terminator is appended, and len is returned.

If len > count, thencount characters are stored in buffer, no null-terminator is appended, and a negative value is returned.


最常見的錯誤用法有:
1.
char sa[256]={0};
_snprintf(sa,sizeof(sa),"%s",sb);
//錯誤原因:當sb的長度>=256的時候,sa將沒有'\0'結尾

2.
char sa[256];
_snprintf(sa,sizeof(sa)-1,"%s",sb);
//錯誤原因:當sb的長度>=255的時候,sa將沒有'\0'結尾,忘記給sa初始化

3.
char sa[256];
_snprintf(sa,sizeof(sa)-1,"%s",sb);
sa[sizeof(sa)]=0;
//錯誤原因:最後一行數組越界

正確的用法
1. //推薦用法
char sa[256];
sa[sizeof(sa)-1]=0;
_snprintf(sa,sizeof(sa),"%s",sb);
if(sa[sizeof(sa)-1]!=0)
{
   printf("warning:string will be truncated");
   sa[sizeof(sa)-1]=0;
}

2.
char sa[256]={0};
int result = _snprintf(sa,sizeof(sa),"%s",sb);
if(result==sizeof(sa) || result<0)
{
    printf("warning:sting will be truncated");
   sa[sizeof(sa)-1]=0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章