strlen is wrong because can not report if there is some error
e.g.
char *a;
and "a" point to an array of size=size_t max that has no 0 in it
How did you get through the clown filter, I wonder?
Well, never mind that - I'll fix it in a second. But you're wrong about
strlen (no surprise there). It is defined to work on strings. If you
pass it something that is not a string, the behaviour is undefined. You
cannot expect strlen to do a job that it is not intended to do. It is
like complaining at a potato peeler for being unable to peel a diamond.
Looks like I need a tighter lid on the bozo bin.
--
Richard Heathfield <http://www.cpax.org.uk >
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
In article <52nkd353do091m lls7hlmb4vmu4ma ui2qo@4ax.com>, ¬a\\/b <al@f.gwrote:
>strlen is wrong because can not report if there is some error
>e.g.
>char *a;
>and "a" point to an array of size=size_t max that has no 0 in it
Then "a" considered as a string that is longer than (size_t)-1 and the
definition of size_t specifically says that the behaviour of
C is undefined if you manage to allocate an object larger than
size_t can count.
If "a" does not in fact have a terminating 0, then no matter what
size it is, it is a programming error to pass it to strlen.
If the behaviour of strlen proves to be an actual problem for your
system (e.g., if you are working in an environment that can
only allocate 64 Kb at most), then you can write a replacement
user-space function that has whatever error-signaling semantics
are appropriate.
--
Programming is what happens while you're busy making other plans.
On Sun, 02 Sep 2007 03:18:11 -0400, Martin Ambuhl wrote:
>¬a\/b wrote:
>strlen is wrong because can not report if there is some error
>e.g.
>char *a;
>and "a" point to an array of size=size_t max that has no 0 in it
>
>strlen is defined on strings. A string has a 0 char terminating it. It
>is not strlen that is wrong but the incompetent programmer.
you have right only if the programmer never make errors
are you a programmer of that kind?
if strlen sees some problem it has to report an error
example return (size_t)-1 and not continue to run
if there is strlen(0); it has to report the error and not shut down
the system
So if i want to hang some of your pragrams i have just give a string
of size_t max len that has 111111111111111 111111111111111 1111111111
etc in it because for these string, there is the chance strlen should
result in an infinite loop (or ggets or fgets or what you want).
the idea is *all* functions should can to report errors if they see
these errors (and the programmer has to see if something goes wrong
until it is printf)
On Sun, 02 Sep 2007 06:58:49 +0000, Richard Heathfield
<rjh@see.sig.in validwrote:
>¬a\/b said:
>
>strlen is wrong because can not report if there is some error
>e.g.
>char *a;
>and "a" point to an array of size=size_t max that has no 0 in it
>
>How did you get through the clown filter, I wonder?
yes it is possible i'm like a clown :)
>Well, never mind that - I'll fix it in a second. But you're wrong about
>strlen (no surprise there). It is defined to work on strings. If you
>pass it something that is not a string, the behaviour is undefined. You
>cannot expect strlen to do a job that it is not intended to do. It is
>like complaining at a potato peeler for being unable to peel a diamond.
On Sun, 02 Sep 2007 18:31:26 +0200, ¬a\\/b wrote:
So if i want to hang some of your pragrams i have just give a string
of size_t max len that has 111111111111111 111111111111111 1111111111
etc in it because for these string, there is the chance strlen should
result in an infinite loop (or ggets or fgets or what you want).
Usually, the only way of getting such invalid data into a program is to
link to it as a library. And all bets are off at that point, anyway: I
could hang your program by dancing all over memory.
>So if i want to hang some of your pragrams i have just give a string
>of size_t max len that has 111111111111111 111111111111111 1111111111
>etc in it because for these string, there is the chance strlen should
>result in an infinite loop (or ggets or fgets or what you want).
>
>Usually, the only way of getting such invalid data into a program is to
>link to it as a library. And all bets are off at that point, anyway: I
>could hang your program by dancing all over memory.
On Sun, 02 Sep 2007 03:18:11 -0400, Martin Ambuhl wrote:
>¬a\/b wrote:
>strlen is wrong because can not report if there is some error
>e.g.
>char *a;
>and "a" point to an array of size=size_t max that has no 0 in it
>
>strlen is defined on strings. A string has a 0 char terminating it. It
>is not strlen that is wrong but the incompetent programmer.
you have right only if the programmer never make errors
are you a programmer of that kind?
if strlen sees some problem it has to report an error
example return (size_t)-1 and not continue to run
if there is strlen(0); it has to report the error and not shut down
the system
So if i want to hang some of your pragrams i have just give a string
of size_t max len that has 111111111111111 111111111111111 1111111111
etc in it because for these string, there is the chance strlen should
result in an infinite loop (or ggets or fgets or what you want).
the idea is *all* functions should can to report errors if they see
these errors (and the programmer has to see if something goes wrong
until it is printf)
On Mon, 03 Sep 2007 18:15:42 +0200, ¬a\\/b wrote:
if there is strlen(0); it has to report the error and not shut down
the system
How about this: No conforming C program will pass anything other than a
string to strlen(). Thus, it is the program's fault, not the language's
or the C library's.
On Mon, 03 Sep 2007 18:15:42 +0200, ¬a\\/b wrote:
>
>if there is strlen(0); it has to report the error and not shut down
>the system
>
How about this: No conforming C program will pass anything other than a
string to strlen(). Thus, it is the program's fault, not the language's
or the C library's.
>
B.
If there is an error it's the programmer's fault, not the
language that allows such badly designed functions like
strlen to exist.
In article <kocod3hp1k15ur 42d65ourh8mrqju fhj46@4ax.com>, ¬a\\/b <al@f.gwrote:
>if there is strlen(0); it has to report the error and not shut down
>the system
No it doesn't. Or are you saying that's how it should be?
>the idea is *all* functions should can to report errors if they see
>these errors (and the programmer has to see if something goes wrong
>until it is printf)
That may be your idea, but it's not C's.
-- Richard
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Comment