Thursday, April 15, 2010

comp.lang.c - 25 new messages in 8 topics - digest

comp.lang.c
http://groups.google.com/group/comp.lang.c?hl=en

comp.lang.c@googlegroups.com

Today's topics:

* chroot() and popen() won't get along - 3 messages, 3 authors
http://groups.google.com/group/comp.lang.c/t/f10540f417427208?hl=en
* mostly OT (was Re: In the Matter of Herb Schildt: a Detailed Analysis of "C:
The Complete Nonsense") - 6 messages, 4 authors
http://groups.google.com/group/comp.lang.c/t/caf17fab4e7d8530?hl=en
* An exercise, regarding the implementation of an "if" - 2 messages, 1 author
http://groups.google.com/group/comp.lang.c/t/e21de5b677e11cbd?hl=en
* google test [POSTFIX ENCLOSED IN SQUARE BRACKETS] - 1 messages, 1 author
http://groups.google.com/group/comp.lang.c/t/383e6d5a95dc3f75?hl=en
* how to implement logical equivalence (==) or negation (!) using only binary
operators - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.c/t/996f7df08a4c8123?hl=en
* Ignoring the return value of a function - 3 messages, 3 authors
http://groups.google.com/group/comp.lang.c/t/899893fc0003e31b?hl=en
* memset pointer to 0 - 6 messages, 3 authors
http://groups.google.com/group/comp.lang.c/t/0844332d363fdb33?hl=en
* C - gets() function implementation help - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.c/t/7bd0bc765a2e9603?hl=en

==============================================================================
TOPIC: chroot() and popen() won't get along
http://groups.google.com/group/comp.lang.c/t/f10540f417427208?hl=en
==============================================================================

== 1 of 3 ==
Date: Thurs, Apr 15 2010 10:27 am
From: blacklight


I'm experiencing a weird behaviour in executing programs in a chroot()
environment. This is the situation.

I have a path like this:

/p +
|--> /p/popen
|--> /p/exe

This is popen.c:


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

int
main ( int argc, char *argv[] )
{
FILE *pipe;
char ch;

if (chroot("/p") < 0)
return EXIT_FAILURE;

if (!(pipe = popen("/exe", "r")))
return EXIT_FAILURE;

printf ("RESULT: ");

while (fread(&ch, 1, 1, pipe) > 0)
printf ("%c", ch);
printf ("\n");

pclose(pipe);
return EXIT_SUCCESS;
}


popen() does not fail, but just no character is read. Of course if I do not use
chroot() and i just call popen ("./exe", "r") I have no problem. I also have
tried popen-ing "./exe" and "exe", both without any results. Even calling a
system() on "/exe", "./exe" or "exe" gives no results. So, how do I invoke an
executable file in a chroot() environment?

Thanks,
BlackLight


== 2 of 3 ==
Date: Thurs, Apr 15 2010 10:33 am
From: Eric Sosman


On 4/15/2010 1:27 PM, blacklight wrote:
> I'm experiencing a weird behaviour in executing programs in a chroot()
> environment.[...]

You want comp.unix.programmer.

--
Eric Sosman
esosman@ieee-dot-org.invalid


== 3 of 3 ==
Date: Thurs, Apr 15 2010 11:53 am
From: "Ersek, Laszlo"


On Thu, 15 Apr 2010, Eric Sosman wrote:

> On 4/15/2010 1:27 PM, blacklight wrote:
>> I'm experiencing a weird behaviour in executing programs in a chroot()
>> environment.[...]
>
> You want comp.unix.programmer.

... and a working shell in your chroot jail.

Read the Fine Manual on popen()/system(), please.

lacos

==============================================================================
TOPIC: mostly OT (was Re: In the Matter of Herb Schildt: a Detailed Analysis
of "C: The Complete Nonsense")
http://groups.google.com/group/comp.lang.c/t/caf17fab4e7d8530?hl=en
==============================================================================

== 1 of 6 ==
Date: Thurs, Apr 15 2010 10:46 am
From: Seebs


On 2010-04-15, BruceS <bruces42@hotmail.com> wrote:
> I had the opportunity to talk casually to a couple of Microsoft
> developers while my employer was in a cooperative project with
> Microsoft. They told me that the schedules at M$ are very loose.
> When everything is working well, they put in short hours, spend time
> playing Frisbee at work, etc. At other times, they work very long
> hours, essentially living at work and just going home for a little
> sleep. I asked how long it had been since they were on the "loose"
> schedule, and they said it had been several years.

Yeah. MS is famous for this.

$ork is serious about it. Yes, we've had periods of long hours -- twice,
shortly before releases, since I got here. We've also been given
compensation for putting in the extra hours. Not always quite hour-for-hour
matching, but close enough for my purposes.

It also helps that if we tell management a schedule is unrealistic, they
usually listen to us, and adapt the schedule to match reality.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!


== 2 of 6 ==
Date: Thurs, Apr 15 2010 12:43 pm
From: Keith Thompson


blmblm@myrealbox.com <blmblm@myrealbox.com> writes:
> In article <7f64b94d-e4d9-4677-b2b6-80304e482a38@q23g2000yqd.googlegroups.com>,
> spinoza1111 <spinoza1111@yahoo.com> wrote:
[...]
> > Also, I checked
> > his Mom's blog...which is public, to find her protesting the
> > affirmative expansion of science classes to minorities, which
> > indicates a sort of social background of mistrust and reluctance to
> > engage.
>
> Can you provide a specific reference to one such post? The link
> from Seebs's Web page (at http://www.seebs.net) to his mother's column
> appears to be broken. Googling, I found this
>
[URL deleted]
>
> which I think is the right person, but in a quick skim of the first
> few entries I don't find anything like what you describe.
[...]

Ok, so Nilges wrote about something that Seebs's mother is
alleged to have posted on her blog. Fine, we've come to expect that
kind of crap from him.

Why the *&%^ are you discussing it with him? In public? Here in
comp.lang.c? Even if what Nilges is true, what possible relevance
could it have to the C language, to Schildt, or to anything that
anyone here could possible care about? Must you reply in public to
every sentence he writes?

I believe that the e-mail address under which Nilges posts is valid.
If you really want to discuss Seebs's mother's blog with him, feel
free to do so by e-mail. Or perhaps you could find another newsgroup
or forum where it's topical. Create an alt.* group for the purpose if
you like.

Please please *please* stop feeding the troll.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


== 3 of 6 ==
Date: Thurs, Apr 15 2010 12:46 pm
From: Squeamizh


On Apr 15, 8:51 am, blm...@myrealbox.com <blm...@myrealbox.com> wrote:
> In article <7f64b94d-e4d9-4677-b2b6-80304e482...@q23g2000yqd.googlegroups.com>,
>
> spinoza1111 <spinoza1...@yahoo.com> wrote:
>
> > But it's a mistake to believe that Linux is the answer. As the
> > (textbook author and professor) Andrew Tanenbaum pointed out, Linux as
> > compared to the MORE MODERN technology of microkernel OSen, is
> > innately insecure, less reliable and maintainable. Tanenbaum had a
> > "flame war" with Torvalds over this. He graciously apologized for some
> > of his flames; Torvalds did not, and failed to credit Tanenbaum for
> > Minix on which Linux was based. This was Maoism; the assault on
> > midlevel academic authority in the service of big money and power.
>
> > Far more than Windows, Linux is in the service of big money and power
> > since it is the product of slave labor. That is, each coder who
> > contributed to any version was a time-sliced slave. He might have been
> > a happy slave, but these are the best kind.
>
> > This allowed IBM, a larger and more powerful (and somewhat less
> > principled) company than Microsoft, to lay off its proprietary OS
> > developers and save the big bucks. It started with Torvalds' attack on
> > and expropriation of, Tanenbaum. These are as far as I can tell the
> > objective facts even if Tanenbaum expressed respect for Torvalds,
> > since computer science departments are so supported by corporate
> > interests that most of their professors lack true intellectual
> > independence.
>
> Are they. Hm, where's *my* corporate support ....

Hey, B.L. Barnaby (yes, I know that is not your real name, but I'm
allowing you to indulge your fantasy of being the circus clown of the
forum), at this time I must ask you the following questions. Do you
derive amusement from responding to spinoza's incomprehensible
ramblings? Are you pleased with yourself when you give Seebs more
ammo to further pollute this group with responses to these threads?
How on Earth have you not realized that spinoza is too pleased with
himself to ever contribute anything in the way of a productive
discussion of ideas?


== 4 of 6 ==
Date: Thurs, Apr 15 2010 12:55 pm
From: Seebs


On 2010-04-15, Keith Thompson <kst-u@mib.org> wrote:
> Ok, so Nilges wrote about something that Seebs's mother is
> alleged to have posted on her blog. Fine, we've come to expect that
> kind of crap from him.

Actually, I have to dispute this. I was absolutely shocked that
he actually managed to successfully identify my mom. (To save him
trouble, the UFO nut "Linda Seebach" is a different person.)

> Why the *&%^ are you discussing it with him? In public? Here in
> comp.lang.c? Even if what Nilges is true, what possible relevance
> could it have to the C language, to Schildt, or to anything that
> anyone here could possible care about? Must you reply in public to
> every sentence he writes?

I think it's like watching a train wreck. It's utterly fascinating
trying to find some kind of connection between the various things
he says. I just think it's a waste of bandwidth. He should come
over to one of the vB boards where it won't bother other people and
he can post nonsense to his heart's content.

Of course, he won't do that if the whole thing is just an attempt
to destroy comp.lang.c over some imagined slight.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!


== 5 of 6 ==
Date: Thurs, Apr 15 2010 1:30 pm
From: Keith Thompson


Seebs <usenet-nospam@seebs.net> writes:
> On 2010-04-15, Keith Thompson <kst-u@mib.org> wrote:
[...]
> > Why the *&%^ are you discussing it with him? In public? Here in
> > comp.lang.c? Even if what Nilges is true, what possible relevance
> > could it have to the C language, to Schildt, or to anything that
> > anyone here could possible care about? Must you reply in public to
> > every sentence he writes?
>
> I think it's like watching a train wreck. It's utterly fascinating
> trying to find some kind of connection between the various things
> he says. I just think it's a waste of bandwidth.
[...]

It's more like showing up at a train wreck with a can of gasoline and
a book of matches.

(Yeah, that's a gross exaggeration and probably an inappropriate
comparison, but ...)

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


== 6 of 6 ==
Date: Thurs, Apr 15 2010 1:41 pm
From: gazelle@shell.xmission.com (Kenny McCormack)


In article <82or0tFeb3U1@mid.individual.net>,
blmblm@myrealbox.com <blmblm@myrealbox.com> wrote:
...
>Can you provide a specific reference to one such post? The link
>from Seebs's Web page (at http://www.seebs.net) to his mother's column
>appears to be broken. Googling, I found this
>
>http://www.lindaseebach.net/wordpress/
>
>which I think is the right person, but in a quick skim of the first
>few entries I don't find anything like what you describe.

You have to dig a bit. For some reason, Ms. Seebs put only a teaser on
her page - you have to go to "bigjournalism.com" and then put "seebach"
in the "Search" box. Then the article comes up.

I'm not sure it says exactly what Nilges says it does, but it is
abundantly clear which side of the political spectrum the seebs are on -
just from the general tone (read: teabagger) of the site and the text in
her article.

It explains a lot about why Seebs fits in well with the other Bible
bangers here in clc.

--
(This discussion group is about C, ...)

Wrong. It is only OCCASIONALLY a discussion group
about C; mostly, like most "discussion" groups, it is
off-topic Rorsharch revelations of the childhood
traumas of the participants...


==============================================================================
TOPIC: An exercise, regarding the implementation of an "if"
http://groups.google.com/group/comp.lang.c/t/e21de5b677e11cbd?hl=en
==============================================================================

== 1 of 2 ==
Date: Thurs, Apr 15 2010 11:02 am
From: Army1987


On Thu, 15 Apr 2010 14:19:23 +0000, Stefan Ram wrote:

> Write a function
>
> double f( int c, double x );
>
> that will return either the sine or the logarithm of its second
> argument depending on whether it was called with a first argument 0 or
> 1, respectively (that is, 0 for sine and 1 for logarithm), but do not
> use any control statement (such as »if« or »while«) nor any operator
> with conditional evaluation (such as ?:, &&, ||).
{
return c * log(x) + (1 - c) * sin(x);
}


== 2 of 2 ==
Date: Thurs, Apr 15 2010 11:06 am
From: Army1987


On Thu, 15 Apr 2010 18:02:15 +0000, Army1987 wrote:

> On Thu, 15 Apr 2010 14:19:23 +0000, Stefan Ram wrote:
>
>> Write a function
>>
>> double f( int c, double x );
>>
>> that will return either the sine or the logarithm of its second
>> argument depending on whether it was called with a first argument 0
>> or 1, respectively (that is, 0 for sine and 1 for logarithm), but do
>> not use any control statement (such as »if« or »while«) nor any
>> operator with conditional evaluation (such as ?:, &&, ||).
> {
> return c * log(x) + (1 - c) * sin(x);
> }

Sorry, I had skimmed the second paragraph of your post too quickly...

==============================================================================
TOPIC: google test [POSTFIX ENCLOSED IN SQUARE BRACKETS]
http://groups.google.com/group/comp.lang.c/t/383e6d5a95dc3f75?hl=en
==============================================================================

== 1 of 1 ==
Date: Thurs, Apr 15 2010 11:39 am
From: BruceS


On Apr 13, 1:30 am, Nick Keighley <nick_keighley_nos...@hotmail.com>
wrote:
> On 13 Apr, 08:28, Nick Keighley <nick_keighley_nos...@hotmail.com>
> wrote:
>
> > On 12 Apr, 15:24, blm...@myrealbox.com <blm...@myrealbox.com> wrote:
> > > In article <6be118ea-7c38-487d-b183-b0ad252ce...@12g2000yqi.googlegroups.com>,
> > > Nick Keighley  <nick_keighley_nos...@hotmail.com> wrote:
> > > > On 12 Apr, 15:07, Nick Keighley <nick_keighley_nos...@hotmail.com>
> > > > > On 12 Apr, 10:03, blm...@myrealbox.com <blm...@myrealbox.com> wrote:
>
> <snip>
>
>
>
> > > > > > As I understand it, Google's posting interface strips tags in square
> > > > > > brackets from subject lines, so your proposed fix here may not be as
> > > > > > effective as you might like.  Just sayin'.
>
> > > > > this is a test.
> > > > > this is posted from google and the subject line should contain square
> > > > > brackets
>
> > > > well I can see em
>
> > > As can I (using news.individual.net).  However, Seebs's subject line
> > > was
>
> > > "Re: [NILGES] Son of Snarky Tirade: a response to Seebach's new CTCN: part 1"
>
> > > and yours doesn't seem to have the beginning "[NILGES]".  That's what
> > > I thought would happen.
>
> > > So maybe the trick is to put the text in square brackets at the end
> > > rather than the beginning?
>
> > the subject of this post is
> > "[PREFIX ENCLOSED IN SQUARE BRACKETS]  google test  [POSTFIX ENCLOSED
> > IN SQUARE BRACKETS]"
>
> again it appears to survive...

