Bug in SosMan's getline_test

F

Friar Tuck

I believe I have found a bug in Dr Sosman's getline_test program, which
is one of the official newsgroup programs. Debugging information
follows.

$ gdb ./getline_test
GNU gdb 6.3
Copyright 2004 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 "i586-suse-linux"...Using host libthread_db library
"/lib/tls/libthread_db.so.1".

(gdb) r
Starting program: /tmp/getline_test

Program received signal SIGSEGV, Segmentation fault.
0x4008febf in getdelim () from /lib/tls/libc.so.6
(gdb) bt
#0 0x4008febf in getdelim () from /lib/tls/libc.so.6
#1 0x4008d844 in getline () from /lib/tls/libc.so.6
#2 0x080486b2 in verify (file=0x804a008, st=0xbfffee70, special=0) at
getline_test.c:44
#3 0x0804891d in main () at getline_test.c:103
(gdb) f 2
#2 0x080486b2 in verify (file=0x804a008, st=0xbfffee70, special=0) at
getline_test.c:44
44 buff = getline(file);
(gdb) p *file
$1 = {_flags = -72539008,
_IO_read_ptr = 0x40018000 'n' <repeats 25 times>, "\n", 'd' <repeats 174 times>...,
_IO_read_end = 0x40018000 'n' <repeats 25 times>, "\n", 'd' <repeats 174 times>...,
_IO_read_base = 0x40018000 'n' <repeats 25 times>, "\n", 'd' <repeats 174 times>...,
_IO_write_base = 0x40018000 'n' <repeats 25 times>, "\n", 'd' <repeats 174 times>...,
_IO_write_ptr = 0x40018000 'n' <repeats 25 times>, "\n", 'd' <repeats 174 times>...,
_IO_write_end = 0x40018000 'n' <repeats 25 times>, "\n", 'd' <repeats 174 times>...,
_IO_buf_base = 0x40018000 'n' <repeats 25 times>, "\n", 'd' <repeats 174 times>...,
_IO_buf_end = 0x40019000 'b' <repeats 200 times>...,
_IO_save_base = 0x0,
_IO_backup_base = 0x0, _IO_save_end = 0x0, _markers = 0x0,
_chain = 0x40151460,
_fileno = 6, _flags2 = 0, _old_offset = 0, _cur_column = 0,
_vtable_offset = 0 '\0',
_shortbuf = "", _lock = 0x804a0a0, _offset = 0, __pad1 =
0x0, __pad2 = 0x804a0ac,
_mode = -1, _unused2 = '\0' <repeats 51 times>}
(gdb)

It looks to me like the _flags field of file has become corrupted - it
seems to have an odd value.
 
B

Ben Pfaff

Friar Tuck said:
I believe I have found a bug in Dr Sosman's getline_test program, which
is one of the official newsgroup programs.

There is no such thing as an "official newsgroup program", at
least not in comp.lang.c
(gdb) bt
#0 0x4008febf in getdelim () from /lib/tls/libc.so.6
#1 0x4008d844 in getline () from /lib/tls/libc.so.6
#2 0x080486b2 in verify (file=0x804a008, st=0xbfffee70, special=0) at
getline_test.c:44
#3 0x0804891d in main () at getline_test.c:103
(gdb) f 2
#2 0x080486b2 in verify (file=0x804a008, st=0xbfffee70, special=0) at
getline_test.c:44
44 buff = getline(file);

It seems very unlikely that Eric would have written his code to
call into the "getline" function in the GNU C library, which is
what this backtrace is showing. I think that you did not compile
and link the test program correctly.

GNU lib'c getline and Eric's getline evidently have different
interfaces, judging from the line of source code above.
 
R

Richard Heathfield

Ben Pfaff said:
There is no such thing as an "official newsgroup program", at
least not in comp.lang.c

There is one. It is:

#include <stdio.h>

int main(void)
{
puts("Hello, world!");
return 0;
}

(or minor variations thereof).

It seems very unlikely that Eric would have written his code to
call into the "getline" function in the GNU C library, which is
what this backtrace is showing. I think that you did not compile
and link the test program correctly.

Quite so. I think Eric is guilty of poor name choice here, but no more.
GNU lib'c getline and Eric's getline evidently have different
interfaces, judging from the line of source code above.

Alas, I think Eric forgot Einstein's dictum that "it should be as simple
as possible, but no simpler". Eric's function is simpler than possible.
 
F

Friar Tuck

There is no such thing as an "official newsgroup program", at
least not in comp.lang.c

