Re: [PATCH] strtok -> strsep (The Easy Cases)

Rusty Russell (rusty@rustcorp.com.au)
Fri, 04 May 2001 10:57:36 +1000


In message <01050120580701.01713@golmepha> you write:
> Hello,
>
> the patch at the bottom does the bulk job of strtok replacement. It's a
> very boring patch, containing easy cases, only. It became a bit big, too,
> but I trust you can digest it nevertheless. It's made against kernel
> version 2.4.4.

There are two cases where the substitution is problematic:

Array:
char tmparray[500];
strcpy(tmparray, str);

/* for (p = strtok(tmparray, "n"); p; p = strtok(NULL, "n")) { */
while ((p = strsep(&tmparray, ","))) {

This is clearly wrong, and invokes a compiler warning. &tmparray ==
tmparray (a cute C oddity I've never really liked). You are blowing
away the first few characters in tmparray, and your parser won't work
properly.

Dynamic:

char *tmp = strdup(str);

/* for (p = strtok(tmp, "n"); p; p = strtok(NULL, "n")) { */
while ((p = strsep(&tmp, ","))) {
...
}

kfree(tmp);

Here, tmp has changed in the strsep implementation, and kfree will do
bad things.

There is a real reason to avoid strtok, and that is SMP and multple
threads calling it at once (that said, I don't know of a problem yet).
But this patch is a step backwards.

Rusty.

--
Premature optmztion is rt of all evl. --DK
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/