Why is it dangerous?

K

Keith Thompson

Richard Heathfield said:
Keith Thompson said: [...]
Suppose a program reads input using gets(). Suppose the programmer is
confident that no input line can be longer than 80 characters, but a
one-bit error reading a '\n' character causes two lines to be merged,
overflowing the input buffer.

Nothing in C can prevent or correct such an error, but just about
anything other than gets() (including ggets(), fgets(), or any of a
number of other routines) can avoid having it cause a buffer overflow.

Fine. Thank you. Could somebody please now explain how such an error could
blow up one's machine.

gets(). Buffer overflow. Stepping on arbitrary memory. Undefined
behavior. Malware vector. Use your imagination.
(Or at least detect it, if not correct it.) Yes, and that's what I thought
he meant. If he *didn't* mean that, why did he bring it up in the first
place?

If you're using gets() in an environment where you *think* you have
complete control over what appears on stdin, but a '\n' character is
somehow (due to a disk failure, for example) changed to something
else, your program can invoke undefined behavior. Using ggets()
rather than gets() is one way to avoid that particular problem; it
doesn't magically correct the corrupted '\n', but it does avoid the
UB.
 
R

Richard Tobin

Fine. Thank you. Could somebody please now explain how such an error could
blow up one's machine.
[/QUOTE]
gets(). Buffer overflow. Stepping on arbitrary memory. Undefined
behavior. Malware vector. Use your imagination.

I think that's Richard's point: it only happens if you use your
imagination, not in real life.

-- Richard
 
P

Paul Hsieh

[...] When I compile a program from our C course with a windows
compiler there is no problem but when I try to compile it with a
linux compiler it complains that

a_03.c:(.text+0x4d): warning: the `gets' function is dangerous
and should not be used.

Is linux more dangerous than windows?

You have to be kidding me. There were BSODs being projected as images
inadvertently during the recent Beijing Olympic Games, and they are
very common occurrences on airport terminal displays. Can you even
describe to me what a Kernel Panic is? Do you know of anyone who ever
even seen one?

The gcc compiler (which is not Linux specific, BTW) is trying to give
you a warning about the use of gets(). The other compilers you are
using are not. Those other compilers are not doing you a very good
service, because this comment from gcc is correct (and it applies
regardless of which OS or compiler you are using.)
[...] Where can I download a non dangerous gets function?

Of course the standards people here probably don't even have the first
clue how to interpret this request. They don't realize that the
problem is not with you, but in fact is with them. They don't realize
that its not good enough to deprecate this function in a standard
nobody is going to follow anymore, or to issue warnings or document
something that says this is bad. They have to functionally poison
this function so *NO ATTEMPT* at making it work will ever succeed.
[...] I have never used gets before is
there undefined behavior somewhere?

Indeed there is. The mere usage of it invokes undefined behavior.
Here is a trimmed down example program from my assignment that
demonstrates the problem

#include <stdio.h>
#include <malloc.h>

void main()
{
    char *string;
    printf("enter string (max 2000 chars): ");
    fflush(stdin);

I'm not sure this makes any sense. Either way, it cannot semantically
do anything useful.
    fflush(stdout);

This is not guaranteed to cause the printf () line to actually flush.
This only causes it to flush from the language's point of view, not
the underlying system's point of view. I would recommend just putting
a \n at the end of the prompt string and forgetting all about these
flushes.
    string = (char *)malloc(2001);
    if(!string) exit(1);
    gets(string);

Remember: gets() *ALWAYS* exhibits undefined behavior. Even if it
appears to have accepted input text correctly, you have no expectation
that it has not also installed a virus on your machine or manipulated
the numbers in all your spread sheets as well or posted your passwords
to alt.scientology or turned on your webcam and posted images to
4chan. You can try to hobble along with fgets() as an alternative or
use the code given here:

http://www.azillionmonkeys.com/qed/userInput.html
    printf("you entered: %s\n", string);
    free(string);
    exit(0);
}

On windows with TurboC and Lcc no error is printed. On linux with
gcc it says gets is dangerous.

Its actually the gcc linker which is giving you this message.
(Because its wrong even if invoked indirectly through some other
programming language.) The function gets() can never be used properly.
Please advise my instructor says gcc is overly pedantic.

You instructor's comments are non sequitur. More man years of work
has gone into gcc than years your instructor has lived.
 
R

Richard Tobin

Can you even
describe to me what a Kernel Panic is? Do you know of anyone who ever
even seen one?

I haven't often seen Linux kernel panics. More often the system just
becomes unresponsive and nothing works except pressing the power
button.

-- Richard
 
D

Doug Miller

gets(). Buffer overflow. Stepping on arbitrary memory. Undefined
behavior. Malware vector. Use your imagination.

I think that's Richard's point: it only happens if you use your
imagination, not in real life.[/QUOTE]

You mean buffer overflows are imaginary?
 
V

vippstar

Who cares? With the error the '\n' is no longer a '\n', and thus
the line is not ended, and gets can happily overwrite
who-knows-what with whatever.

That 'bit error' thing is total bullshit, it can't happend, because of
the defined behavior of getc.
If *ANY* error happends with getc reading a byte, it *WILL* set the
error flag.
gets will return NULL, and the error flag will be set.

Don't post nonsense. I wasted my time thinking about this too hard.
If you are talking about hardware errors, they are not relevant to
comp.lang.c or ISO C. There are no hardware errors.
 
D

Doug Miller

Doug Miller said:


No - but it's tricky to see how a buffer overflow can cause a machine to
blow up unless you've gone to the trouble of installing some kind of
peripheral device that contains something along the lines of Semtex.
Seems you read the phrase "blow up" a bit more literally than I did...
 
J

jameskuyper

(e-mail address removed) wrote:
....
That 'bit error' thing is total bullshit, it can't happend, because of
the defined behavior of getc.
If *ANY* error happends with getc reading a byte, it *WILL* set the
error flag.
gets will return NULL, and the error flag will be set.

Don't post nonsense. I wasted my time thinking about this too hard.
If you are talking about hardware errors, they are not relevant to
comp.lang.c or ISO C. There are no hardware errors.

Hardware errors are extremely relevant to whether or not ISO C's
specification for gets() is a bad design. The standard describes the
behavior of C code in terms of an abstract machine, but that's just
because the committee does not wish to tie C too closely to any
particular kind of machine architecture. It doesn't mean that the
characteristics of real machines are irrelevant to how the C language
and library should be designed. Hardware errors are an unavoidable
fact of life for all real platforms for C implementations, and also
for all plausible future platforms.

Good defensive coding practices for coping with the real possibility
of hardware errors is entirely on-topic in this newsgroup, so long as
the discussion doesn't get too bogged down into the details of
particular kinds of hardware.

And of course, in real life, as opposed to the fantasy world of gets()
defenders, hardware errors are far from being the only possible reason
why the standard input to your program might not have the format or
contents that you expected when you wrote it.
 
P

Paul Hsieh

a_03.c:(.text+0x4d): warning: the `gets' function is dangerous
and should [never] be used.
 ...
Of course, this is nonsense. There is a perfectly safe way to use
gets(), namely by being in control of what appears on stdin.

Heresy!  I'm surprised no one launched a diatribe here
against Mr. Twink, so let me offer a diatribe in support!

Six comments on gets().

First, some history.  (Some c.l.c'ers weren't
even alive at the time of the infamous Internet
Worm.)

I was just learning the C language at the time. I was surprised to
learn that C had such weak string handling, but the advocates kept
insisting that it was a more powerful language than Pascal. Back
then, ideological indoctrinations still had some sort of effect on me.
The infamous worm was complex enough to attempt
exploitation of at least 4 different security loopholes,
but just one of the loopholes was ubiquitous enough
to make it necessary and sufficient for the Worm's
"success."  That loophole was the dangerous use
of gets() in a program (fingerd) usually run with
superuser authority.  It was the exploits of the
Internet Worm that led to the deprecations against
gets().  (The exploiter, IIRC, wasn't a larcenous
"black hat", but rather a "gray hat" who deliberately
aroused the Unix community from its apathy about
such bugs.)

The fingerd->gets() exploit was not trivial.

Well by today's standards it is. Exploiting gets() on an auto buffer
would probably be considered a minimum ability for any black hat
operating today. Even as a non-exploiter kind of programmer myself, I
know exactly how this is done and why and how it works.
The overrun buffer was an automatic variable just
below a procedure frame, whose return-address was
modified to point to executable code within the overrun
buffer.  That code loaded and ran another program
(misnamed 'sh' or 'csh') which, among other things,
executed the 'fake finger' program to exploit the
fingerd->gets() bug on still other machines.

