C語言snprintf函數

來源:互聯網
上載者:User

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

函數說明:最多從源串中拷貝n-1個字元到目標串中,然後再在後面加一個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, then
len characters are stored in
buffer, a null-terminator is appended, and len is returned.

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

If len > count, then
count 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;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.