FWIW: I'm using GG, and this subthread shows the full subject (pre-
and postfix) in the left pane. However, looking at individual
messages, your first one is full, but the following ones (even your
own) all strip the prefix.

==============================================================================
TOPIC: how to implement logical equivalence (==) or negation (!) using only
binary operators
http://groups.google.com/group/comp.lang.c/t/996f7df08a4c8123?hl=en
==============================================================================

== 1 of 2 ==
Date: Thurs, Apr 15 2010 11:43 am
From: "Ersek, Laszlo"


On Thu, 15 Apr 2010, Tom St Denis wrote:

> On Apr 15, 5:27�am, "Ersek, Laszlo" <la...@caesar.elte.hu> wrote:

>> /* Return 1u if any bit is set in "x", return 0u otherwise. */
>> unsigned
>> mig32(uint32_t x)
>> {
>> � �x |= x >> 16; /* "condensed" to bits 15 .. 0 */
>> � �x |= x >> �8; /* "condensed" to bits �7 .. 0 */
>> � �x |= x >> �4; /* "condensed" to bits �3 .. 0 */
>> � �x |= x >> �2; /* "condensed" to bits �1 .. 0 */
>> � �x |= x >> �1; /* "condensed" to bit 0 */
>> � �return x & 1u;
>>
>> }
>>
>> {
>> � �uint32_t a, b;
>>
>> ďż˝ ďż˝/* ... */
>>
>> � �mig32(a) ^ 1u; � � � � � �/* boolean-neg */
>> � �mig32(a) ^ mig32(b) ^ 1u; /* boolean-eq; IOW, negated boolean-neq */

> I'm sure you meant mig32(a^b)^1;

I didn't think of that, but if

{
uint32_t a, b;

a = 1u;
b = 2u;
}

Then "a" and "b" are logically equivalent (both compare unequal to 0 --
both are "true"), but

mig32(1u ^ 2u) ^ 1u

yields 0.

AIUI, we're checking for

(0 == a) == (0 == b)

not

a == b

Cheers,
lacos

== 2 of 2 ==
Date: Thurs, Apr 15 2010 1:19 pm
From: Phil Carmody


Andrew Poelstra <apoelstra@localhost.localdomain> writes:
> On 2010-04-14, Ricky <rickyp999@gmail.com> wrote:
>> how to implement logical equivalence (==) or negation (!) for binary
>> numbers using only binary operators such as ~ & ^ | + << >>?

Presumably that should be 'bitwise', not 'binary'.

> /*
> * int compare(int, int):
> * compares two numbers and returns 1 if they are equal
> */
>
> #include <limits.h>
>
> int compare(int a, int b) {
> unsigned int values[UINT_MAX];
> unsigned int idx;
> unsigned int ua = a,

not a bitwise operator.

> ub = b;
> int zcount = 0;
>
> if(ua < 1 || ub < 1)

again, not bitwise

> ua += 73, ub += 73;

also not bitwise.

Phil
--
I find the easiest thing to do is to k/f myself and just troll away
-- David Melville on r.a.s.f1

==============================================================================
TOPIC: Ignoring the return value of a function
http://groups.google.com/group/comp.lang.c/t/899893fc0003e31b?hl=en
==============================================================================

== 1 of 3 ==
Date: Thurs, Apr 15 2010 11:52 am
From: markhobley@hotpop.donottypethisbit.com (Mark Hobley)


I have a function with a prototype as follows:

int foobar(int n)

If I am not interested in the return value of the function, is it legal to
use the function in statement context as follows?

foobar(6); /* Is this legal? */

Or, is it necessary to create a dummy variable for the return value, and
disregard it, as follows?

int tmp;
tmp = foobar(6); /* variable tmp is not used anywhere else in the code */

Mark.

--
Mark Hobley
Linux User: #370818 http://markhobley.yi.org/

== 2 of 3 ==
Date: Thurs, Apr 15 2010 12:16 pm
From: Ben Pfaff


markhobley@hotpop.donottypethisbit.com (Mark Hobley) writes:

> I have a function with a prototype as follows:
>
> int foobar(int n)
>
> If I am not interested in the return value of the function, is it legal to
> use the function in statement context as follows?
>
> foobar(6); /* Is this legal? */

Yes.
--
"Structure padding is the use of extraneous materials to
enhance the shape of a struct and make it more attractive to
members of the opposite struct. (See also "struct silicone.")"
--Eric Sosman


== 3 of 3 ==
Date: Thurs, Apr 15 2010 12:20 pm
From: Andrew Poelstra


On 2010-04-15, Ben Pfaff <blp@cs.stanford.edu> wrote:
> markhobley@hotpop.donottypethisbit.com (Mark Hobley) writes:
>
>> I have a function with a prototype as follows:
>>
>> int foobar(int n)
>>
>> If I am not interested in the return value of the function, is it legal to
>> use the function in statement context as follows?
>>
>> foobar(6); /* Is this legal? */
>
> Yes.

If you use lint, it might complain about it (since the
int return value is there for a reason, therefore you
should use it, goes the theory).

If you know the return value is irrelevant, you can do

(void) foobar(6);

if it makes you feel better.

--
Andrew Poelstra
http://www.wpsoftware.net/andrew


==============================================================================
TOPIC: memset pointer to 0
http://groups.google.com/group/comp.lang.c/t/0844332d363fdb33?hl=en
==============================================================================

== 1 of 6 ==
Date: Thurs, Apr 15 2010 12:44 pm
From: ImpalerCore


Quick question. Is there a guarantee that memset a pointer to 0 will
be equivalent to assigning the NULL pointer? I think not, since there
can be systems where the null pointer is non-zero. Is this the only
reason why?

Thanks


== 2 of 6 ==
Date: Thurs, Apr 15 2010 12:50 pm
From: Keith Thompson


ImpalerCore <jadill33@gmail.com> writes:
> Quick question. Is there a guarantee that memset a pointer to 0 will
> be equivalent to assigning the NULL pointer?

No.

> I think not, since there
> can be systems where the null pointer is non-zero.

Right, assuming that "non-zero" refers to the representation.

> Is this the only
> reason why?

Probably. (I don't see that more than one reason is required.)

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


== 3 of 6 ==
Date: Thurs, Apr 15 2010 12:53 pm
From: ImpalerCore


On Apr 15, 3:50 pm, Keith Thompson <ks...@mib.org> wrote:
> ImpalerCore <jadil...@gmail.com> writes:
> > Quick question.  Is there a guarantee that memset a pointer to 0 will
> > be equivalent to assigning the NULL pointer?
>
> No.
>
> >                                               I think not, since there
> > can be systems where the null pointer is non-zero.
>
> Right, assuming that "non-zero" refers to the representation.
>
> >                                                     Is this the only
> > reason why?
>
> Probably.  (I don't see that more than one reason is required.)

Followup question. If I have two pointers 'a' and 'b', and 'b' is set
explicitly to NULL, can I memcpy 'b' to 'a' and be guaranteed that 'a'
will be interpreted as NULL?

> --
> Keith Thompson (The_Other_Keith) ks...@mib.org  <http://www.ghoti.net/~kst>
> Nokia
> "We must do something.  This is something.  Therefore, we must do this."
>     -- Antony Jay and Jonathan Lynn, "Yes Minister"

== 4 of 6 ==
Date: Thurs, Apr 15 2010 12:55 pm
From: Seebs


On 2010-04-15, ImpalerCore <jadill33@gmail.com> wrote:
> Quick question. Is there a guarantee that memset a pointer to 0 will
> be equivalent to assigning the NULL pointer? I think not, since there
> can be systems where the null pointer is non-zero. Is this the only
> reason why?

Pretty much. all-bits-zero is only guaranteed to be zero-like for
integers, as I recall.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!


== 5 of 6 ==
Date: Thurs, Apr 15 2010 12:56 pm
From: Seebs


On 2010-04-15, ImpalerCore <jadill33@gmail.com> wrote:
> Followup question. If I have two pointers 'a' and 'b', and 'b' is set
> explicitly to NULL, can I memcpy 'b' to 'a' and be guaranteed that 'a'
> will be interpreted as NULL?

Assuming they're the same type of pointer, I am pretty sure you are,
because you're assured that copying the bytes out of an object and
then back into it preserves it.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!


== 6 of 6 ==
Date: Thurs, Apr 15 2010 1:28 pm
From: Keith Thompson


Seebs <usenet-nospam@seebs.net> writes:
> On 2010-04-15, ImpalerCore <jadill33@gmail.com> wrote:
> > Quick question. Is there a guarantee that memset a pointer to 0 will
> > be equivalent to assigning the NULL pointer? I think not, since there
> > can be systems where the null pointer is non-zero. Is this the only
> > reason why?
>
> Pretty much. all-bits-zero is only guaranteed to be zero-like for
> integers, as I recall.

And that guarantee was added only in one of the Technical Corrigenda
to C99. The C99 standard itself allows for the possibility that
all-bits-zero could be a trap representation for an integer type.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

==============================================================================
TOPIC: C - gets() function implementation help
http://groups.google.com/group/comp.lang.c/t/7bd0bc765a2e9603?hl=en
==============================================================================

== 1 of 2 ==
Date: Thurs, Apr 15 2010 12:46 pm
From: Keith Thompson


Andrew Poelstra <apoelstra@localhost.localdomain> writes:
> On 2010-04-15, Keith Thompson <kst-u@mib.org> wrote:
> > Andrew Poelstra <apoelstra@localhost.localdomain> writes:
> >> On 2010-04-15, Nick Keighley <nick_keighley_nospam@hotmail.com> wrote:
> >> > On 12 Apr, 09:33, Richard Heathfield <r...@see.sig.invalid> wrote:
> > [...]
> >> >> If that's the case, the course isn't worth passing.
> >> >
> >> > this is simply nonsense.
> >> > It's a mistake, yes, but not a big enough mistake to invalidate the
> >> > entire course.
> >>
> >> Yes, it is. Using gets() demonstrates a complete inability to reason
> >> about buffers or their boundaries, and writing such a beast is even
> >> worse.
> >
> > Really? What if you're writing a conforming implementation of the
> > standard C library? In that case, you *have* to implement gets()
> > (unless you're only interested in conforming to the C201X standard --
> > you know, the one that doesn't exist yet).
> >
>
> We had a discussion a while back about whether this is a conforming
> implementation:
>
> char *gets(char *buffer) {
> fputs("Hey jerk!!", stderr);
> exit(0);
> return NULL;
> }
>
> Since in the general case, there is no way to guarantee buffer will
> not be run, causing undefined behavior, and printing "Hey jerk" and
> dying is well within the bounds of undefined behavior.
>
> I don't remember where the group ended up on it, but that looks
> perfectly okay to me.

It isn't.

The behavior of gets() is well defined as long as you don't feed it a
line too long to be stored in the buffer. A conforming implementation
may not take advantage of the permissions granted by undefined
behavior unless the undefined behavior actually occurs.

> >> I would fail this assignment because there is no way within the
> >> bounds of the C language to do it correctly. For the same reason,
> >> no legitimate course would ever have such an assignment.
> >
> > Have you considered the possibility that the point of the assignment
> > is to demonstrate the danger of using gets()? Admittedly we've seen
> > no direct evidence of that but it's possible.
> >
>
> I have considered it, yes, but it doesn't seem like the OP
> is at an appropriate skill level to be learning such things.

What is the "appropriate skill level" for learning that gets() is
dangerous? Or did I misunderstand you?

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


== 2 of 2 ==
Date: Thurs, Apr 15 2010 1:09 pm
From: Andrew Poelstra


On 2010-04-15, Keith Thompson <kst-u@mib.org> wrote:
> Andrew Poelstra <apoelstra@localhost.localdomain> writes:
>>
>> We had a discussion a while back about whether this is a conforming
>> implementation:
>>
>> char *gets(char *buffer) {
>> fputs("Hey jerk!!", stderr);
>> exit(0);
>> return NULL;
>> }
>>
>> Since in the general case, there is no way to guarantee buffer will
>> not be run, causing undefined behavior, and printing "Hey jerk" and
>> dying is well within the bounds of undefined behavior.
>>
>> I don't remember where the group ended up on it, but that looks
>> perfectly okay to me.
>
> It isn't.
>
> The behavior of gets() is well defined as long as you don't feed it a
> line too long to be stored in the buffer. A conforming implementation
> may not take advantage of the permissions granted by undefined
> behavior unless the undefined behavior actually occurs.
>

The argument against that was that there is always the possibility
of I/O errors - if not a malicious user, then a corrupted file, bad
pipe or network connection, stray emfs flipping bits, etc, and the
buffer could be overrun despite one's best intentions.

>> >> I would fail this assignment because there is no way within the
>> >> bounds of the C language to do it correctly. For the same reason,
>> >> no legitimate course would ever have such an assignment.
>> >
>> > Have you considered the possibility that the point of the assignment
>> > is to demonstrate the danger of using gets()? Admittedly we've seen
>> > no direct evidence of that but it's possible.
>> >
>>
>> I have considered it, yes, but it doesn't seem like the OP
>> is at an appropriate skill level to be learning such things.
>
> What is the "appropriate skill level" for learning that gets() is
> dangerous? Or did I misunderstand you?
>

Partially. When first learning C, it's fine to say "gets is
dangerous" or "gets is evil" and leave it at that. And given
how easy it is to replace it with fgets() and a fixed buffer
(thus turning "undefined behaviour" into "defined but probably
wrong behavior"), there's no reason not to.

Using fgets() also helps them to understand how arrays and
C strings work, what buffers are, why they have boundaries
and that said boundaries are not checked.

At that point, you can explain /why/ gets() is bad. But requiring
students to implement the function, when they are at a point where
they aren't able to slodge together

c = getc();
while(c != '\n' && c != EOF) {
*buff++ = c;
c = getc();
}

, will quite likely result in the dangers going completely over
the students' heads.

--
Andrew Poelstra
http://www.wpsoftware.net/andrew


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

You received this message because you are subscribed to the Google Groups "comp.lang.c"
group.

To post to this group, visit http://groups.google.com/group/comp.lang.c?hl=en

To unsubscribe from this group, send email to comp.lang.c+unsubscribe@googlegroups.com

To change the way you get mail from this group, visit:
http://groups.google.com/group/comp.lang.c/subscribe?hl=en

To report abuse, send email explaining the problem to abuse@googlegroups.com

==============================================================================
Google Groups: http://groups.google.com/?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate