如何安全地使用scanf()? [英] How to use scanf() safely?

查看:123
本文介绍了如何安全地使用scanf()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨。

在我使用scanf()之前,我必须为它内存malloc,如下所示:


//开始

char * buffer;


buffer = malloc(20);

scanf("%s",& buffer);

//结束


我们知道,如果输入30个字符,会发生一些不好的事情。

所以,我怎么能解决这个问题?

(我的意思是,无论你键入多少个字符,它都可以正常工作。)

解决方案

< blockquote> iwinux写道:


嗨。

在使用scanf()之前,我必须为它内存malloc,如下所示: br />

//开始

char * buffer;


buffer = malloc(20);

scanf("%s"& buffer);

//结束


我们知道,如果我输入30个字符,会发生一些不好的事情。

那么,我怎么能解决这个问题呢?

(我的意思是,无论你输入多少个字符,它都是c效果很好。)



不要使用scanf,而是使用类似fgets的东西。

-

==============

不是学生

==============


iwinux schrieb:


在使用scanf()之前,我必须为它内存malloc,就像这样:


//开始

char * buffer;


buffer = malloc(20);

scanf("%s",& buffer);

//结束


我们知道,如果我输入30个字符in,会发生一些不好的事情。

那么,我怎么能解决这个问题?

(我的意思是,无论你输入多少个字符,它都可以正常工作。 )



scanf()不能以安全的方式轻松使用。

请参阅过去的讨论和常见问题解答。

通常,只需在循环中使用fgets()(或getchar())。


返回scanf():

如果你有编译时间限制,你可以使用


#define stringize(s)#s

#define XSTR(s)stringize(s)

#定义BUFSIZE 20


char * buffer = malloc(BUFSIZE + 1);

if(buffer){

if(1 == scanf("%" XSTR(BUFSIZE)" s"& buffer){

do_something(buffer);

}

}


否则,你可以做

int len;

char * format;

char * buffer;


len = 1 + snprintf(0,0," %%% lus",bufSize);

if(len 0){

format = malloc(len);

buffer = malloc(bufSize + 1);

if(format&&缓冲区){

snprintf(格式,len," %%% lus",bufSize);

if(1 == scanf(format,buffer)){< br $> b $ b do_something(缓冲区);

}

}

}


干杯

Michael

-

电子邮件:我的是/ at / gmx / dot / de地址。


iwinux写道:


嗨。

在使用scanf()之前,我必须要malloc它的记忆,像这样:


//开始

char * buffer;


buffer = malloc (20);



if(buffer == NULL)...


scanf("%s", &安培;缓冲液);



scanf("%s",buffer); / * no& * /


//结束


我们知道,如果输入30个字符,就会发生不好的事情。

那么,我该如何解决这个问题呢?

(我的意思是,无论你键入多少个字符,它都可以正常工作。)


你可以做一整套不同的事情。

一个是告诉scanf()有多少空间:

scanf("%19s",buffer); / * 19 + 1 == 20 * /


这会阻止scanf()尝试存储字符

超出分配内存的末尾,但是它仍然不是很好的b $ b:如果你输入supercalifragilisticexpialidocious

,缓冲区将收到supercalifragilisti。并且零

字节,然后下一个输入操作将以

cexpial ...开头。如果你输入它是古代水手,

缓冲区将收到它和一个零字节,下一个

输入操作将以是一个......。


经验表明,scanf()对于交互式输入来说不是一个好的

函数。通常使用fgets()(不是使用gets(),

介意你!)然后从完整的<提取数据
一次读取一行更好br />
行,可能使用sscanf()。 fgets()有自己的设置

的问题,但它们通常比那些更复杂的scanf()更容易处理


< br $>
-

Eric Sosman
es *** **@acm-dot-org.inva 盖子


Hi.
Before I use scanf(), I must malloc the memory for it, like this:

//Start
char * buffer;

buffer = malloc(20);
scanf("%s", &buffer);
//End

As we know, if I type 30 characters in, something bad will happen.
So, how can I solve this problem?
(I mean, no matter how many charaters you type in, it can works well.)

解决方案

iwinux wrote:

Hi.
Before I use scanf(), I must malloc the memory for it, like this:

//Start
char * buffer;

buffer = malloc(20);
scanf("%s", &buffer);
//End

As we know, if I type 30 characters in, something bad will happen.
So, how can I solve this problem?
(I mean, no matter how many charaters you type in, it can works well.)

Don''t use scanf, use something like fgets instead.
--
==============
Not a pedant
==============


iwinux schrieb:

Before I use scanf(), I must malloc the memory for it, like this:

//Start
char * buffer;

buffer = malloc(20);
scanf("%s", &buffer);
//End

As we know, if I type 30 characters in, something bad will happen.
So, how can I solve this problem?
(I mean, no matter how many charaters you type in, it can works well.)

scanf() cannot easily be used in a safe manner.
See past discussions and the FAQ for this.
Usually, one just uses fgets() (or getchar() in a loop).

Back to scanf():
If you have compile time limits, you can use

#define stringize(s) #s
#define XSTR(s) stringize(s)
#define BUFSIZE 20

char *buffer = malloc(BUFSIZE+1);
if (buffer) {
if (1 == scanf("%"XSTR(BUFSIZE)"s", &buffer) {
do_something(buffer);
}
}

Otherwise, you can do
int len;
char *format;
char *buffer;

len = 1 + snprintf(0, 0, "%%%lus", bufSize);
if (len 0) {
format = malloc(len);
buffer = malloc(bufSize+1);
if (format && buffer) {
snprintf(format, len, "%%%lus", bufSize);
if (1 == scanf(format, buffer)) {
do_something(buffer);
}
}
}

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


iwinux wrote:

Hi.
Before I use scanf(), I must malloc the memory for it, like this:

//Start
char * buffer;

buffer = malloc(20);

if (buffer == NULL) ...

scanf("%s", &buffer);

scanf ("%s", buffer); /* no & */

//End

As we know, if I type 30 characters in, something bad will happen.
So, how can I solve this problem?
(I mean, no matter how many charaters you type in, it can works well.)

There''s a whole suite of different things you can do.
One is to tell scanf() how much space is available:

scanf ("%19s", buffer); /* 19 + 1 == 20 */

This will prevent scanf() from trying to store characters
beyond the end of the allocated memory, but it still isn''t
wonderful: If you type "supercalifragilisticexpialidocious"
the buffer will receive "supercalifragilisti" and a zero
byte, and then the next input operation will start with
"cexpial...". If you type "It is an Ancient Mariner" the
buffer will receive "It" and a zero byte, and the next
input operation will start with " is an...".

Experience suggests that scanf() is *not* a good
function for interactive input. It is often better to
read a line at a time with fgets() (not with gets(),
mind you!) and then extract data from the complete
line, possibly with sscanf(). fgets() has its own set
of problems, but they are usually easier to deal with
than those of the much more complex scanf().

--
Eric Sosman
es*****@acm-dot-org.invalid


这篇关于如何安全地使用scanf()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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