J
John Kelly
First off, there is no portable way to tell when you've run out of
address bits. The standard says very little about how addresses are
represented.
But ok, given that an address is 32 bits, you could stop looking after
2**32 bytes. But that will still take you beyond the bounds of the
object you're examining.
For the purposes of making trim() robust against bad input, there's no
such thing as an "object" to be examined. If the user points me to some
random garbage memory, then I will try and trim the garbage. If I can
find a \0 out there in the garbage, I'll treat the garbage as a string
and trim it.
I don't care what pointer the user gives me as input. That's his
problem. I just want to guarantee that no matter what he gives me, I
won't be stuck in an infinite loop.
Look again at the strlen() example above. Suppose the array is
followed in the machine's address space by a chunk of memory that
your process doesn't own. How can strlen() or trim() detect this
and avoid blowing up?
If you have a pointer to the beginning of a 100-byte array with a
'\0' in the last position, you must scan for 100 bytes. If you have
a pointer to a 10-byte array, not containing any '\0' characters,
immediately followed in the address space by memory not owned by
your process, you must not scan for more than 10 bytes. You cannot
tell the difference in any portable manner, and you very likely
cannot tell the difference even in some non-portable manner.
Segfaults are the caller's problem, not mine. If he gives me a garbage
pointer, I'll try and do what the dummy asks.
I want to explain to you how this stuff is actually defined.
I think I understand it. But I don't think you understand what my
objective is, or why.
I just finished up the bullet proof version. Here it comes ...