请更正缓冲区溢出

发布于 2024-10-08 06:58:21 字数 695 浏览 0 评论 0原文

我想避免以下程序中的缓冲区溢出漏洞,

int main (int argc, char *argv[ ]) {    
  int valid=FALSE;     
  char str1[8];     
  char str2[8];      
  next_tag(str1);     
  gets(str2);    
  if (strncmp(str1,str2,8)==0)      
    valid=TRUE;     
  printf("buffer: str1(%s),str2(%s),valid(%d) \n", str1, str2, valid); 
} 

这是修复漏洞的正确修正吗?

int main (int argc, char *argv[ ]) {     
  int valid=FALSE;     
  char str1[8];     
  char str2[8];      
  next_tag(str1);     
  fgets(str2);       /* HERE IS THE CHANGE! */
  if (strncmp(str1,str2,8)==0)        
    valid=TRUE;     
  printf("buffer: str1(%s),str2(%s),valid(%d) \n", str1, str2, valid); 
} 

i want to avoid buffer overflow vulnerability in the following program,

int main (int argc, char *argv[ ]) {    
  int valid=FALSE;     
  char str1[8];     
  char str2[8];      
  next_tag(str1);     
  gets(str2);    
  if (strncmp(str1,str2,8)==0)      
    valid=TRUE;     
  printf("buffer: str1(%s),str2(%s),valid(%d) \n", str1, str2, valid); 
} 

is this the right correction to it in order to fix vulnerability?

int main (int argc, char *argv[ ]) {     
  int valid=FALSE;     
  char str1[8];     
  char str2[8];      
  next_tag(str1);     
  fgets(str2);       /* HERE IS THE CHANGE! */
  if (strncmp(str1,str2,8)==0)        
    valid=TRUE;     
  printf("buffer: str1(%s),str2(%s),valid(%d) \n", str1, str2, valid); 
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独夜无伴 2024-10-15 06:58:21
  fgets(str2, 8, STDIN);    

fgets 接受三个参数:

str

  • 指向字符数组的指针,其中
    读取的字符串被存储。

num

  • 最大字符数
    阅读(包括最后的
    空字符)。通常,长度
    使用作为 str 传递的数组的。

流 指向

  • FILE 对象的指针
    标识流所在位置
    从中读取字符。阅读
    从标准输入,stdin 可以是
    用于该参数。

请参阅此处

  fgets(str2, 8, STDIN);    

fgets takes three arguments:

str

  • Pointer to an array of chars where
    the string read is stored.

num

  • Maximum number of characters to be
    read (including the final
    null-character). Usually, the length
    of the array passed as str is used.

stream

  • Pointer to a FILE object that
    identifies the stream where
    characters are read from. To read
    from the standard input, stdin can be
    used for this parameter.

see here.

风吹雪碎 2024-10-15 06:58:21

我要指出的第一件事是为什么您的第一个实现会出现缓冲区溢出。

// Allocate a char array that can hold 'max' 8 characters.
char str2[8];

// Ask user for input and stuff it into str2. If the user
// gives us more than 8 characters, we will end up overwriting
// str2 beyond its allocated buffer. 
gets(str2);  

所以我们需要的是一种告诉“获取”的方法,获取不超过8个字符。 fgets 帮助我们解决了这个问题。它需要一个参数,即要读取的最大字符数。有关 fgets 的更多详细信息,请参阅 Vladimir 的帖子。

The first thing I should point out is why your first implementation has a buffer overflow.

// Allocate a char array that can hold 'max' 8 characters.
char str2[8];

// Ask user for input and stuff it into str2. If the user
// gives us more than 8 characters, we will end up overwriting
// str2 beyond its allocated buffer. 
gets(str2);  

So what we need is a way to tell 'gets', to get no more than 8 characters. fgets helps us solve this. It takes for a parameter, the maximum number of characters to read. Look at Vladimir's post for more details about fgets.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文