Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Win32 OTTD2FS/FS2OTTD used from multiple threads but use a static buffer #8713

Closed
JGRennison opened this issue Feb 21, 2021 · 1 comment
Closed

Comments

@JGRennison
Copy link
Contributor

Version of OpenTTD

master

Expected result

No thread safety issues

Actual result

The OTTD2FS/FS2OTTD functions are used in a wide variety of places in the codebase, including indirectly from non-main threads. (e.g. file scanner thread, network threads when debug output is on, crash logger). Various macros also use these functions (e.g. fopen, MB_TO_WIDE/WIDE_TO_MB, etc.).

On Win32, these methods use a static buffer, and return a pointer to that buffer.
In the event that the method is called from multiple threads in the same general time frame, callers could receive output from a different thread. Theoretically this could result in a buffer over-read if a thread is reading the buffer at the same time as another is in the middle of writing it.

I used TLS to make this go away: JGRennison/OpenTTD-patches@0a61671. std::string conversion could also be an option.

Steps to reproduce

I'm sure there must be a way of automatically detecting this on Windows.

@nielsmh
Copy link
Contributor

nielsmh commented Feb 21, 2021

unix.cpp also appears to use a global iconv instance and global buffers.

nielsmh added a commit to nielsmh/OpenTTD that referenced this issue Feb 21, 2021
nielsmh added a commit to nielsmh/OpenTTD that referenced this issue Feb 21, 2021
nielsmh added a commit to nielsmh/OpenTTD that referenced this issue Feb 21, 2021
nielsmh added a commit to nielsmh/OpenTTD that referenced this issue Feb 21, 2021
nielsmh added a commit to nielsmh/OpenTTD that referenced this issue Feb 21, 2021
nielsmh added a commit to nielsmh/OpenTTD that referenced this issue Feb 23, 2021
nielsmh added a commit to nielsmh/OpenTTD that referenced this issue Apr 2, 2021
@nielsmh nielsmh closed this as completed in e0561db Apr 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants