2010-11-18 80 views
3

我有简单的测试程序在Linux上sprintf的错误用法?

#include <stdio.h> 
int main(int argc , char* argv[]) 
{ 
    unsigned int number=2048; 

    char* cpOut; 
    char cOut[4]; 
    cpOut=(char*)&cOut[0]; 
    printf("cOut address= %x \n",&cOut[0]); 
    printf("cpOut address = %x \n",cpOut); 

    sprintf(&cOut[0],"%d \n", number); 

    printf("cOut address= %x \n",&cOut[0]); 
    printf("cpOut address = %x \n",cpOut); 
}; 

试运行,GCC 4.3.4:在Solaris 10

[email protected] /tmp $ ./a.out 
cOut address= f9f41880 
cpOut address = f9f41880 
cOut address= f9f41880 
cpOut address = f9f41880 

试运行,太阳C++ 5.10:

bash-3.00$ ./a.out 
cOut address= 8047488 
cpOut address = 8047488 
cOut address= 8047488 
cpOut address = 8000a20 

任何人都可以请解释为什么通过调用sprintf函数覆盖指针cpOut?

回答

3

我认为这是一个缓冲区溢出的情况。尝试使COUT较大,也更安全的替代snprintf的sprintf的:

sprintf(&cOut[0],"%d \n", number); 

应改为

snprintf(cOut,sizeof(cOut),"%d \n", number); 
+1

非常感谢回答 – jano 2010-11-18 15:58:06

6

由于字符串"2048 \n"不适合char cOut[4];,你要创建一个缓冲区溢出。

+1

非常感谢您的回答 – jano 2010-11-18 15:56:44

4

您正在将7个字节(“2048 \ n”+ NUL)写入堆栈中大小为4的数组中。这将覆盖堆栈中3个字节的任何低于它的值,在本例中为cpOut。新值cpOut向您显示:第一个字节不变为0x08,然后接下来的3个字符串是您正在编写的字符串的最后三个字节:00(NUL),0a('\ n'),20('' )。

+1

非常感谢回答 – jano 2010-11-18 15:57:30

1

这一行:

sprintf(&cOut[0],"%d \n", number); 

写入7个字符: “2048 \ n \ 0”,但只对它们中的4空间。值0x8000a20包含(相反顺序):空格,换行和字符0.

+1

非常感谢回答 – jano 2010-11-18 15:58:30