Re: [PATCH 1/3] getpwuid(mingw): initialize the structure only once

2018-10-15 Thread Eric Sunshine
On Mon, Oct 15, 2018 at 5:47 AM Johannes Schindelin via GitGitGadget
 wrote:
> Signed-off-by: Johannes Schindelin 
> ---
> diff --git a/compat/mingw.c b/compat/mingw.c
> @@ -1800,16 +1800,27 @@ int mingw_getpagesize(void)
>  struct passwd *getpwuid(int uid)
>  {
> +   static unsigned initialized;
> static char user_name[100];
> -   static struct passwd p;
> +   static struct passwd *p;
>
> +   if (initialized)
> +   return p;
> +
> +   len = sizeof(user_name);
> +   if (!GetUserName(user_name, )) {
> +   initialized = 1;
> return NULL;
> +   }

If GetUserName() fails, that failure is recorded (as "initialized=1"
and 'p' is still NULL), so subsequent invocations just return NULL
without doing any more work. Makes sense.

> +   p = xmalloc(sizeof(*p));
> +   p->pw_name = user_name;
> +   p->pw_gecos = "unknown";
> +   p->pw_dir = NULL;
> +
> +   initialized = 1;
> +   return p;
>  }


[PATCH 1/3] getpwuid(mingw): initialize the structure only once

2018-10-15 Thread Johannes Schindelin via GitGitGadget
From: Johannes Schindelin 

Signed-off-by: Johannes Schindelin 
---
 compat/mingw.c | 25 ++---
 1 file changed, 18 insertions(+), 7 deletions(-)

diff --git a/compat/mingw.c b/compat/mingw.c
index 18caf21969..597781b370 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -1800,16 +1800,27 @@ int mingw_getpagesize(void)
 
 struct passwd *getpwuid(int uid)
 {
+   static unsigned initialized;
static char user_name[100];
-   static struct passwd p;
+   static struct passwd *p;
+   DWORD len;
 
-   DWORD len = sizeof(user_name);
-   if (!GetUserName(user_name, ))
+   if (initialized)
+   return p;
+
+   len = sizeof(user_name);
+   if (!GetUserName(user_name, )) {
+   initialized = 1;
return NULL;
-   p.pw_name = user_name;
-   p.pw_gecos = "unknown";
-   p.pw_dir = NULL;
-   return 
+   }
+
+   p = xmalloc(sizeof(*p));
+   p->pw_name = user_name;
+   p->pw_gecos = "unknown";
+   p->pw_dir = NULL;
+
+   initialized = 1;
+   return p;
 }
 
 static HANDLE timer_event;
-- 
gitgitgadget