[clang] [clang] replaced the usage of `asctime` with `strftime` (PR #99075)

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 17 06:54:10 PDT 2024


================
@@ -1722,10 +1722,12 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
     // MSVC, ICC, GCC, VisualAge C++ extension.  The generated string should be
     // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
     const char *Result;
+    char TimeString[std::size("Ddd Mmm dd hh:mm:ss yyyy")];
     if (getPreprocessorOpts().SourceDateEpoch) {
       time_t TT = *getPreprocessorOpts().SourceDateEpoch;
       std::tm *TM = std::gmtime(&TT);
-      Result = asctime(TM);
+      strftime(TimeString, std::size(TimeString), "%c", TM);
----------------
AaronBallman wrote:

I think there's three problems here:

1) We need to look at the return value from `strftime()` and if it's `0`, fallback to the failure mode.
2) Different locales may require a different buffer size.
3) `%c` isn't specified to return the same thing as `asctime()` though it frequently will.

So I think `TimeString` should probably have a hard-coded buffer size that's a bit bigger than we expect is necessary, like `char TimeString[64];`, and the format string to `strftime()` should be `%a %b %d %T %Y`, and we should check the return value of the call to `strftime()` and fall back to `?? .. ?` if the value is 0.

https://github.com/llvm/llvm-project/pull/99075


More information about the cfe-commits mailing list