"Richard M. Hartman" <[EMAIL PROTECTED]> wrote
> Eliah Ninyo wrote in message <26309@palm-dev-forum>...
> >according to this, why did u define the SrvCall to be [10] if it is just
> >8 digit long? what is padding? do i always need to add 1 for null and 1
> >for paddind?
>
> Padding could be either of two things. A) paranoia, or B) for data
>
Yup, you picked it: paranoia. The programmer's friend.
I should have explained better. A common mistake with strings is what's
called a 'fencepost error' which results in the NULL byte being written one
byte past the last declared byte (the name is taken from the question "how
many fenceposts are needed to put one post every meter down a ten meter
fenceline?" since the common answer (10) isn't the right answer (11))
To be specific:
const int cDataNeeded = 8;
char test[cDataNeeded];
StrCopy(test, "012345678");
This code is WRONG, WRONG, WRONG and will cause memory corruption. The
reason is that test[cDataNeeded] is only cDataNeeded - 1 cells long and the
last line writes a NULL byte into whatever follows test in memory (some part
of the stack in this case?). If you're working with allocated memory
(MemPtrNew) then you'll corrupt the heap.
These bugs are very, very difficult to find and cause hell for new
programmers and old programmers alike. For that reason I either use very
well-tested and safe string handling code or add another byte to the end of
each string for safety. That means that if I do accidentally crap on it I
don't get evil memory corruption errors. One of the few things the Palm API
does really well is provide string handling routines that are far more
NULL-robust than the standard C ones. If the code above used
StrNCopy(test, "012345678", cDataNeeded);
it would still fail but wouldn't write past the end of the block.
You can also extend this logic to provide safety checking for your string
handling. If you write a special value to the buffer byte before you use
the string and then check the byte when you finish with the string you can
tell whether your code is writing past the end of the buffer or not. This
is generally called 'sentry marking' or 'sentry blocks' and there are a
number of different ways to use them.
Anyway, I'm meant to be locating a nasty field-handling bug.
Chris Tutty
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palmos.com/dev/tech/support/forums/