lib/gis: Replace non-reentrant strtok with thread-safe strtok_r by NeelGhoshal · Pull Request #7595 · OSGeo/grass · GitHub
Skip to content

lib/gis: Replace non-reentrant strtok with thread-safe strtok_r#7595

Draft
NeelGhoshal wants to merge 1 commit into
OSGeo:mainfrom
NeelGhoshal:fix/lib-gis-reentrant-high-coverage
Draft

lib/gis: Replace non-reentrant strtok with thread-safe strtok_r#7595
NeelGhoshal wants to merge 1 commit into
OSGeo:mainfrom
NeelGhoshal:fix/lib-gis-reentrant-high-coverage

Conversation

@NeelGhoshal

Copy link
Copy Markdown
Contributor

Description

Replace all calls to the non-reentrant strtok() with strtok_r() in
parser_interface.c and parser_wps.c. Each call site introduces a local
saveptr variable so tokenization state is kept per-invocation rather than
in a hidden global buffer.

Affected functions:

  • G__usage_xml() in parser_interface.ckey_desc and gisprompt tokenization loops
  • G__wps_print_process_description() in parser_wps.c — three tokenization loops (gisprompt ×2, key_desc)
  • wps_print_literal_input_output() in parser_wps.c — range value parsing

Motivation and context

strtok() uses a hidden global buffer, making it unsafe in multithreaded
contexts and fragile if any callee also calls strtok(). strtok_r() is
the POSIX reentrant equivalent and is supported on all platforms GRASS targets.

How has this been tested?

The changed functions are exercised by the existing test suites in:

  • lib/gis/testsuite/test_parser_json.py
  • lib/gis/tests/lib_gis_parser_json_test.py

No new behaviour is introduced — only the internal tokenization mechanism changes.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist

  • PR title provides summary of the changes and starts with one of the pre-defined prefixes
  • My code follows the code style of this project
  • My change requires a change to the documentation
  • I have added tests to cover my changes — existing parser tests cover these paths

Replace all uses of the non-reentrant strtok() with strtok_r() in
the GIS parser modules to improve thread safety.

Affected functions:
- G__usage_xml() in parser_interface.c: two tokenization loops
  (key_desc and gisprompt parsing)
- G__wps_print_process_description() in parser_wps.c: three
  tokenization loops (gisprompt x2, key_desc)
- wps_print_literal_input_output() in parser_wps.c: range parsing

Each call site introduces a local saveptr variable so the reentrant
state is kept per-call rather than in a hidden global buffer.
@github-actions github-actions Bot added C Related code is in C libraries labels Jun 23, 2026
Comment thread lib/gis/parser_wps.c

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[pre-commit] reported by reviewdog 🐶

Suggested change

@NeelGhoshal NeelGhoshal marked this pull request as draft June 23, 2026 20:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C Related code is in C libraries

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants