strlen

¬

¬a\\/b

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
 
R

Richard Heathfield

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

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

Martin Ambuhl

¬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

strlen is defined on strings. A string has a 0 char terminating it. It
is not strlen that is wrong but the incompetent programmer.
 
W

Walter Roberson

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

jacob navia

¬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

You are right. We are trying to fix this. Just wait a minute, we will be
soon there.
 
¬

¬a\\/b

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

¬a\\/b

¬a\/b said:


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.

nothing to say
 
R

Rob Kendrick

So if i want to hang some of your pragrams i have just give a string
of size_t max len that has 1111111111111111111111111111111111111111
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.

B.
 
¬

¬a\\/b

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.

what is the "program"?
 
¬

¬a\\/b

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

Richard Tobin

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
 
R

Rob Kendrick

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

jacob navia

Rob said:
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.
 
K

Karl Heinze

If there is an error it's the programmer's fault, not the
language that allows such badly designed functions like
strlen to exist.
You don't get it, Jacob: ANSI/ISO C is consider sacred.


K. H.
 
B

Barry Schwarz

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

Since SIZE_MAX can be a valid return value from strlen on some
systems, that wouldn't seem to work.

How would you have strlen detect this problem without invoking
undefined behavior?

Why stop with strlen? What about strcat? And the string search
functions? By your logic, the subscript operators should do range
checking. As should the is-- functions. And the division operator
should check for 0. Maybe bsearch should verify the array is sorted.
Where does it stop?
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 1111111111111111111111111111111111111111
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)


Remove del for email
 
K

Kenny McCormack

You don't get it, Jacob: ANSI/ISO C is consider sacred.

By some.

And some people consider the oil stain on their driveway that (they
think) looks like Jesus to be sacred.

Doesn't prove much.
 
P

pete

¬a\/b said:
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 1111111111111111111111111111111111111111
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)

I can't imagine myself writing a program that would
check the return value of a function
to see if invalid arguments had been passed,
instead of writing the program
to ensure that the arguments were valid,
prior to making the function call.
 
G

Gordon Burditt

strlen is wrong because can not report if there is some error

strlen() has no way to report errors, according to its definition
in Standard C.

strlen() may well have no way to *detect* the error of an unterminated
string without causing undefined behavior (aborting the program with
a smegmentation fault, for example. And nobody said a smegmentation
fault has to be a catchable error).
you have right only if the programmer never make errors
are you a programmer of that kind?

How does one *detect* such an error? Especially if a + size_t max is
off the end of allocated memory?
if strlen sees some problem it has to report an error

That's not what Standard C says.
example return (size_t)-1 and not continue to run

It can either return or not continue to run; it can't do both.
if there is strlen(0); it has to report the error and not shut down
the system

That's not what Standard C says. And there's a difference between
shutting down the *program* (e.g. with smegmentation fault), and
shutting down the *system* (e.g. with blue screen of death or panic:
out of swap space).
So if i want to hang some of your pragrams i have just give a string
of size_t max len that has 1111111111111111111111111111111111111111
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)

I'd be interested in how functions like exit() and abort() report errors,
and how a programmer checks for errors.
 
M

Mark McIntyre

You don't get it, Jacob: ANSI/ISO C is consider sacred.

I recall the old saying:
"its a bad workman who blames his tools."

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

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

No members online now.

Forum statistics

Threads
474,099
Messages
2,570,626
Members
47,237
Latest member
David123

Latest Threads

Top