为什么危险? [英] Why is it dangerous?

查看:99
本文介绍了为什么危险?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

''晚上。


我不是C的新手,自从我8岁开始就一直在编程,但

here''sa奇怪的问题我以前从未见过。


当我使用Windows编译器从我们的C课程编译程序时

没有问题但是当我尝试用linux编译器编译它

它抱怨


a_03.c :(。text + 0x4d):警告:`gets''函数是危险的

,不应该使用。


linux比Windows更危险吗?我在哪里可以下载

非危险获取功能?我之前从未使用过get

某处存在未定义的行为?

这是我的任务中的一个精简的示例程序

演示了这个问题


#include< stdio.h>

#include< malloc.h>


void main ()

{

char * string;

printf(" enter string(max 2000 chars):");

fflush(stdin);

fflush(stdout);

string =(char *)malloc(2001);

if(!string)exit(1);

得到(字符串);

printf("你输入:%s \ n",string);

免费(字符串);

退出(0);

}


在使用TurboC和Windows的窗口上Lcc没有错误打印。在Linux上使用

gcc它说获取是危险的。


请告诉我的导师说gcc过于迂腐。

''evening.

I''m not new to C and have been programming in it since I was 8 but
here''s a strange problem I''ve never seen before.

When I compile a program from our C course with a windows compiler
there is no problem but when I try to compile it with a linux compiler
it complains that

a_03.c:(.text+0x4d): warning: the `gets'' function is dangerous
and should not be used.

Is linux more dangerous than windows? Where can I download a
non dangerous gets function? I have never used gets before is
there undefined behavior somewhere?
Here is a trimmed down example program from my assignment that
demonstrates the problem

#include <stdio.h>
#include <malloc.h>

void main()
{
char *string;
printf("enter string (max 2000 chars): ");
fflush(stdin);
fflush(stdout);
string = (char *)malloc(2001);
if(!string) exit(1);
gets(string);
printf("you entered: %s\n", string);
free(string);
exit(0);
}

On windows with TurboC and Lcc no error is printed. On linux with
gcc it says gets is dangerous.

Please advise my instructor says gcc is overly pedantic.

推荐答案

Julian说:
Julian said:

''晚上。


我是不是C新手,自从我8岁开始就一直在编程,但这是一个我以前从未见过的奇怪问题。


什么时候我用我的C编译器编译了一个带有windows编译器的程序

没有问题,但是当我尝试用linux编译器编译它时,它会抱怨

它会抱怨


a_03.c :(。text + 0x4d):警告:获取功能很危险

,不应该使用。

linux比Windows更危险吗?
''evening.

I''m not new to C and have been programming in it since I was 8 but
here''s a strange problem I''ve never seen before.

When I compile a program from our C course with a windows compiler
there is no problem but when I try to compile it with a linux compiler
it complains that

