git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] Use mingw.h declarations for gmtime_r/localtime_r on msys2

From
Mike Hommey <mh@glandium.org>
Date
Oct 5, 2021, 06:39 UTC
Message-ID
<20211005063936.588874-1-mh@glandium.org>

Older versions of msys2 had _POSIX_THREAD_SAFE_FUNCTIONS set in pthread_unistd.h, included from unistd.h. That would enable the declarations for gmtime_r and localtime_r in time.h.

That's not the case anymore, and gmtime_r and localtime_r end up being undeclared, which subsequently leads to "miscompilations", for example, in datestamp(), where the result of localtime_r would be truncated and sign-extended before being passed to tm_to_time_t, leading to segfaults at runtime.

Signed-off-by: Mike Hommey <mh@glandium.org>
---
 compat/mingw.h | 2 --
 1 file changed, 2 deletions(-)

A possible alternative fix would be to e.g. add `#define _POSIX_C_SOURCE 200112L` to git-compat-util.h and add `ifndef __MINGW64_VERSION_MAJOR` around the definitions of `gmtime_r` and `localtime_r` in compat/mingw.c, since, after all, they are available there.

diff --git a/compat/mingw.h b/compat/mingw.h
index c9a52ad64a..4fd989980c 100644
--- a/compat/mingw.h
+++ b/compat/mingw.h
@@ -204,10 +204,8 @@ int pipe(int filedes[2]);
 unsigned int sleep (unsigned int seconds);
 int mkstemp(char *template);
 int gettimeofday(struct timeval *tv, void *tz);
-#ifndef __MINGW64_VERSION_MAJOR
 struct tm *gmtime_r(const time_t *timep, struct tm *result);
 struct tm *localtime_r(const time_t *timep, struct tm *result);
-#endif
 int getpagesize(void);	/* defined in MinGW's libgcc.a */
 struct passwd *getpwuid(uid_t uid);
 int setitimer(int type, struct itimerval *in, struct itimerval *out);
-- 
2.33.0
Next: Carlo Arenas
Message 1 of 11 in “Use mingw.h declarations for gmtime_r/localtime_r on msys2”
  1. Use mingw.h declarations for gmtime_r/localtime_r on msys2Mike Hommey, Oct 5, 2021
  2. Carlo ArenasOct 5, 2021
  3. Mike HommeyOct 5, 2021
  4. Mike HommeyNov 18, 2021
  5. Carlo ArenasNov 18, 2021
  6. Mike HommeyNov 18, 2021
  7. Carlo ArenasNov 18, 2021
  8. Mike HommeyNov 18, 2021
  9. Carlo ArenasNov 19, 2021
  10. Mike HommeyNov 19, 2021
  11. Carlo ArenasNov 19, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.