gets()fgets()with subdivide.c tilepack.c squarect.c [英] gets() fgets() with subdivide.c tilepack.c squarect.c

查看:61
本文介绍了gets()fgets()with subdivide.c tilepack.c squarect.c的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望使用Ken Stephenson的CirclePack计划

http://www.math.utk.edu/~kens/ )有一些平铺程序由

Cannon,Floyd,Parry编写。我想要使​​用的程序是subdivide.c

tilepack.c squarect.c。它们可从
http: //www.math.vt.edu/people/floyd/...re/subdiv.html

我尝试在我的Redhat 9盒子上编译它们,例如cc subdivide.c。当我执行

时,我会收到类似函数`Readtilingforvertex''的错误:

:`gets''函数很危险,不应该使用。


我检查了常见问题解答,并说gets()很糟糕,我应该使用fgets()

代替。我找到了一个代码片段的例子,例如

''这是你得到的代码类型:


{

char buf [512];

得到(buf);

/ * ...更多代码... * /

}


如果放置超过512个字符,将导致缓冲区溢出

缓冲区中随机覆盖其他内存。这可能导致一些难以追踪的错误,这只是一个危险的习惯用法。


这也是BIND中漏洞的根源和一个巨大的

其他程序的数量 - 通过向缓冲区提供正确的序列,你实际上可以让它执行缓存中的代码 - 尽管我不知道'$

认为人们会使用CCP4进入UNIX盒子。


替换它的代码非常简单:

{

char buf [512];

fgets(buf,sizeof(buf),stdin);

/ * ...更多代码...... * /

}


这两个函数原型所需的头文件仍然是

stdio.h''


所以不是ac程序员我用上面的

替换了所有的gets()实例。现在我得到了不同的错误。

[stuart @ localhost cpack] $ cc subdivide.c

subdivide.c:在函数`Readrules'':

subdivide.c:160:在sizeof之前解析错误

subdivide.c:160:stdin的冲突类型

/ usr / include / stdio.h:142:先前声明`stdin''

subdivide.c:160:在'''之前解析错误'''令牌

subdivide.c:167:警告:传递`fgets''的arg 3使得指针来自整数而不使用强制转换

subdivide.c:在函数`Readtiling'中:

subdivide.c:311:在sizeof之前解析错误

subdivide.c:311:'stdin''的冲突类型

/usr/include/stdio.h:142:之前的声明`stdin''

subdivide.c:311:解析前错误'''''令牌

subdivide.c:318:警告:传递`fgets'的arg 3 '在没有强制转换的情况下从整数中生成指针

subdivide.c:在函数`Readtypet中iling'':

subdivide.c:461:在sizeof之前解析错误

subdivide.c:461:`stdin''的冲突类型

/usr/include/stdio.h:142:先前声明`stdin''

subdivide.c:461:''之前的解析错误'''令牌

subdivide.c:在函数`Readbdytiling'':

subdivide.c:596:在sizeof之前解析错误

subdivide.c:596:冲突`stdin'的类型'

/usr/include/stdio.h:142:先前的'stdin''声明

subdivide.c:596:解析错误之前' ')''令牌

subdivide.c:在函数`Subdivide'':

subdivide.c:719:在sizeof之前解析错误

subdivide.c:719:`stdin''的冲突类型

/usr/include/stdio.h:142:之前的'stdin''声明

subdivide.c:719:''之前的解析错误'''令牌

subdivide.c:在函数`Writetilingtofile'中:

subdivide.c:972:在sizeof之前解析错误

subdivide.c:972:stdin的冲突类型

