gcc为什么以及如何为gets()发出警告? [英] Why and how gcc emits warning for gets()?

查看:146
本文介绍了gcc为什么以及如何为gets()发出警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  while(1)
{
printf(\\\
Enter message:);
获得(消息);

//发送一些数据
if(发送(sock,message,strlen(message),0)< 0)
{
puts(发送失败);
返回1;
}

//接收服务器的回复
if(recv(sock,server_reply,2000,0)<0)
{
放(recv失败);
休息;
}

puts(服务器回复:);
puts(server_reply);
}

close(sock);
返回0;
}

这是我程序的一部分。当我编译并运行它时,出现错误。错误信息是


警告:gets函数是危险的,不应该使用它!



解决方案

关于这个问题:



gets 函数对于缓冲区溢出的风险是危险的,并且从标准 C 中丢失,按照 C11

FWIW,此警告不是由 gcc 发出的本身。很可能, glibc 包含一个编译器发出警告的编译指示。 Ref



关于错误:



您有 -Werror 在编译语句中启用,它基本上要求 gcc 将任何警告视为错误。 / p>

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;
}

This is part of my program. When I compile and run it, I get an error. The error's message is

warning: the gets function is dangerous and should not be used!

解决方案

Regarding the issue:

gets() function is dangerous for the risk of buffer overflow and dropped from the standard C, as per C11 standard. Compilers may support them for backward compatibility for legacy codes.

FWIW, this warning is not issued by gcc all by itself. Most likely, glibc contains a pragma which causes the compiler to emit the warning. Ref

Regarding the error:

You have -Werror enabled in your compilation statement, which basically asks gcc to treat any warning as error.

这篇关于gcc为什么以及如何为gets()发出警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