The detailed steps of this exploit get elided in the
retelling,

Wikipedia has most of what you need to know and pointers to a detailed
explanation.
[...] and programmers are left with the take-home
lesson: use gets() and the Russian mob will take
over your machine and the rest of the world.

Uh ... the Russian mob *does* use hackers for these sorts of things to
control machines to make money through spam and other sorts of fraud.
The gets(auto) exploit's primary realistic risk is in fact *exactly
that*.
One doesn't have to be a gets() enthusiast to note
fingerd's special nature, and that the claim that
all gets() usage risks catastrophe is confused.

fingerd's special nature is that it used an auto buffer to accept
input and it used gets(). This same risk and failure has been seen
multiple times in various incarnations. gets() just happens to have
the unusual property that you *cannot* avoid a problem when you use
it. I.e., you cannot code around it.
Second, a confession.

Whenever I build the Index to the Fabulous Pedigree
 http://fabpedigree.com/altix.htm
I do several hundred thousand gets()'s, but none of
them are "dangerous".

That's because you have an external specification for your input that
limits its size (and your compiler has an optimistic implementation of
gets). The point about gets() is that it was enshrined by a standard
that omitted any standard on what could or could not be input. While
its easy to see why such a standard is silly, it was hard for the
standards committee to see that this leads to a problem with gets().
And for you, you just go ahead and set a standard for input; you know,
the one I just called silly.
[...] I live with a few "dangerous"
messages during the build (although I'm sure the pedants
would prefer that each of the several hundred thousand
gets()'s produced its own such message.   :)
Perhaps there's a way to disable gcc's "dangerous"
message but, in keeping with the FSF philosophy, I'm
sure the cure is worse than the disease, something like
   setenv IM_AN_UNREPENTANT_MORONIC_ASSHOLE

In what sense is removing this message worth it? Why not just call
fgets() and nail the trailing \n?
Third, a boast:

Since another of my "eccentric" codings, in private
throw-away code, is to *not* test malloc()'s return for
zero (failure leads to a core dump, which is what I want
anyway(*)), I'm sure that many in this ng believe that
James Dow Allen's code is buggy!

Why would I listen to your confessions to determine that? I have
looked at your code (on your website). Buggy is not the right word
for it. *Narrow* is more what I would use.
[...] I do not believe
this is the case.  When I was rehired after a year to
add new support to a complete OS I wrote as a contractor
I was pleased to note that no changes had been found
necessary to my delivered code.  Code reliability
doesn't require ingenuity (indeed the two may be
inversely related!); it requires conscientiousness
and avoiding the cheap substitution of dogma for thought.

Or perhaps an undemanding audience? Can I obtain your OS and run the
Firefox browser on top of it?
AFAIK, I've never used gets() in code I've delivered
to a customer.  (This is partly because most of my delivered
code has been OS or standalone, with any stdio library
calls unavailable.)  I do use gets() sometimes, on
private code, when the gets()'ed string was itself
machine-produced.  The gets() buffer is usually at least
ten times as large as the longest machine-produced
string.  The executables are protected from the
Internet by an Impenetrable Firewall.  If someone does
break into my house, intending computer mischief,
I'd be surprised if his mischief needed to invoke gets().

The gets() deprecators aren't wrong; indeed I'll cheerfully
concede that their position is more defensible than mine!
But I'm happy to take a Devil's Advocate position to
encourage critical thinking when I see the preposterous and
dogmatic over-generalizations which become so routine in
this ng.  Is gets() a *potential* source of bugs?  Obviously.
But I'd love to organize a wager, between me and one of
the pedants, on whose code contains more *actual* bugs.

I'm sure you could peg me as the worst of the gets "pedants" though I
don't post much here any more. Richard Heathfield takes a strong
principled position against it, but he only scratches the surface of
what is obvious about gets(), then invokes his judgmental nature to
say its evil. I honestly take the position that gets() should, as a
side effect, attempt to delete the source file that contained it the
moment it is run. Instructing programmers that gets() is dangerous is
pointless if we just put it into documents, or compiler warnings or if
you laugh at someone for using it. Modern programmers don't learn
that way.