a_03.c:(.text+0x4d): warning: the `gets'' function is dangerous
and should not be used.

Is linux more dangerous than windows?



编号你的Linux编译器警告你一个危险的函数应该从不使用
。您的Windows编译器显然忘记了这样做。所以它可以说Windows b比Linux更危险。

No. Your Linux compiler warned you about a dangerous function that should
never be used. Your Windows compiler clearly forgot to do this. So it
could be argued that Windows is more dangerous than Linux.


我在哪里可以下载

非危险获得功能?
Where can I download a
non dangerous gets function?



无处。 gets()的功能由ISO定义;它需要一个指针

到缓冲区中的第一个字符,并将整个行从stdin

存储到该缓冲区中,*不管缓冲区的大小*! !没有安全的方式

来使用这样的功能。


相反,你可以使用fgets(),这是另一个标准的ISO C函数,它可以让/>
你指定缓冲区的大小,并且不会尝试在缓冲区中存储比你说的更合适的
。所以,如果你的尺寸合适,

fgets()并不危险。

Nowhere. The functionality of gets() is defined by ISO; it takes a pointer
to the first character in a buffer, and stores an entire line from stdin
into that buffer, *regardless of the buffer''s size*!! There is no safe way
to use such a function.

Instead, you can use fgets(), another standard ISO C function, which lets
you specify the size of the buffer, and which will not attempt to store
more in the buffer than you say will fit. So if you get the size right,
fgets() is not dangerous.


我之前从未使用过的是

某处有未定义的行为?
I have never used gets before is
there undefined behavior somewhere?



这取决于你的用户的表现如何(他们会限制自己

并且只输入你自己的字符数量)在你的缓冲区提供?),

但是最安全的做法是假设你永远不应该使用gets()。

It depends on how well-behaved your user is (will they restrain themselves
and only type as many characters as you''ve provided for in your buffer?),
but it''s safest to assume that you should never, ever, ever use gets().


这是我的作业中一个精简的示例程序,

演示了问题


#include< stdio.h>

#include< malloc.h>
Here is a trimmed down example program from my assignment that
demonstrates the problem

#include <stdio.h>
#include <malloc.h>



C没有该名称的标题(尽管有些实现可以)。对于malloc和free的

原型,#include< stdlib.hinstead。

C has no header by that name (although some implementations do). For the
prototypes for malloc and free, #include <stdlib.hinstead.


>

void main()
>
void main()



int main(void)

int main(void)


{

char * string;

printf(" enter string(max 2000 chars):");

fflush(stdin);
{
char *string;
printf("enter string (max 2000 chars): ");
fflush(stdin);



fflush的行为仅针对打开输出的流或

update定义,而stdin仅针对输入打开。简而言之,不要这样做。

The behaviour of fflush is defined only for streams open for output or
update, whereas stdin is open only for input. In short, Don''t Do That.


fflush(stdout);
fflush(stdout);



这很好,在这种情况下很有意义,因为你的printf字符串不是
以换行符结尾,所以你需要将数据从缓冲区刷新到输出

设备。

That''s fine, and meaningful in this case, because your printf string didn''t
end in a newline, so you need to flush data from the buffer to the output
device.


string =(char *)malloc(2001);
string = (char *)malloc(2001);



string = malloc(2001);将是完全足够的。你不需要

演员,事实上这是个坏主意。

string = malloc(2001); will be perfectly adequate. You do not need the
cast, and in fact it''s a bad idea.


if(!string)exit(1) ;
if(!string) exit(1);



更好:退出(EXIT_FAILURE);这个宏在< stdlib.hand has

可移植语义中定义。

Better: exit(EXIT_FAILURE); This macro is defined in <stdlib.hand has
portable semantics.


gets(string);
gets(string);



否,请改用:


if(fgets(string,2001,stdin)!= NULL)

{

No, use this instead:

if(fgets(string, 2001, stdin) != NULL)
{


printf("你输入:%s \ n",string);

免费(串);
printf("you entered: %s\n", string);
free(string);



}

}


exit(0);

}


在使用TurboC和Lcc的窗口上,不会打印错误。在Linux上用

gcc它说获取是危险的。


请告诉我的导师说gcc过于迂腐。
exit(0);
}

On windows with TurboC and Lcc no error is printed. On linux with
gcc it says gets is dangerous.

Please advise my instructor says gcc is overly pedantic.



你的导师很迂腐。 (所以gcc,除非你努力。)


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

Your instructor is underly pedantic. (So is gcc, unless you kick it hard.)

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


Julian写道:
Julian wrote:

>

请建议我的导师说gcc过于迂腐。
>
Please advise my instructor says gcc is overly pedantic.



正如理查德所说,除非你用

正确的选项调用gcc,否则情况正好相反。这就是为什么它有一个-dantant选项!


作为一个学习者使用gcc,你应该使用


gcc -ansi -Wall -pedantic


作为最低限度的选项。如果你的b $ b正在学习C99,请用'' - std = c99'代替''-ansi''。


-

Ian Collins。

As Richard said, the opposite is true unless you invoke gcc with the
correct options. That''s why it has a -pedantic option!

As a learner using gcc, you should use

gcc -ansi -Wall -pedantic

as a minimum set of options. Substitute ''-std=c99'' for ''-ansi'' if you
are learning C99.

--
Ian Collins.


8月9日,7:42 * pm,Julian< ju ** @nospam.invalidwrote:
On Aug 9, 7:42*pm, Julian <ju**@nospam.invalidwrote:

''晚上。


我不是C的新手,自从我8岁起就开始编程,但是

这是我以前从未见过的奇怪问题。


当我使用Windows编译器从C课程编译程序时

没有问题,但是当我尝试用linux编译器编译它时,它会抱怨


a_03.c :(。text + 0x4d):警告:`得到''功能很危险

