使用scanf来防止缓冲区溢出... [英] Usage of scanf to prevent buffer overflow...

查看:337
本文介绍了使用scanf来防止缓冲区溢出...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好......这是一个很棒的论坛,

在我的一篇帖子中,有人告诉我使用输入功能更安全

with''A字段宽度说明符''...

所以我的问题,我应该使用哪个函数?。

" scanf("%s",start-> acNome);"

Hi all...this is a great forum,
In one of my posts, someone tell me that is more secure use input function
with ''A field-width specifier''...
So my question, which function i should use ?.
"scanf("%s",start->acNome);"

保证黑客使用>>任何
guarantee for hackers to kontaminate your mashine with >>viruses of any

的病毒来保护您的mashine

种。不要使用scanf或缓冲区溢出>>废弃你的mashine。


最好的问候..所有...并且度过愉快的一天(学术短语:-)))。 ...


kind. Don''t use scanf or buffer overflow >>ruins your mashine.

Best regards..all..and have a nice day(scholastic phrase :-)))....

推荐答案



写道:

wrote:
大家好.. 。这是一个很棒的论坛,
在我的一篇帖子中,有人告诉我使用''字段宽度说明符''输入
函数更安全...
所以我的问题,我应该使用哪种功能?。

" scanf("%s",start-> acNome);"
Hi all...this is a great forum,
In one of my posts, someone tell me that is more secure use input function with ''A field-width specifier''...
So my question, which function i should use ?.
"scanf("%s",start->acNome);"
保证黑客使用>>
guarantee for hackers to kontaminate your mashine with >>viruses of


的任何类型的病毒来保护您的mashine。不要使用scanf或缓冲区溢出>>废弃你的mashine。

最好的问候..所有...并且度过愉快的一天(学术短语: - )))....

any kind. Don''t use scanf or buffer overflow >>ruins your mashine.

Best regards..all..and have a nice day(scholastic phrase :-)))....



一般惯例是使用fgets而不是scanf来输入

字符串。

-

ISA


The general convention is to use fgets rather than scanf to enter
strings.
--
ISA


写道:

大家好...这是一个很棒的论坛,
在我的一篇帖子中,有人告诉我使用输入功能更安全
使用''字段宽度说明符''...
所以我的问题,我应该使用哪个函数?。

" scanf("%s",start-> acNome);"

Hi all...this is a great forum,
In one of my posts,
someone tell me that is more secure use input function
with ''A field-width specifier''...
So my question, which function i should use ?.

"scanf("%s",start->acNome);"




scanf适用于字符串输入,简单,

一旦你看到它是如何完成的。

在new.c中,输入字符超过LENGTH,将被丢弃。 />
rc可以赋值为EOF或0或1.

如果rc等于1,那么你在''array''中有一个字符串。

如果r c等于0,然后是读取的行,

只有一个换行符,

并且不能保证在''array''中有一个字符串。

如果rc等于EOF,那么在任何转换之前你输入的失败是



并且不能保证是' 'array''。


/ * BEGIN new.c * /


#include< stdio.h>


#define LENGTH 100

#define str(x)#x

#define xstr(x)str(x)


int main(无效)

{

int rc;

char数组[LENGTH + 1];


fputs("输入任何字符串:",stdout);

fflush(stdout);

rc = scanf(" ;%" xstr(LENGTH)" [^ \ n]%* [^ \ n]",array);

if(!feof(stdin)){

getchar();

}

while(rc == 1){

printf(" your string is%s \ n" ;,数组);

fputs("输入任何要继续的字符串,

或只需按Enter键结束程序:",

stdout);

fflush(stdout);

rc = scanf("%" xstr(LENGTH)" [^ \ n]%* [^ \ n]",array);

if(!feof(stdin)){

getchar();

}

}

返回0;

}


/ * END new。 c * /


-

pete



scanf is fine for string input, and easy,
once you''ve seen how it''s done.
In new.c, input characters beyond LENGTH, will be discarded.
rc can be assigned a value of EOF or 0 or 1.
If rc equals 1, then you have a string in ''array''.
If rc equals 0, then the line which was read,
only had a newline character,
and there is not guaranteed to be a string in ''array''.
If rc equals EOF, then you have an input failure occuring
before any conversion,
and there is not guaranteed to be a string in ''array''.

/* BEGIN new.c */

#include <stdio.h>

#define LENGTH 100
#define str(x) # x
#define xstr(x) str(x)

int main(void)
{
int rc;
char array[LENGTH + 1];

fputs("Enter any string: ", stdout);
fflush(stdout);
rc = scanf("%" xstr(LENGTH) "[^\n]%*[^\n]", array);
if (!feof(stdin)) {
getchar();
}
while (rc == 1) {
printf("Your string was %s\n", array);
fputs("Enter any string to continue, "
"or just hit the Enter key to end the program: ",
stdout);
fflush(stdout);
rc = scanf("%" xstr(LENGTH) "[^\n]%*[^\n]", array);
if (!feof(stdin)) {
getchar();
}
}
return 0;
}

/* END new.c */

--
pete


"" <>写道:
"" <> wrote:
大家好...这是一个很棒的论坛,
在我的一篇帖子中,有人告诉我使用输入功能更安全
''字段宽度说明符''...


不只是_more_ secure;使用字段宽度说明符是_only_

安全输入选项。好吧,有fgetc()和相关函数,但是你可以认为它们具有内置的,不可更改的字段宽度

说明符为1。 br />
所以我的问题,我应该使用哪种功能?


除了得到(),_但是正确使用它们。

" scanf("%s",start-> acNome);"
Hi all...this is a great forum,
In one of my posts, someone tell me that is more secure use input function
with ''A field-width specifier''...
Not just _more_ secure; using a field width specifier is the _only_
secure input choice. Well, there''s fgetc() and related functions, but
you can think of them as having a built-in, unchangeable field width
specifier of 1.
So my question, which function i should use ?.
Any but gets(), _but_ use them correctly.
"scanf("%s",start->acNome);"
保证黑客使用任何


类型的>>病毒来保护您的mashine。不要使用scanf或缓冲区溢出>>破坏你的mashine。


kind. Don''t use scanf or buffer overflow >>ruins your mashine.




那太强了。上面使用的scanf()_as保证在一个快乐的日子里给你一个

的缓冲区溢出问题。当正确使用

时,scanf()没有问题,即_with_字段说明符。例如,如果start-> acNome

是20个字符长,则scanf("%19s",start-> acNome);是安全的。


我会建议不要使用scanf(),但这只是因为使用

正确使用除可预测宽度数据之外的任何内容都很棘手,并且%s没有做大多数新手认为它做的事情。 fgets()更容易使用,并且具有

的优势,它_requires_,而不仅仅是允许,你指定一个

最大输入宽度。


Richard



That''s too strong. scanf() _as used above_ is guarantee to get you a
buffer overflow problem one happy day. scanf() is no problem when used
correctly, i.e., _with_ a field specifier. For example, if start->acNome
is 20 chars long, scanf("%19s", start->acNome); is safe.

I''d advise against scanf(), but only because it is tricky to use
correctly with any except predictable-width data, and %s does not do
what most newbies think it does. fgets() is much easier to use, and has
the advantage that it _requires_, not just allows, you to specify a
maximum input width.

Richard


这篇关于使用scanf来防止缓冲区溢出...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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