/ usr /include/stdio.h:142:以前声明`stdin''

subdivide.c:972:在'''之前解析错误'''令牌

subdivide.c :979:警告:传递'fgets'的arg 3'使得整数指针没有投射


任何想法?


谢谢,


Stuart

I am looking to use Ken Stephenson''s CirclePack program
(http://www.math.utk.edu/~kens/) with some tiling programs written by
Cannon, Floyd, Parry. The programs I want to use are subdivide.c
tilepack.c squarect.c . They are available from
http://www.math.vt.edu/people/floyd/...re/subdiv.html.

I tried compiling them on my Redhat 9 box, eg cc subdivide.c. When I do
so I get errors like "In function `Readtilingforvertex'':
: the `gets'' function is dangerous and should not be used."

I checked the FAQ and is says gets() is BAD and I should use fgets()
instead. I found an example of a code fragment to use, eg
''This is the sort of code you''ve got:

{
char buf[512];
gets( buf );
/* ... more code ... */
}

which will cause a buffer overrun if more than 512 characters are placed
in the buffer and randomly overwrite other memory. This can cause a
number of hard-to-trace bugs and is just a dangerous idiom.

This is also the source of the the vulnerabilities in BIND and a huge
number of other programs--by feeding the buffer the right sequence, you
can actually get it execute code buried in the buffer---though I don''t
think people will use CCP4 to break into UNIX boxes.

The code to replace it is really simple:

{
char buf[512];
fgets( buf, sizeof(buf), stdin );
/* ... more code ... */
}

The header file needed for the prototypes of both functions is still
stdio.h''

So not being a c programmer I just replaced all instances of gets() with
with the above. Now I am getting different errors.
[stuart@localhost cpack]$ cc subdivide.c
subdivide.c: In function `Readrules'':
subdivide.c:160: parse error before "sizeof"
subdivide.c:160: conflicting types for `stdin''
/usr/include/stdio.h:142: previous declaration of `stdin''
subdivide.c:160: parse error before '')'' token
subdivide.c:167: warning: passing arg 3 of `fgets'' makes pointer from integer without a cast
subdivide.c: In function `Readtiling'':
subdivide.c:311: parse error before "sizeof"
subdivide.c:311: conflicting types for `stdin''
/usr/include/stdio.h:142: previous declaration of `stdin''
subdivide.c:311: parse error before '')'' token
subdivide.c:318: warning: passing arg 3 of `fgets'' makes pointer from integer without a cast
subdivide.c: In function `Readtypetiling'':
subdivide.c:461: parse error before "sizeof"
subdivide.c:461: conflicting types for `stdin''
/usr/include/stdio.h:142: previous declaration of `stdin''
subdivide.c:461: parse error before '')'' token
subdivide.c: In function `Readbdytiling'':
subdivide.c:596: parse error before "sizeof"
subdivide.c:596: conflicting types for `stdin''
/usr/include/stdio.h:142: previous declaration of `stdin''
subdivide.c:596: parse error before '')'' token
subdivide.c: In function `Subdivide'':
subdivide.c:719: parse error before "sizeof"
subdivide.c:719: conflicting types for `stdin''
/usr/include/stdio.h:142: previous declaration of `stdin''
subdivide.c:719: parse error before '')'' token
subdivide.c: In function `Writetilingtofile'':
subdivide.c:972: parse error before "sizeof"
subdivide.c:972: conflicting types for `stdin''
/usr/include/stdio.h:142: previous declaration of `stdin''
subdivide.c:972: parse error before '')'' token
subdivide.c:979: warning: passing arg 3 of `fgets'' makes pointer from integer without a cast

Any ideas?

thanks ,

Stuart

推荐答案

cc subdivide.c

细分.c:在函数Readrules中:

subdivide.c:160:在sizeof之前解析错误

subdivide.c:160:`stdin的冲突类型''

/usr/include/stdio.h:142:先前声明`stdin''

subdivide.c:160:在'''之前解析错误'''令牌

subdivide.c:167:警告:传递`fgets''的arg 3使得整数指针不带演员

subdivide.c:在func中'Readtiling'':

subdivide.c:311:在sizeof之前解析错误

subdivide.c:311:'stdin''的冲突类型

/usr/include/stdio.h:142:先前声明`stdin''

subdivide.c:311:在'''之前解析错误''令牌

subdivide.c:318:警告:传递`fgets''的arg 3使得整数指针没有强制转换

subdivide.c:在函数`Readtypetiling''中:

subdivide.c:461:在sizeof之前解析错误

subdivide.c:461:stdin的冲突类型

/ usr / include / stdio.h:142:以前声明`stdin''

subdivide.c:461:''之前的解析错误'''令牌

subdivide.c:在函数`Readbdytiling'':

subdivide.c:596:在sizeof之前解析错误

subdivide.c:596:`stdin''的冲突类型

/usr/include/stdio.h:142:之前的'stdin''声明

subdivide.c:596:在'''之前解析错误''令牌

subdivide.c:在函数`Subdivide''中:

subdivide.c:719 :在sizeof之前解析错误

subdivide.c:719:stdin的冲突类型

/usr/include/stdio.h:142:之前的声明'stdin''

subdivide.c:719:''之前的解析错误'''令牌

subdivide.c:在函数`Writetilingtofile'中:

subdivide.c:972:在sizeof之前解析错误

subdivide.c:972:stdin的冲突类型

/ usr / include / stdio.h:142:先前声明`stdin''

