Qry:fgets的行为 - ? [英] Qry : Behaviour of fgets -- ?

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

问题描述



机器1:

bash-3.00 $ uname -a

SunOS< hostname5.10 Generic_118822-30 sun4u sparc SUNW, Sun-Fire-280R

bash-3.00 $ gcc -v

从/ usr / local / lib / gcc-lib / sparc-sun-阅读规格solaris2.8 / 2.95.3 /

规格

gcc版本2.95.3 20010315(发布)

机器2:

bash-2.05b $ uname -a

Linux< hostname2.4.21-4.EL#1 Fri Oct 3 18:13:58 EDT 2003 i686 i686

i386 GNU / Linux

bash-2.05b $ gcc -v

从/ usr / lib / gcc-lib / i386-redhat-linux读取规范/3.2.3/specs

配置:../ configure --prefix = / usr --mandir = / usr / share / man -

infodir = / usr / share / info --enable-shared --enable-threads = posix -

disable-checking --with-system-zlib --enable -__ cxa_atexit --host = i386-

redhat-linux

线程模型:posix

gcc版本3.2.3 20030502(Red Hat Linux 3.2.3-20)


bash-2.05b $ cat fgets_ fail_test.c

#include< stdio.h>

#include< string.h>

#include< stdlib.h>


/ *演示失败的Raxit Sheth& Gaurav Gupta * /

int main()

