gcc 为什么以及如何发出 gets() 警告?

2024-01-10

while(1)
    {
        printf("\nEnter message : ");
        gets(message);

        //Send some data
        if( send(sock , message , strlen(message) , 0) < 0)
        {
            puts("Send failed");
            return 1;
        }

        //Receive a reply from the server
        if( recv(sock , server_reply , 2000 , 0) < 0)
        {
            puts("recv failed");
            break;
        }

        puts("Server reply :");
        puts(server_reply);
    }

    close(sock);
    return 0;
}

这是我的计划的一部分。当我编译并运行它时,出现错误。错误消息是

警告:gets 函数很危险,不应使用!


一个简单的谷歌搜索就会给出很多有用的信息,比如这个答案。

https://stackoverflow.com/a/1694042/2425366 https://stackoverflow.com/a/1694042/2425366

使用的缓冲区溢出示例gets

#include <stdio.h>
#include <string.h>

int main(void) {
    char buff[15];
    int pass = 0;
    printf("\n Enter the password : \n");
    gets(buff);
    if (strcmp(buff, "thegeekstuff")) {
        printf("\n Wrong Password \n");
    }
    else {
        printf("\n Correct Password \n");
        pass = 1;
    }
    if (pass) {
        /* Now Give root or admin rights to user*/
        printf("\n Root privileges given to the user \n");
    }
    return 0;
}

Input 1

thegeekstuff

Output

 Correct Password
 Root privileges given to the user

Input 2

abcdefghijklmnopqr    <-- stack smashing

Output

 Wrong Password
 Root privileges given to the user
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

gcc 为什么以及如何发出 gets() 警告? 的相关文章

随机推荐