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

Codechange: use thread safe time functions #9260

Merged
merged 1 commit into from May 14, 2021

Conversation

rubidium42
Copy link
Contributor

@rubidium42 rubidium42 commented May 13, 2021

Motivation / Problem

Functions like localtime, gmtime and asctime are not thread safe as they (might) reuse the same buffer.

Description

Use the safer _s/_r variant for localtime and gmtime, and use strftime in favour of asctime. Since similar code is used in multiple places, implement the logic only once... especially since Microsoft has localtime_s and the other localtime_r, and localtime_s has its parameters swapped. The same holds for gmtime.

Limitations

The format function being filled with preprocessor magic to prevent a warning of GCC that a non-literal string is passed to strftime, which is considered to be a bug in GCC even when the appropriate attributes are set to the function. And that magic being hidden from Microsoft's compiler so it does not warn about it. In other words: yuck... any better ideas how to solve that are appreciated.

Checklist for review

Some things are not automated, and forgotten often. This list is a reminder for the reviewers.

  • The bug fix is important enough to be backported? (label: 'backport requested')
  • This PR affects the save game format? (label 'savegame upgrade')
  • This PR affects the GS/AI API? (label 'needs review: Script API')
    • ai_changelog.hpp, gs_changelog.hpp need updating.
    • The compatibility wrappers (compat_*.nut) need updating.
  • This PR affects the NewGRF API? (label 'needs review: NewGRF')

@rubidium42 rubidium42 force-pushed the safe_time_functions branch 3 times, most recently from f3463e3 to 6e72ef0 Compare May 13, 2021 09:11
Functions like localtime, gmtime and asctime are not thread safe as they (might) reuse the same buffer. So use the safer _s/_r variant for localtime and gmtime, and use strftime in favour of asctime.
Copy link
Member

@LordAro LordAro left a comment

Choose a reason for hiding this comment

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

¯\_(ツ)_/¯ fine by me

@rubidium42 rubidium42 merged commit aa5a8fe into OpenTTD:master May 14, 2021
@rubidium42 rubidium42 deleted the safe_time_functions branch May 14, 2021 21:22
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

Successfully merging this pull request may close these issues.

None yet

2 participants