I have had my code actually tested against other OS developers and
compared fairly well. So I am up for it. How do you propose we do
this comparison and what would we wager?
* - Detractors will argue that what I *should* want to
do is spend hours writing a diagnostic for such malloc()
failures!

Or we might argue that you are misusing your time by not doing things
with a safer programming model in the first place. For example, your
whole "family tree" project -- you did the whole thing in C didn't
you? And you chose C because it was convenient to what you know, and
you think its actually tighter or something; even though you are using
massively over-sized buffers to hold those strings. And how do you
deal with the growing list of tree nodes? Do you painstakingly
allocate each one, then free them all? Of course you do, you revere
K&R as if they were gods. That's a job for Python if ever I saw one.
Even if you do it in C, you are going to want to write a special tree
node allocation pool layer and use a string library.
[...] In fact I don't want to do anything about them
since the smallish malloc()'s I use to build the website
Aren't Going To Fail(tm).  (The pedants will respond to
this with some nonsense about how the website building
may be ported, some day, to the limited-memory chip
inside my car's fuel injection system !)

No, its like your hash table. Your program can only ever support one
of them, so its flaw is more fundamental. It should be more of a "what
if I wanted to deploy a million such websites simultaneously" issue.
Fourth, a peeve:

fgets() preserves LineFeeds, gets() discards them.
Either behavior is fine (an application whose
stringency requires special treatment of an
"unterminated last line" probably will avoid
fgets() for other reasons anyway), but similarly-named
functions *SHOULD BEHAVE SIMILARLY*.
Assuming gets() came first and it was too late to
redefine it, fgets() should have either handled LineFeeds
the same, or have been given an obviously different name.

Or you could have code that did trimming. Because that's what
everyone does anyways.
Whoever created the disparity in these similarly-named
functions should have done to him what Jesse J. secretly
claimed to want to do to Obama.

I *might* have changed from gets() to fgets() on some
of my private code if it weren't for the above nit.
(And yes, I *do* know how to do
     if (*s == '\n') *s = 0;
in C.)

Fifth, an oft-overlooked truism:

Programming (and much real-world activity) involves
compromise between thoroughness and convenience.

I always thought it was: speed, correctness, minimal development-time,
pick any two. Personally, I try to deal with correctness by using
cookie cutter patterns. But this involves developing a lot of
patterns and building up libraries that let you escape from C's
nonsense. So I am delivering on quicker development-time by reusing
development time from prior projects.

Giving up on thoroughness is basically never worth it.
strncpy(), for example, can do everything(*) strcpy()
can do, *except*, when properly coded, overrun a buffer.
In other words, the *only* reason to ever use strcpy()
(besides deliberately creating a security loophole!)
is the convenience of a 2-argument function call compared
with a 3-argument call.  (* -- yes, strcpy() doesn't
null-pad.  Any c.l.c'er ever write code that relied
on the *non*-padding?)

As long as you think the debate is between one K&R function and
another, you cannot escape the trap they leave you in.
Thoroughness is not wrong, *BUT YOU SHOULD SPEND YOUR
THOROUGHNESS WISELY*.  The original Hubbell Telescope
program spent $10,000 studying whether or not to do a
$3 Million test.  Meanwhile the flaw, that showed up
post-launch, could have been found with a simple $50 test.
I'll bet some engineer would have done the $50 test
if not dizzied by the testing paperwork requirements
dictated by pedants.

That's the wrong lesson. The right lesson: the only way to catch
everything via a test, is to test everything. The most complicated
things tend to get the most attention, meaning they tend not to be as
high a risk for failure as you intuitively think.
Finally, let's note that programming and lawyerism
are different crafts.

The Authorities(tm) who post so pedantically in this
ng are often not completely wrong, but their pretentious
comments about gets() show confused thinking.  In
particular, I wonder if some of them are law school
dropouts.

But you draw a false dichotomy. Remember the standard *ENDORSED*
gets() for a long time, and all the official drafts continue to
endorse it. The rationale gave a BS explanation for why it is there.
They also continue to support idiotic functions like strtok(). The
*lawyers*, like you technically have to stand behind some supposed
proper usage of these functions.

To really get an objection to gets() you have to go to the real world
programmers, not the lawyers. It is only through repeated
embarrassment of the lawyers have we gotten them to deprecate it from
the standard (only 20 years after the Morris Worm.)

Doug Gwyn telling the story of how he desperately was trying to save
gets() by making the library into even more of an abomination is
actually quite amusing in a sad kind of way. Somebody on that
committee must have had some shred of decency left in him/her
sufficient to the task of dropping the axe on this blight. But they
cannot erase the 20 year stain it has left.
When I mention the gets()'s that I use, in private,
behind my Impenetrable Firewall(tm), on strings generated
by my own Bugfree Software(tm), they never acknowledge
that some gets()'s are less dangerous than others
but instead reject "safe" usages of gets() based on
     pipe(fd);
     dup2(fd[0], 0);
     write(fd[1], "Hello world\n", 13);
     printf("%s\n", gets(buff));
on grounds that the semantics of pipe(), etc. are *not
guaranteed* by the C Standard(tm).

So you copy as a side effect of IO, rather than making two copies
yourself. Where did this number 13 come from and what happens when
you decide to correct the grammar mistakes in your string some years
later, but forget where the 13 came from? See you can't avoid the
embarrassment.
If anyone has trouble understanding the absurdity and
hypocrisy of this legalistic view, I refer them to answers
previously given, here in the ng.

The "legalistic view" is to support the usage of gets() (like you are
doing) until the new standard is published.
 
C

CBFalconer

Richard said:
Doug Miller said:

No - but it's tricky to see how a buffer overflow can cause a
machine to blow up unless you've gone to the trouble of
installing some kind of peripheral device that contains
something along the lines of Semtex.

If the buffer is in the stack (which the C system may not have) it
allows rewriting the return address etc. This can turn machine
control over to anything at all, random or contrived. Enjoy.
 
C

CBFalconer

That 'bit error' thing is total bullshit, it can't happend,
because of the defined behavior of getc. If *ANY* error
happends with getc reading a byte, it *WILL* set the error
flag. gets will return NULL, and the error flag will be set.

You are highly confused. Even if you assume individual chars are
parity protected, there can be two bit errors. I think you have
never dealt with real hardware.
 
R

Richard Bos

I think that's Richard's point: it only happens if you use your
imagination, not in real life.

If you read "blow up" as "explode physically and violently", yes; but
only Jesuits would read it as such. "Blow up" can, in computer terms,
also mean many other kinds of abrupt erroneous behaviour, from physical
but non-violent failure (no longer likely, but a real possibility in
several systems in the past - and even today, what happens if malware
causes your hard drive to pattern-seek? I haven't tried, and am not
about to), to fatal hard drive corruption, to a spectacular but
temporary crash.

Richard
 
B

Bart

a_03.c:(.text+0x4d): warning: the `gets' function is dangerous
and should not be used.

In C lots of things are dangerous. Simply assigning to an array
element is dangerous because you can easily write outside the array.

But gets() has been singled out -- by this compiler -- because you
can't make it safe by putting in extra tests for example. Other
compilers are obsessed about other things.
 
S

Serve Lau

Richard Heathfield said:
It is possible that I have said it's evil, but I'm failing to find a
quote.
What I *am* finding is lots of quotes from other people saying it's evil.
Please check your facts before posting in future.

he wasnt quoting you and you admit you couldve said it so his remark about
judgmental nature is correct
 
J

jameskuyper

Richard said:
Paul Hsieh said:



It is possible that I have said it's evil, but I'm failing to find a quote.

Here's every relevant quote from you that I could find using Google:

1999-04-14:
probably the most evil function in the standard library (except for gets, of
1999-04-27:
scanf may well be evil, but gets is positively diabolical. Stick to fgets.
1999-04-30:
will not protect your buffer. gets is - well, I disagree with those who say
it's evil because that's rather silly anthropomorphism. But I would agree
1999-07-28:
scanf is evil, remember? Not quite as evil as gets, but still fairly
malicious.
2005-12-24:
Joe Wright said:

1988, I think.

5 comments over more than 9 years, with two of them rejecting the
applicability of the word "evil" to gets(), and only one of them less
than 9 years old. That's not a particular strong demonstration of your
"judgmental nature". There are other demonstrations that are much more
convincing ;-)
 
J

James Dow Allen

Chuck said:
They're all dangerous. For example, a one bit error
in reading a '\n' from your prewritten data can
blow everything.

I won't address Mr. Falconer's charges in the sequel,
but I *do* refer to the problem he cites, so I quote
him here lest someone accuse *me* of ingesting
the wrong kind of mushrooms.

Chuck doesn't specify what causes such byte damage.
The Russian mob? The Pentium Bug(tm)? gets() itself,
due to Trojan House sabotage by the Deprecator-in-Chief?
I'll (give Chuck the benefit of the doubt and) assume
the problem is gamma radiation.

Well by today's standards it is. ...
I know exactly how this is done and why and how it works.

Even *I* could and did write a 'fake finger' from the brief
description. In 1988 the message logs of those I knew got
the message:
You have been finger-f*cked by root
When the quick workaround (chown/chmod) was done, the message
became the slightly more mysterious (and Odyssean?)
You have been finger-f*cked by nobody
[...] and programmers are left with the take-home
lesson: use gets() and the Russian mob will take
over your machine and the rest of the world.

Uh ... the Russian mob *does* use hackers ...

Uh ... yes. But watch the conclusion you're about to reach
and see if blind dogma is subverting rational thought.
fingerd's special nature is that it used an auto buffer
to accept input and it used gets()....

No. fingerd's special nature was that stdin was directed
to a socket connected to an (untrusted) external machine.
Obviously gets()'er's in that environment deserve to have
their n*ts c** *ff. No one has implied anything different.
You managed to write several paragraphs on this subtopic
without even hinting that you understand that the gets()'s
we're "defending" are simply not subject to Russian mob
exploitations!
In what sense is removing this message worth it? Why not
just call fgets() and nail the trailing \n?

I've time to read very few Usenet groups these days.
Are many as totally devoid of humor as c.l.c?
Did anyone seriously think I was worried about the warning
message? (I *do* use a simple 'grep -v' to remove
one irrelevant gcc warning, but haven't bothered for
the friendly "dangerous" message.)

(Even in rec.puzzles, when I said I'd omitted a
smiley-face to "save bandwidth" I received an
overly-sober response about modern modem speeds!
You're the guys who like to over-generalize, but
I'm tempted to guess that if Americans had a better
sense of humor some of the recent domestic and
geopolitical crises might have been avoided.)

Another poster implied that a reason gets() is "dangerous"
is that it will disappear when the pedants take over
libc! Does anyone think any of us would have trouble
writing our own gets() when it goes missing from libc?
(This would also be a trivial way to get rid of the
"dangerous" message.) In fact, at the risk of encouraging
the mob to Repeat_The_Obvious One_More_Time I'll put a
gets implementation in the public domain right now:

/* Not tested :) */
char *gets(char *s)
{
s = fgets(s, 1000000, stdin);
/* core-dump immediately if a cosmic ray has
* damaged the terminating LineFeed:
*/ if (s) 0[index(s, '\n')] = 0;
return s;
}

Hmmm. Surprised that the pedants don't add a string length
count to index() to deal with unterminated strings.
:) :) :) :)
I have looked at your code (on your website).
Buggy is not the right word for it.
*Narrow* is more what I would use.

"Narrow" is unclear. I'll assume you meant something
worse, but are being polite! In any event *most* of
the example programs at my website are just that:
*examples* rather than library functions intended for
immediate reuse.

I think there may be some useful ideas at my site.
For example, real-time programmers might benefit from a
glance at
http://james.fabpedigree.com/lesson9.htm#p94
but I imagine if you condescend to comment further
it will be to nitpick or to gloat when you find some
fragment still written in old-fashioned K&R C.

Recently I added functions to operate on compressed
sparse bit strings:
http://james.fabpedigree.com/sofa.c
which *is* intended as a small but useful (for some)
downloadable package. Paul (and anyone else) is welcome
to examine it and offer comments.
... like your hash table. Your program can only ever
support one of them, so its flaw is more fundamental.

I guess I've not uploaded my cuckoo hash implementation
yet, so you're referring to the hash table I introduced some
years ago under a Subject like "Reusable Source code".
To say that program "can only ever support one [hash table]"
is to ignore that its source code can be modified!
It's My Own(tm) code for My Own(tm) projects; I use
Just-In-Time Programming(tm) for My Own(tm) projects,
and have found this code easy to modify. As it turns
out, not supporting the feature you mention was the correct
Just-In-Time Programming(tm) decision, as that routine has
been superseded by my cuckoo hash implementation which
*does* support multiple users.

The "Reusable Source code" thread used the hash table source as
an example of a concept, without necessarily espousing *that
particular* source code. The point of the thread wasn't hash
tables, it was "Reusable Source Code" in the sense that
% vi hashtable.h
% $CC -c hashtable.c
% $CC application.c hashtable.o
allows more flexibility than the simple
% $CC application.c hashtable.o

As a specific example of the need for customization
(of hash table handling) I mentioned that many applications
permit a *maximum* of two bytes per entry (because essentially
all available memory must be well exploited) while most
off-the-shelf hash table handlers require a *minimum* of
eight bytes per entry for overhead! (Many of us use hash
tables for combinatorial searches or memory-efficient
graphs; these use smallish fixed-length keys, rather than
the variable-length keys usually assumed by hashlib, etc.)

But the whole point was completely lost, as Chuck reacted
defensively re his own hashlib (without ever explaining how
the user needing two bytes maximum could cope with hashlib's
eight-byte minimum), and Paul enlightened us with code in
which Paul (or his dog?) proudly allows the programmer to
replace the confusing, error-prone predicate
(! p->curr)
with an invocation of the function

int utilGenLinkedListIteratorEndReached \
(struct genLinkedListIterator * iter) {
if (iter == NULL) return -__LINE__;
return NULL == iter->curr;
}

Is this the kind of code you think I need to improve
my pedigree generator, Paul? Maybe I should train
*my* dog to come up with useful gems like

int add_two_numbers(int number1, int number2) {
return number1 + number2;
}

int add_three_numbers(int number1, int number2, \
int number3) {
return number1 + add_two_numbers(number2, number3);
}

or perhaps a better caricature of Pedantic Programming(tm)
might be:
int divide_checking_for_zero(int a, int b) {
if (b != 0)
return a / b;
else /* illegal: send an appropriate signal: */
return a / 0;
}

:) :)
Can I obtain your OS and run the
Firefox browser on top of it?

Hunh? The OS performed to customer spec, which was
to do a MS-DOS lookalike on MC68000 chip. I estimated
how long it would take me (quite accurately as it turned
out), multiplied by my then-generous hourly wage and
ended up underbidding a company which probably already
had the OS on-shelf and was charging just for hand-holding.

My customer (who got exclusive rights -- I never claimed
to be a businessman) ended up selling the source to
A Very Big Company You've All Heard Of(tm) and recovering
his payment to me. (The Company's representative spent
several days on-site at my customer's examining the source
"eyes only" and concluded with a comment: "Originally I
was almost sure we'd develop our own in-house instead,
but after examining the source ... Sold!")
I'm sure you could peg me as the worst of the gets "pedants" ...
... I honestly take the position that gets() should, as a
side effect, attempt to delete the source file that
contained it the moment it is run.

With your "honestly", one is tempted to guess you might
not even be joking! If so: Wow! Words fail me.


I'm afraid I'll have to (cheerfully) retract that concession
in your case, Paul.

As I compose this reply, I'm reading, in another window, about
present-day Internet security problems, despite the Billion$
of $$$ spent on $oftware by Micro$oft and others. Frankly,
I doubt that the problem is programmers using simple
functions to do simple things, but is more likely to be
the overly complex methods some of you seem to advocate.
For example, your whole "family tree" project ...

My Family Tree project started out when I wanted to
print our pedigree and snail-mail it to my nieces, but found
that 2 or 3 pages wouldn't be enough. After that
it ... er ... gradually got out of hand ... :)
I confess that code would look *quite* different if
starting over or delivering it to a paying customer.

Just now I did
grep "gets(" *.c
in the tree subdirectory and was slightly surprised at how very
few there were. (The estimate of several 100,000's of gets()
*executions* was still conservative as the Index starts with
330,000 lines which are passed pipe-like to three
gets()'ing filters.)
... you did the whole thing in C didn't you?

Call it a fetish if you wish, but I *do* like to program in C.
My brain isn't completely dormant here, raising my kids and
smelling the roses: I read up on a variety of topics, e.g.
historical linguistics. Learning a new programming language
is *not* a priority and wasn't even before I retired, unless
you count LaTeX as a "programming" language.
And you chose C because ...
you think its actually tighter or something;
even though you are using
massively over-sized buffers to hold those strings.

LOL, chuckle! Call me simplistic but I process one line
at a time. The PC I use has relatively little memory,
but it's enough to spare one (1) 2000-byte buffer
for one (1) 200-byte string. :) :)

Moreover, your comments show another confusion: one uses
the "over-sized buffer" whether one gets()'s or fgets()'s.
In either case, if the string needs to be preserved,
one strlen()'s, malloc()'s, and strcpy()'s (*) Yes, one can
hire Chuck F. to implement this trivial functionality, but
so what? (* - or do the pedants use strncpy() instead of
strcpy() even when they've just strlen()'ed on the off-chance
a gamma-ray zapped the null terminator? :)

True, the fgets() *can* handle lines too long to fit in
the already "over-sized" buffer, but if you're doing that
handling in more than 2% of your fgets() applications you must
be coding like a masochist! The whole point of gets()/fgets()
is the simplicity of reading exactly one line: if you lose
that, you might as well just use fread().
deal with the growing list of tree nodes? Do you painstakingly
allocate each one, then free them all?
... Even if you do it in C, you are going to
want to write a special tree node allocation pool layer
and use a string library.

You seem to have a model of what my software does which
has very little relationship to its reality. There certainly
are painful parts, but they have nothing to do with growing,
allocating, or freeing. Programming speed and reliability
depend on simplicity and you are imagining problems I've
never encountered in 'tree.'
Of course you do, you revere K&R as if they were gods.

If you've been reading my website, you're misquoting!
I revere T&R (not K&R) as if they were human, but excellent
and creative designers.
Giving up on thoroughness is basically never worth it.


That's the wrong lesson. The right lesson: the only way to catch
everything via a test, is to test everything....

I'm agnostic about whether the taxpayers should have been
required to fund the expensive test; the point is that the
flaw would have surfaced readily (with the $50 test) in a
less pedantic testing environment. You seem to have missed
this point; indeed you seem to be arguing that *no* test is
too expensive for the taxpayers! ... Where do you fit on the
political spectrum, Paul? :)

Regarding my claim, which you contested, that one
SHOULD SPEND ONE'S THOROUGHNESS WISELY
consider again the table hander you deprecated as
single instance. Making it multi-instance *would* have
complicated it and did indeed prove unnecessary.
I once worked with a guy who added so many unnecessary
features to his software package, that it was difficult,
later, to add *useful* features he'd overlooked.

I think
SPEND ONE'S THOROUGHNESS WISELY
is a good synopsis of this whole subthread.
There's a lot of changes I'd make to the 'tree'
software if it were worth my while spending time cleaning
it up. Replacing gets() with fgets() is *definitely*
not on the list.

OP wrote, to start the thread:
Is linux more dangerous than windows?

*This* is a joke, right?
a_03.c:(.text+0x4d): warning: the `gets' function is dangerous
and should not be used.
Please advise my instructor says gcc is overly pedantic.

Good question! Thanks for asking. :)

James Hussein Allen
 
N

Nick Keighley

Paul Hsieh said:

It is possible that I have said it's evil, but I'm failing to find a quote.
What I *am* finding is lots of quotes from other people saying it's evil.
Please check your facts before posting in future.

I've assumed that the rest of your article is about as accurate as that
paragraph, so I didn't consider it worth reading.

I'd recommend giving it a try. Some of it is quite interesting
(and I don't think he mentions you again).
 
J

James Dow Allen

James Dow Allen said:

I put as much in as I can without being silly about it.
Alas, many Usanians tend not to notice it.

I'd have exempted *you* specifically but, since we're
on opposite sides of this debate, I was afraid I'd
risk being labeled as your sock puppet playing
"Trolling Pickle." I *did* include a specific
reference to "Americans" to limit my aspersion.
But *this* article contains no humour whatsoever.

Having read your past posts, this seems like a give-away
that there *is* some hidden humor, but I don't see
it, unless your flagrant misspelling "humour" is itself
some caprice or megrim related to the lost Empire.

James
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top