There have been many postings in recent months on whether to pass NULL, an empty 
string, or a space to FrmCustomAlert. I just discovered a bug in this routine which I 
thought people should know about. My conclusions are:
- Pass an empty string for unreferenced text replacement variables. NULL or "" works 
fine but avoid NULL to be compatible with the documented API (see below).
- Pass a non-empty string for referenced text replacement variables: neither NULL nor 
"" will work in Palm OS 3.0.

If you have access to the source code, you'll see that the routine:
- Computes the length of the final message after replacements.
- Allocates a chunk in the dynamic heap big enough for the final message.
- Copies the alert string (with "^1", etc) into the new chunk.
- Finds "^1"s, etc and replaces them with the text replacement strings.
- Puts up the dialog, etc...

The code which computes the length of the final message adds the size of the 
appropriate text replacement string when it encounters an '^' followed by '1', '2', or 
'3'. It then counts the '1', '2', or '3' as well, so it's bigger than it needs to be - 
no problem so far.

The problem is the third step above: copying the alert string into the new chunk. This 
only works if the length computed above is big enough for the alert string. Normally, 
the text replacement strings (eg. "hello") are longer than the text replacement 
variables (eg. "^2"). However an empty text replacement string is two characters 
smaller. Because the length calculation added an extra one, the result is we're only 
over by one character. If you run into this in the emulator, it'll detect an attempt 
to write into data manager memory because the StrCopy is writing past the end of the 
string.

Note that this bug is in the source code distributed under license. It's in the Palm 
OS 3.0 ROM, but not in more recent versions. The documentation (3.0 and preliminary 
3.3) for FrmCustomAlert says you shouldn't pass NULL, but an empty string is ok. It 
says to use a space for Palm OS 2.0 and earlier. In my opinion, this advice should 
apply to Palm OS 3.0 as well.

So be careful when using FrmCustomAlert in Palm OS 3.0. You might want to check for an 
empty string and replace it with " ", "Untitled", or something like that before 
calling FrmCustomAlert.
-
Danny Epstein     *     mailto:[EMAIL PROTECTED]
Applied Thought Corporation     *     http://www.appliedthought.com
Flytrap for PalmOS     *     http://www.appliedthought.com/flytrap

Reply via email to