[PATCH] Fix tokenize function and test.
Josh Stone
jistone@redhat.com
Mon Jun 15 20:01:00 GMT 2009
On 06/13/2009 05:33 AM, Przemyslaw Pawelczyk wrote:
> Previous implementation was error-prone, because allowed returning empty
> tokens (mimiced strsep()), which is fine if there is a NULL semantic.
> Unfortunately SystemTap doesn't provide it in scripts and has only blank
> string (""), therefore testing against it was misleading.
> The solution is to return only non-empty tokens (mimic strtok()).
>
> * tapset/string.stp: Fix tokenize.
> * testsuite/systemtap.string/tokenize.stp: Improve and add case with
> more than one delimiter in the delim string.
> * stapfuncs.3stap.in: Update tokenize description.
> * doc/langref.tex: Ditto.
[...]
> - token = strsep(&str_start, THIS->delim);
> + while ((token = strsep(&str_start, THIS->delim)) && !token[0])
> + ;
do-while would be cleaner, please.
> if (token)
> - strncpy (THIS->__retvalue, token, MAXSTRINGLEN);
> + strncpy(THIS->__retvalue, token, (str_start ? str_start : str_end + 1) - token);
> %}
Why do you need the explicit length computation? There will always be a
NUL there anyway, right? I tried reverting this part, and the tests
still pass, so I don't see what this is for.
Also, while we're at it, strlcpy would be preferred for better
termination semantics (guaranteed NUL and no extra NUL padding).
Josh
More information about the Systemtap
mailing list