#364 strtok_r defined in pthread.h rather than string.h

v1.0 (example)

Per POSIX.1-2001, strtok_r is supposed to be defined in string.h, not pthread.h. Defining it somewhere else breaks autoconf/gnulib's detection, causing it to try to supplement with its own implementation (which then breaks because the pthread.h strtok_r is a #define).

Note that strtok_r is useful even in programs which do not use threading: one may wish to iterate over two token lists in parallel.


  • Stephen Kitt

    Stephen Kitt - 2014-01-21

    The current definition

    #ifndef strtok_r
    #define strtok_r(__s, __sep, __last)  (*(__last) = strtok((__s), (__sep)))

    isn't re-entrant, which goes against the spec.

    Simply using strtok_s instead of strtok_r fixes it. The attached patch (against 3.1.0) does that, removing the definition from pthread.h, but it assumes that sec_api is available. strtok-test.c, also attached, illustrates the problem.

    Last edit: Stephen Kitt 2014-01-21
  • Gianluigi Tiesi

    Gianluigi Tiesi - 2014-07-20

    this patch breaks compatibility with windows xp

  • Stephen Kitt

    Stephen Kitt - 2014-07-21

    Thanks, the attached version implements strtok_r directly, which should work on XP.

  • Jonathan Yong

    Jonathan Yong - 2014-07-21

    This has already been fixed in master, closing.

  • Jonathan Yong

    Jonathan Yong - 2014-07-21
    • status: open --> closed-out-of-date

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks