Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Bug in SosMan's getline_test

Reply
Thread Tools

Bug in SosMan's getline_test

 
 
Charlie Gordon
Guest
Posts: n/a
 
      09-13-2007
"Malcolm McLean" <> a écrit dans le message de news:
zf-...
>
> "Richard Heathfield" <> wrote in message
> news:...
>> #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;
> }


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.

--
Chqrlie


 
Reply With Quote
 
 
 
 
Joachim Schmitz
Guest
Posts: n/a
 
      09-13-2007
"Charlie Gordon" <> schrieb im Newsbeitrag
news:46e90935$0$2527$...
> "Malcolm McLean" <> a écrit dans le message de
> news: zf-...
>>
>> "Richard Heathfield" <> wrote in message
>> news:...
>>> #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;
>> }

>
> 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


 
Reply With Quote
 
 
 
 
Joachim Schmitz
Guest
Posts: n/a
 
      09-13-2007

"Joachim Schmitz" <> schrieb im Newsbeitrag
news:fcb1nf$di7$...
> "Charlie Gordon" <> schrieb im Newsbeitrag
> news:46e90935$0$2527$...
>> "Malcolm McLean" <> a écrit dans le message de
>> news: zf-...
>>>
>>> "Richard Heathfield" <> wrote in message
>>> news:...
>>>> #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;
>>> }

>>
>> 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)

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.


 
Reply With Quote
 
Philip Potter
Guest
Posts: n/a
 
      09-13-2007
Charlie Gordon wrote:
> "Malcolm McLean" <> a écrit dans le message de news:
>> 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.


puts() supplies that newline for you.

Phil

--
Philip Potter pgp <at> doc.ic.ac.uk
 
Reply With Quote
 
Charlie Gordon
Guest
Posts: n/a
 
      09-13-2007
"Charlie Gordon" <> a écrit dans le message de news:
46e90935$0$2527$...
> "Malcolm McLean" <> a écrit dans le message de
> news: zf-...
>>
>> "Richard Heathfield" <> wrote in message
>> news:...
>>> #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;
>> }

>
> 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");

--
Chqrlie.


 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      09-13-2007
"Malcolm McLean" <> writes:

> 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.
--
"The way I see it, an intelligent person who disagrees with me is
probably the most important person I'll interact with on any given
day."
--Billy Chambless
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      09-13-2007
Charlie Gordon wrote:
>
> "Charlie Gordon" <> a écrit dans le message de news:
> 46e90935$0$2527$...
> > "Malcolm McLean" <> a écrit dans le message de
> > news: zf-...
> >>
> >> "Richard Heathfield" <> wrote in message
> >> news:...
> >>> #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;
> >> }

> >
> > 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 !


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.

--
pete
 
Reply With Quote
 
Friar Tuck
Guest
Posts: n/a
 
      09-13-2007
On 12 Sep 2007 at 23:47, pete wrote:
> Friar Tuck wrote:
>>
>> On 11 Sep 2007 at 23:30, Ben Pfaff wrote:
>> > Friar Tuck <> writes:
>> >
>> >> I believe I have found a bug in
>> >> Dr Sosman's getline_test program,

>
> Is he really a doctor?
>
>> >> 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

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

>
> 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.

>> > GNU lib'c getline

>
>> I don't believe there's a standard library function called getline.

>
> 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.)

 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      09-14-2007
On Fri, 14 Sep 2007 01:39:12 +0200 (CEST), Friar Tuck
<> wrote:

>On 12 Sep 2007 at 23:47, pete wrote:
>> Friar Tuck wrote:
>>>
>>> On 11 Sep 2007 at 23:30, Ben Pfaff wrote:
>>> > Friar Tuck <> writes:
>>> >
>>> >> I believe I have found a bug in
>>> >> Dr Sosman's getline_test program,

>>
>> Is he really a doctor?
>>
>>> >> 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
>>>
>>> Well, it's listed on the group website, so I'd say that's official.

>>
>> 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.


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
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      09-14-2007
Friar Tuck <> writes:

> On 12 Sep 2007 at 23:47, pete wrote:
>> Friar Tuck wrote:
>>>
>>> On 11 Sep 2007 at 23:30, Ben Pfaff wrote:
>>> > Friar Tuck <> writes:
>>> >
>>> >> I believe I have found a bug in
>>> >> Dr Sosman's getline_test program,

>>
>> Is he really a doctor?
>>
>>> >> 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
>>>
>>> Well, it's listed on the group website, so I'd say that's official.

>>
>> 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.


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.)
--
"Your correction is 100% correct and 0% helpful. Well done!"
--Richard Heathfield
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
*bug* *bug* *bug* David Raleigh Arnold Firefox 12 04-02-2007 03:13 AM
ASP.NET Login control bug or SQL 2005 bug? RedEye ASP .Net 2 12-13-2005 10:57 AM
Re: BUG? OR NOT A BUG? John ASP .Net 2 09-21-2005 10:31 AM
Bug Parade Bug 4953793 Michel Joly de Lotbiniere Java 4 12-02-2003 05:05 AM
how to report bug to g++ ? got a bug and fixed up source code DarkSpy C++ 4 06-27-2003 09:05 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57