Well, it's listed on the group website, so I'd say that's official.
It seems very unlikely that Eric would have written his code to
call into the "getline" function in the GNU C library, which is
what this backtrace is showing. I think that you did not compile
and link the test program correctly.

GNU lib'c getline and Eric's getline evidently have different
interfaces, judging from the line of source code above.

I don't believe there's a standard library function called getline. If
there is, wouldn't it be better if Dr Sosman's version either had a
different name, or followed the same interface?
 
U

user923005

Well, it's listed on the group website, so I'd say that's official.

What's the group website?
I don't believe there's a standard library function called getline.

There isn't. But there is a GNU version that is very popular among
POSIX platforms.
http://www.gnu.org/software/libc/manual/html_node/Line-Input.html
If
there is, wouldn't it be better if Dr Sosman's version either had a
different name, or followed the same interface?

Right. But there isn't a standard C function called getline(). There
is a popular one, so it still might be a good idea to change the name
a bit.
 
M

Mark McIntyre

Well, it's listed on the group website, so I'd say that's official.

There /is/ no group website. Do you mean the CLC wiki? Thats an
unofficial effort maintained by some of the regulars here, but by no
means officially sanctioned (how could it be, there's no elected
officers of this unmoderated group).
I don't believe there's a standard library function called getline.

Correct. However what Ben said was "GNU libc's getline". GNU evidently
provide a nonstandard 'getline' function with different semantics to
Eric's.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
P

pete

Is he really a doctor?
Well, it's listed on the group website, so I'd say that's official.

There is no such thing as "the group website".
I don't believe there's a standard library function called getline.

There isn't.
What is your native language?
 
P

pete

M

Malcolm McLean

Richard Heathfield said:
#include <stdio.h>

int main(void)
{
puts("Hello, world!");
return 0;
}

(or minor variations thereof).
Buggy code is highly frowned on here.
Your progrm returns a success status if the puts operation fails.
It should be

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

int main(void)
{
if(puts("Hello world!") == EOF)
exit(EXIT_FAILURE);
return 0;
}
 
C

Charlie Gordon

Malcolm McLean said:
Buggy code is highly frowned on here.
Your progrm returns a success status if the puts operation fails.
It should be

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

int main(void)
{
if(puts("Hello world!") == EOF)
exit(EXIT_FAILURE);
return 0;
}

Since you want to be overtly picky, you should also notice that writing text
to stdout without a final new-line invokes implementation defined behaviour
(7.19.2p2). There is always one more bug.
 
J

Joachim Schmitz

Charlie Gordon said:
Since you want to be overtly picky, you should also notice that writing
text to stdout without a final new-line invokes implementation defined
behaviour (7.19.2p2). There is always one more bug.
True had he used printf(). puts() though does add a newline (7.17.7.10-2 in
n1124)

Bye, Jojo
 
J

Joachim Schmitz

Joachim Schmitz said:
True had he used printf(). puts() though does add a newline (7.17.7.10-2
in n1124)
Sorry, 7.19.7.10-2:
The puts function writes the string pointed to by s to the stream pointed to
by stdout, and appends a new-line character to the output.
 
P

Philip Potter

Charlie said:
Since you want to be overtly picky, you should also notice that writing text
to stdout without a final new-line invokes implementation defined behaviour
(7.19.2p2). There is always one more bug.

puts() supplies that newline for you.

Phil
 
C

Charlie Gordon

Charlie Gordon said:
Since you want to be overtly picky, you should also notice that writing
text to stdout without a final new-line invokes implementation defined
behaviour (7.19.2p2). There is always one more bug.

OOPS! it was puts, not printf !
But why betray the original scriptures? The Word has always been

printf("Hello world!\n");
 
B

Ben Pfaff

Malcolm McLean said:
int main(void)
{
if(puts("Hello world!") == EOF)
exit(EXIT_FAILURE);
return 0;
}

I'd recommend flushing stdout and checking its return value also,
to ensure that the message was actually printed.
 
P

pete

Charlie said:
OOPS! it was puts, not printf !

It's not implementation defined behavior either.
Whether or not the behavior is defined,
is implementation defined.
In other words, the behavior of a program which does not
terminate the last line of a text stream with a newline,
is not constrained by the standard;
and in yet other words, it's undefined behavior.
 
F

Friar Tuck

Is he really a doctor?


There is no such thing as "the group website".

Isn't the group website cpac.org? It's in one of the group moderator's
signatures and has lots of C resources. It certainly looks official.
There isn't.
What is your native language?