{

char spcontent [100],sendrepfile [100];

FILE * spfp = NULL;

memset(sendrepfile,0,100);

strcpy(sendrepfile," / tmp / filenotexists");

if((spfp = fopen(sendrepfile," r"))==(FILE *)NULL)

{

printf("打开文件%s",sendrepfile中的错误; fflush(stdout);

}

memset(spcontent,0,sizeof(spcontent));


while(fgets(spcontent,40,spfp)!= NULL)

{

printf(" spcontent的值是

[%s]",spcontent); fflush(stdout);;

}

返回0;

}

< simillar核心转储在系统上发生>

bash-2.05b $ gcc -Wall fgets_fail_test.c -o test

bash -2.05b $ ./test
打开文件/ tmp / filenotexistsSegmentation故障时出现
错误(核心

倾销)

bash-2.05b $ gdb ./test ./core.27887

GNU gdb Red Hat Linux(5.3.90-0.20030710.40rh)

版权所有2003 Free Software Foundation,Inc。

GDB是免费软件,受GNU通用公共许可证保护,并且

你是

欢迎更改它和/或在某些

条件下分发它的副本。

键入show copying;查看条件。

GDB绝对没有保修。输入show warranty for

详情。

这个GDB被配置为i386-redhat-linux-gnu...(没有调试

符号被找到)...使用主机libthread_db库" / lib / tls /

libthread_db.so.1"。


核心由`./test生成''。

程序终止,信号11,分段错误。

从/lib/tls/libc.so.6...读取符号(没有调试符号

找到)...完成。

/lib/tls/libc.so.6的加载符号

从/ lib / ld-读取符号linux.so.2 ...(没有调试符号
找到
)...完成。

/lib/ld-linux.so.2
#0 0xb7506444在fgets()中来自/lib/tls/libc.so.6

(gdb)其中

#0 0xb7506444 in fgets( )来自/lib/tls/libc.so.6

#1 0x0804854b in main()

(gdb)第0帧

#0来自/lib/tls/libc.so.6的fgets()中的0xb7506444

(gdb)print errno

无法访问地址0xb74a7008
char * fgets(char * s,int n,FILE * stream)

Qry:如果stream为NULL,行为是什么。 ?


根据我们的理解,fgets应该返回NULL<和内部

得到应该检查流,如果它不是NULL然后只应该

额外访问流。


请忽略它是否已知,如果你能在实现细节中指出

,那就太好了。

--Gaurav Gupta& Raxit Sheth
http://www.barcamp.org/BarCampMumbaiOct2007 < ; ---- BarCampMumbaiOct2007


Machine 1 :
bash-3.00$ uname -a
SunOS <hostname5.10 Generic_118822-30 sun4u sparc SUNW,Sun-Fire-280R

bash-3.00$ gcc -v
Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/
specs
gcc version 2.95.3 20010315 (release)
Machine 2:
bash-2.05b$ uname -a
Linux <hostname2.4.21-4.EL #1 Fri Oct 3 18:13:58 EDT 2003 i686 i686
i386 GNU/Linux

bash-2.05b$ gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2.3/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --
infodir=/usr/share/info --enable-shared --enable-threads=posix --
disable-checking --with-system-zlib --enable-__cxa_atexit --host=i386-
redhat-linux
Thread model: posix
gcc version 3.2.3 20030502 (Red Hat Linux 3.2.3-20)

bash-2.05b$ cat fgets_fail_test.c
#include<stdio.h>
#include <string.h>
#include<stdlib.h>

/* Demo fgets failed by Raxit Sheth & Gaurav Gupta */

int main()
{
char spcontent[100],sendrepfile[100];
FILE *spfp=NULL;
memset(sendrepfile,0,100);
strcpy(sendrepfile,"/tmp/filenotexists");
if ((spfp = fopen(sendrepfile, "r")) == (FILE *)NULL)
{
printf("error in opening file %s",sendrepfile);fflush(stdout);
}
memset(spcontent, 0, sizeof(spcontent));

while (fgets(spcontent,40, spfp)!=NULL)
{
printf("The value of spcontent is
[%s]",spcontent);fflush(stdout);;
}
return 0;
}
<simillar core dump occurs on both the system>
bash-2.05b$ gcc -Wall fgets_fail_test.c -o test
bash-2.05b$ ./test
error in opening file /tmp/filenotexistsSegmentation fault (core
dumped)
bash-2.05b$ gdb ./test ./core.27887
GNU gdb Red Hat Linux (5.3.90-0.20030710.40rh)
Copyright 2003 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and
you are
welcome to change it and/or distribute copies of it under certain
conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for
details.
This GDB was configured as "i386-redhat-linux-gnu"...(no debugging
symbols found)...Using host libthread_db library "/lib/tls/
libthread_db.so.1".

Core was generated by `./test''.
Program terminated with signal 11, Segmentation fault.
Reading symbols from /lib/tls/libc.so.6...(no debugging symbols
found)...done.
Loaded symbols for /lib/tls/libc.so.6
Reading symbols from /lib/ld-linux.so.2...(no debugging symbols
found)...done.
Loaded symbols for /lib/ld-linux.so.2
#0 0xb7506444 in fgets () from /lib/tls/libc.so.6
(gdb) where
#0 0xb7506444 in fgets () from /lib/tls/libc.so.6
#1 0x0804854b in main ()
(gdb) frame 0
#0 0xb7506444 in fgets () from /lib/tls/libc.so.6
(gdb) print errno
Cannot access memory at address 0xb74a7008
char *fgets(char *s, int n, FILE *stream)
Qry : What is the Behaviour if stream is NULL. ?

As per our understanding fgets should return NULL < and internally
gets should put check on stream, if it is not NULL then only should do
additional access of stream.

please ignore if it is known and would be great if you can point out
in the implementation detail.
--Gaurav Gupta & Raxit Sheth
http://www.barcamp.org/BarCampMumbaiOct2007 <----BarCampMumbaiOct2007

推荐答案

uname -a

SunOS< hostname5.10 Generic_118822- 30 sun4u sparc SUNW,Sun-Fire-280R

bash-3.00
uname -a
SunOS <hostname5.10 Generic_118822-30 sun4u sparc SUNW,Sun-Fire-280R

bash-3.00


gcc -v

阅读规格来自/usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/

specs

gcc version 2.95.3 20010315(release)

机器2:

bash-2.05b
gcc -v
Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/
specs
gcc version 2.95.3 20010315 (release)
Machine 2:
bash-2.05b


uname -a

Linux< hostname2.4.21 -4.EL#1 Fri Oct 3 18:13:58 EDT 2003 i686 i686

i386 GNU / Linux

bash-2.05b
uname -a
Linux <hostname2.4.21-4.EL #1 Fri Oct 3 18:13:58 EDT 2003 i686 i686
i386 GNU/Linux

bash-2.05b


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

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