Why is strtok() Considered Unsafe?

According with the strtok_s section of this document:

6.7.3.1 The strtok_s function The strtok_s function fixes two problems
in the strtok function:

  1. A new parameter, s1max, prevents strtok_s from storing outside of the
    string being tokenized. (The string
    being divided into tokens is both an
    input and output of the function since
    strtok_s stores null characters into
    the string.)
  2. A new parameter, ptr, eliminates the static internal state that
    prevents strtok from being re-entrant
    (Subclause 1.1.12). (The ISO/IEC 9899
    function wcstok and the ISO/IEC 9945
    (POSIX) function strtok_r fix this
    problem identically.)

Leave a Comment