并且不应该使用。


linux比Windows更危险吗?我在哪里可以下载

非危险获取功能?我之前从未使用过get

某处存在未定义的行为?


这是我的作业中的一个精简的示例程序

演示了这个问题


#include< stdio.h>

#include< malloc.h>


void main()

{

* * char * string;

* * printf(" enter string(max 2000 chars) :");

* * fflush(stdin);

* * fflush(stdout);

* * string =(char * )malloc(2001);

* * if(!string)exit(1);

* * gets(string);

* * printf(你输入:%s \ n,字符串);

* * free(字符串);

* * exit(0);


}


在使用TurboC和Lcc的窗口上,不会打印错误。在Linux上用

gcc它说获取是危险的。


请告诉我的导师说gcc过于迂腐。
''evening.

I''m not new to C and have been programming in it since I was 8 but
here''s a strange problem I''ve never seen before.

When I compile a program from our C course with a windows compiler
there is no problem but when I try to compile it with a linux compiler
it complains that

a_03.c:(.text+0x4d): warning: the `gets'' function is dangerous
and should not be used.

Is linux more dangerous than windows? Where can I download a
non dangerous gets function? I have never used gets before is
there undefined behavior somewhere?

Here is a trimmed down example program from my assignment that
demonstrates the problem

#include <stdio.h>
#include <malloc.h>

void main()
{
* * char *string;
* * printf("enter string (max 2000 chars): ");
* * fflush(stdin);
* * fflush(stdout);
* * string = (char *)malloc(2001);
* * if(!string) exit(1);
* * gets(string);
* * printf("you entered: %s\n", string);
* * free(string);
* * exit(0);

}

On windows with TurboC and Lcc no error is printed. On linux with
gcc it says gets is dangerous.

Please advise my instructor says gcc is overly pedantic.



(撇开其他人已经指出的代码中的所有错误

已经指出并将继续指出...)


它与操作系统无关,它与编译器无关,它与你的教练没有任何关系;它有与get()有关的
和仅有()(你不能得到一个更安全的

得到(),BTW)。问题是gets()无法知道传递给它的缓冲区的大小

,它将继续读取,直到

换行符。你分配了2001字节,这对于一行文本来说足够大了。

。但是......假设一个饼干进入你的程序并且

在终端上给你这行:


输入字符串(最多2000个字符):

11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 1111111111 1111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 1111111111111111111111 1111111111111111111111111111 111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 1


这些是2001个字符。你去了,饼干溢出你的

缓冲区。


塞巴斯蒂安

(Leaving aside all the errors in the code that other people have
already pointed out and will continue to point out...)

It has nothing to do with the operating system, it has nothing to do
with the compiler, it has nothing to do with your instructor; it has
to do with gets(), and gets() alone (and you can''t get a "safer"
gets(), BTW). The problem is that gets() has no way to know the size
of the buffer you pass to it, and it will continue to read until a
newline. You allocated 2001 bytes, which is reasonably large enough
for a line of text. But... suppose a cracker gets to your program and
gives you this line on the terminal:

enter string (max 2000 chars):
11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 1

Those are 2001 characters. There you go, the cracker overflowed your
buffer.

Sebastian


这篇关于为什么危险?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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