C (gcc 4.1.3).

Was the final conclusion of this thread that Dr Sosman's function
clashes with a common library funcion and should be renamed?

Actually, looking through that backtrace again is quite interesting:

(gdb) p *file
$1 = {_flags = -72539008,
_IO_read_ptr = 0x40018000 'n' <repeats 25 times>, "\n", 'd' <repeats
174 times>...,
_IO_read_end = 0x40018000 'n' <repeats 25 times>, "\n", 'd' <repeats
174 times>...,
_IO_read_base = 0x40018000 'n' <repeats 25 times>, "\n", 'd'
<repeats 174 times>...,
_IO_write_base = 0x40018000 'n' <repeats 25 times>, "\n", 'd'
<repeats 174 times>...,
_IO_write_ptr = 0x40018000 'n' <repeats 25 times>, "\n", 'd'
<repeats 174 times>...,
_IO_write_end = 0x40018000 'n' <repeats 25 times>, "\n", 'd'
<repeats 174 times>...,
_IO_buf_base = 0x40018000 'n' <repeats 25 times>, "\n",
'd' <repeats 174 times>...,
_IO_buf_end = 0x40019000 'b' <repeats 200 times>...,
_IO_save_base = 0x0,
_IO_backup_base = 0x0, _IO_save_end = 0x0, _markers
= 0x0,
_chain = 0x40151460,
_fileno = 6, _flags2 = 0, _old_offset = 0,
_cur_column = 0,
_vtable_offset = 0 '\0',
_shortbuf = "", _lock = 0x804a0a0, _offset =
0, __pad1 =
0x0, __pad2 = 0x804a0ac,
_mode = -1, _unused2 = '\0' <repeats
51 times>}
(gdb)

Does anyone know what all these fields in the FILE * structure do? It
could be useful to be able to pick out information directly from a file
pointer. (By the way, it still looks to me like the _flags field has
been corrupted somehow by getline_test.)
 
B

Barry Schwarz

Isn't the group website cpac.org? It's in one of the group moderator's
signatures and has lots of C resources. It certainly looks official.

This group doesn't have a moderator either.

I wonder what you think a political web site has to do with the C
language.


snip
Actually, looking through that backtrace again is quite interesting:

(gdb) p *file
$1 = {_flags = -72539008,
_IO_read_ptr = 0x40018000 'n' <repeats 25 times>, "\n", 'd' <repeats
174 times>...,
_IO_read_end = 0x40018000 'n' <repeats 25 times>, "\n", 'd' <repeats
174 times>...,
_IO_read_base = 0x40018000 'n' <repeats 25 times>, "\n", 'd'
<repeats 174 times>...,
_IO_write_base = 0x40018000 'n' <repeats 25 times>, "\n", 'd'
<repeats 174 times>...,
_IO_write_ptr = 0x40018000 'n' <repeats 25 times>, "\n", 'd'
<repeats 174 times>...,
_IO_write_end = 0x40018000 'n' <repeats 25 times>, "\n", 'd'
<repeats 174 times>...,
_IO_buf_base = 0x40018000 'n' <repeats 25 times>, "\n",
'd' <repeats 174 times>...,
_IO_buf_end = 0x40019000 'b' <repeats 200 times>...,
_IO_save_base = 0x0,
_IO_backup_base = 0x0, _IO_save_end = 0x0, _markers
= 0x0,
_chain = 0x40151460,
_fileno = 6, _flags2 = 0, _old_offset = 0,
_cur_column = 0,
_vtable_offset = 0 '\0',
_shortbuf = "", _lock = 0x804a0a0, _offset =
0, __pad1 =
0x0, __pad2 = 0x804a0ac,
_mode = -1, _unused2 = '\0' <repeats
51 times>}
(gdb)

The internal structure of a FILE object is implementation dependent.
You will need to ask in a group that discusses your implementation.


Remove del for email
 
B

Ben Pfaff

Friar Tuck said:
Isn't the group website cpac.org? It's in one of the group moderator's
signatures and has lots of C resources. It certainly looks official.

Do you mean www.cpax.org.uk? That's just a website that Richard
Heathfield likes to point to, and apparently contributes to
and/or maintains. He's a leading contributor to comp.lang.c, but
doesn't speak for the group (no one does).

(I doubt that an "official" comp.lang.c website would have a
pointer to "the next Northampton Prayer and Praise meeting", as
that seems quite off-topic for the purpose of the group.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top