subdivide.c:972:''之前的解析错误'''令牌

subdivide.c: 979:警告:传递'fgets'的arg 3'使得整数指针没有投射


任何想法?


谢谢,


Stuart

cc subdivide.c
subdivide.c: In function `Readrules'':
subdivide.c:160: parse error before "sizeof"
subdivide.c:160: conflicting types for `stdin''
/usr/include/stdio.h:142: previous declaration of `stdin''
subdivide.c:160: parse error before '')'' token
subdivide.c:167: warning: passing arg 3 of `fgets'' makes pointer from integer without a cast
subdivide.c: In function `Readtiling'':
subdivide.c:311: parse error before "sizeof"
subdivide.c:311: conflicting types for `stdin''
/usr/include/stdio.h:142: previous declaration of `stdin''
subdivide.c:311: parse error before '')'' token
subdivide.c:318: warning: passing arg 3 of `fgets'' makes pointer from integer without a cast
subdivide.c: In function `Readtypetiling'':
subdivide.c:461: parse error before "sizeof"
subdivide.c:461: conflicting types for `stdin''
/usr/include/stdio.h:142: previous declaration of `stdin''
subdivide.c:461: parse error before '')'' token
subdivide.c: In function `Readbdytiling'':
subdivide.c:596: parse error before "sizeof"
subdivide.c:596: conflicting types for `stdin''
/usr/include/stdio.h:142: previous declaration of `stdin''
subdivide.c:596: parse error before '')'' token
subdivide.c: In function `Subdivide'':
subdivide.c:719: parse error before "sizeof"
subdivide.c:719: conflicting types for `stdin''
/usr/include/stdio.h:142: previous declaration of `stdin''
subdivide.c:719: parse error before '')'' token
subdivide.c: In function `Writetilingtofile'':
subdivide.c:972: parse error before "sizeof"
subdivide.c:972: conflicting types for `stdin''
/usr/include/stdio.h:142: previous declaration of `stdin''
subdivide.c:972: parse error before '')'' token
subdivide.c:979: warning: passing arg 3 of `fgets'' makes pointer from integer without a cast

Any ideas?

thanks ,

Stuart




2004年1月21日星期三,Stuart Anderson wro te:

On Wed, 21 Jan 2004, Stuart Anderson wrote:

我希望使用Ken Stephenson的CirclePack计划
http://www.math.utk.edu/~kens/ )有一些由Cannon,Floyd,Parry编写的平铺程序。我想要使​​用的程序是subdivide.c
tilepack.c squarect.c。它们可从
http: //www.math.vt.edu/people/floyd/...re/subdiv.html


哎呀,那是旧代码!我真的希望科学类型的人能够学习现代语言;它会让人们的生活变得更轻松

就像你我一样。不幸的是,今天的许多算法仍然是以莎士比亚英语的编程等价物形式呈现,

a.k.a.K& R C.来自subdivide.c的长引用:


Readrules()

{/ *循环变量* /

int i,j ,k;


/ *标准输入变量和函数* /

char s [256];

extern char * gets ();

extern int atoi();


FILE * fp; / *将此移动到变量声明部分* /

fprintf(stderr,读取哪个规则文件?(例如,filename.r)\ n);

得到(s);

if((fp = fopen(s," r"))== NULL)

{

fprintf(stderr,无法打开文件\ n);

退出();

}

[...]

在现代标准C中,这将写成如下:


#include< stdio.h>

#include < stdlib.h>


int Readrules(无效)

{

int i,j,k;

char s [256];

FILE * fp;


fprintf(stderr,读取哪个规则文件?(例如,文件名.r)\ n");

fgets(s,sizeof s,stdin);

if((fp = fopen(s," r")) == NULL)

{

fprintf(stderr,无法打开文件\ n);

退出(EXIT_FAILURE); < br $>
}

[...]


注意正确使用'' fgets''在上面。这就是你的代码

如果你想让''cc''停止给你错误的话。 (通过

的方式,''cc''通常是编译器的别名,通常称为

''gcc'';这是你应该知道的如果你读了这个小组。

和clc推荐的调用''gcc''的方法是


gcc -W -Wall -ansi -pedantic - O2细分。


它的时间更长,但就帮助和

诊断信息来说它会更好,更好你。)


我尝试在我的Redhat 9盒子上编译它们,例如cc subdivide.c。当我这样做时,我得到的错误就像在函数中`Readtilingforvertex'':
:`gets''函数很危险,不应该使用。

我检查了常见问题解答,并说gets()是坏的,我应该使用fgets()



那是对的。 [剪了很长的常见问题报价,所有这些都是正确的,

当然。]

所以不是ac程序员我只是用 [stuart @ localhost cpack]

I am looking to use Ken Stephenson''s CirclePack program
(http://www.math.utk.edu/~kens/) with some tiling programs written by
Cannon, Floyd, Parry. The programs I want to use are subdivide.c
tilepack.c squarect.c . They are available from
http://www.math.vt.edu/people/floyd/...re/subdiv.html.
Yuck, that''s old code! I really wish science-type people would
learn the modern language; it would make life a lot easier for people
like you and me. Unfortunately, a lot of algorithms today are still
being presented in the programming equivalent of Shakespearean English,
a.k.a. "K&R C." A long quote from subdivide.c:

Readrules()
{ /* loop variables */
int i,j,k;

/* standard input variable and functions */
char s[256];
extern char *gets();
extern int atoi();

FILE *fp; /* move this to the variable declaration section */
fprintf(stderr," Read which rules file? (e.g., filename.r)\n");
gets(s);
if ((fp = fopen(s,"r")) == NULL)
{
fprintf(stderr," cannot open file \n");
exit();
}
[...]
In modern standard C, this would be written something like this:

#include <stdio.h>
#include <stdlib.h>

int Readrules(void)
{
int i, j, k;
char s[256];
FILE *fp;

fprintf(stderr, " Read which rules file? (e.g., filename.r)\n");
fgets(s, sizeof s, stdin);
if ((fp = fopen(s, "r")) == NULL)
{
fprintf(stderr, " cannot open file \n");
exit(EXIT_FAILURE);
}
[...]

Note the correct use of ''fgets'' above. This is what your code
should look like if you want ''cc'' to stop giving you errors. (By
the way, ''cc'' is usually an alias for the compiler usually known as
''gcc''; that''s something you should know if you read this group.
And the c.l.c-recommended way to invoke ''gcc'' is

gcc -W -Wall -ansi -pedantic -O2 subdivide.c

It''s longer, but it''s much, much better in terms of the help and
diagnostic messages it will give you.)

I tried compiling them on my Redhat 9 box, eg cc subdivide.c. When I do
so I get errors like "In function `Readtilingforvertex'':
: the `gets'' function is dangerous and should not be used."

I checked the FAQ and is says gets() is BAD and I should use fgets()
instead.
That''s right. [Snipped the long FAQ quote, all of which is correct,
of course.]
So not being a c programmer I just replaced all instances of gets() with
with the above. Now I am getting different errors.
[stuart@localhost cpack]


cc subdivide.c
subdivide.c:在函数`Readrules'中:
subdivide.c:160:在sizeof之前解析错误
cc subdivide.c
subdivide.c: In function `Readrules'':
subdivide.c:160: parse error before "sizeof"




如果你实际上至少发布了它将会有很大的帮助

a编译器抱怨的几行代码。

例如,你可以说,我的副本中的第160行>
是我在下面标记的行:


char s [256];

extern char * fgets(buf,sizeof buf,stdin) ; / **这一行** /

extern int atoi();


",然后我们可以告诉你,你不是应该这样做。

这是我对这些错误信息意味着什么的最好猜测;因为你没有告诉我们你想要编译什么,我真的无法知道我的诊断是否正确。


[剪掉了更多无用的错误信息]


看看你是否能弄清楚我的非莎士比亚是怎样的。示例代码

使用fgets()工作,然后相应地修改您的代码。如果你仍然有麻烦,请发布最小的完整,可编辑的程序

,它仍会显示错误,我们会看看。


-Arthur



It would have helped a whole lot if you''d actually posted at least
a few of the lines of code about which the compiler was complaining.
For example, you could have said, "Line 160 in my copy of subdivide.c
is the line I''ve marked below:

char s[256];
extern char *fgets(buf, sizeof buf, stdin); /** THIS LINE **/
extern int atoi();

", and then we could have told you that you''re not supposed to do that.
That''s my best guess as to what these error messages mean; since you
haven''t told us what you tried to compile, I really have no way of
knowing whether my diagnosis is right or not.

[snipped a lot more useless error messages]

See if you can figure out how my "non-Shakespearean" sample code
using fgets() works, and then modify your code accordingly. If you
still have trouble, post the smallest complete, compilable program
that still exhibits the bug, and we''ll take a look.

-Arthur


这篇关于gets()fgets()with subdivide.c tilepack.c squarect.c的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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