我需要重写该函数,使其不再容易受到堆栈缓冲区溢出的影响。
void hello (char *tag)
{
char inp [16];
printf("enter value for %s:", tag);
gets(inp);
printf("hello your %s is %s\n", tag, inp);
}
此外, 如何重写以下其他代码以避免缓冲区溢出:
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);
}
最佳答案
只需使用 fgets()而不是 gets()。它允许您限制存储在缓冲区中的字符数。
关于c - 如何重写以下函数,使其不再容易受到堆栈缓冲区溢出的影响?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4471306/