From c590c64726db5c1a50640332c1383a471c24459d Mon Sep 17 00:00:00 2001 From: Ulrich Sibiller Date: Fri, 4 Oct 2019 21:13:35 +0200 Subject: Xau files: adapt code to match upstream libXau 1.0.9 --- nx-X11/lib/src/AuFileName.c | 50 ++++++++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 17 deletions(-) (limited to 'nx-X11/lib/src/AuFileName.c') diff --git a/nx-X11/lib/src/AuFileName.c b/nx-X11/lib/src/AuFileName.c index 6c4fb7dc3..3c6aa8bfd 100644 --- a/nx-X11/lib/src/AuFileName.c +++ b/nx-X11/lib/src/AuFileName.c @@ -1,4 +1,3 @@ - /* Copyright 1988, 1998 The Open Group @@ -30,44 +29,61 @@ in this Software without prior written authorization from The Open Group. #endif #include #include +#include #include +static char *buf = NULL; + +static void +free_filename_buffer(void) +{ + free(buf); + buf = NULL; +} + char * -XauFileName () +XauFileName (void) { - char *slashDotXauthority = "/.Xauthority"; + const char *slashDotXauthority = "/.Xauthority"; char *name; - static char *buf; - static int bsize; + static size_t bsize; + static int atexit_registered = 0; #ifdef WIN32 char dir[128]; #endif - int size; + size_t size; if ((name = getenv ("XAUTHORITY"))) return name; name = getenv ("HOME"); if (!name) { #ifdef WIN32 - (void) strcpy (dir, "/users/"); if ((name = getenv("USERNAME"))) { - (void) strcat (dir, name); + snprintf(dir, sizeof(dir), "/users/%s", name); name = dir; } if (!name) #endif - return 0; + return NULL; } size = strlen (name) + strlen(&slashDotXauthority[1]) + 2; - if (size > bsize) { - if (buf) - free (buf); - buf = malloc ((unsigned) size); - if (!buf) - return 0; + if ((size > bsize) || (buf == NULL)) { + free (buf); + assert(size > 0); + buf = malloc (size); + if (!buf) { + bsize = 0; + return NULL; + } + + if (!atexit_registered) { + atexit(free_filename_buffer); + atexit_registered = 1; + } + bsize = size; } - strcpy (buf, name); - strcat (buf, slashDotXauthority + (name[1] == '\0' ? 1 : 0)); + snprintf (buf, bsize, "%s%s", name, + slashDotXauthority + (name[0] == '/' && name[1] == '\0' ? 1 : 0)); return buf; } -- cgit v1.2.3