From a7355c5c8ab9c013f4f0a75175a718ee3f4e970a Mon Sep 17 00:00:00 2001 From: Ulrich Sibiller Date: Fri, 4 Oct 2019 21:01:59 +0200 Subject: Xau: mark NX changes libXau is integrated into libNX_X11, but the differences to libXau original code where not explicitly marked. This path adds some ifdefs and also restores the original code. Attributes to ArcticaProject/nx-libs#850 --- nx-X11/lib/src/AuRead.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/nx-X11/lib/src/AuRead.c b/nx-X11/lib/src/AuRead.c index 61814792d..5c04c3258 100644 --- a/nx-X11/lib/src/AuRead.c +++ b/nx-X11/lib/src/AuRead.c @@ -30,13 +30,16 @@ in this Software without prior written authorization from The Open Group. #endif #include #include +#ifdef NX_TRANS_SOCKET #include +#endif static int read_short (unsigned short *shortp, FILE *file) { unsigned char file_short[2]; +#ifdef NX_TRANS_SOCKET /* * Added a check on EINTR to prevent the fread() call to be * interrupted by any signal not blocked by OsBlockSignals(). @@ -53,6 +56,10 @@ read_short (unsigned short *shortp, FILE *file) } break; } +#else + if (fread ((char *) file_short, (int) sizeof (file_short), 1, file) != 1) + return 0; +#endif *shortp = file_short[0] * 256 + file_short[1]; return 1; } @@ -71,6 +78,7 @@ read_counted_string (unsigned short *countp, char **stringp, FILE *file) data = malloc ((unsigned) len); if (!data) return 0; +#ifdef NX_TRANS_SOCKET for (;;) { if (fread (data, (int) sizeof (char), (int) len, file) != len) @@ -87,6 +95,13 @@ read_counted_string (unsigned short *countp, char **stringp, FILE *file) } break; } +#else + if (fread (data, (int) sizeof (char), (int) len, file) != len) { + bzero (data, len); + free (data); + return 0; + } +#endif } *stringp = data; *countp = len; -- cgit v1.2.3 From 9deebfc8faf3572747fb08fe13ced80ff51cad32 Mon Sep 17 00:00:00 2001 From: Ulrich Sibiller Date: Fri, 4 Oct 2019 21:03:47 +0200 Subject: AuRead.c: whitespace cleanup --- nx-X11/lib/src/AuRead.c | 60 ++++++++++++++++++++++++------------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/nx-X11/lib/src/AuRead.c b/nx-X11/lib/src/AuRead.c index 5c04c3258..d63dc96e9 100644 --- a/nx-X11/lib/src/AuRead.c +++ b/nx-X11/lib/src/AuRead.c @@ -68,16 +68,16 @@ static int read_counted_string (unsigned short *countp, char **stringp, FILE *file) { unsigned short len; - char *data; + char *data; if (read_short (&len, file) == 0) - return 0; + return 0; if (len == 0) { - data = 0; + data = 0; } else { - data = malloc ((unsigned) len); - if (!data) - return 0; + data = malloc ((unsigned) len); + if (!data) + return 0; #ifdef NX_TRANS_SOCKET for (;;) { @@ -89,10 +89,10 @@ read_counted_string (unsigned short *countp, char **stringp, FILE *file) clearerr (file); continue; } - bzero (data, len); - free (data); - return 0; - } + bzero (data, len); + free (data); + return 0; + } break; } #else @@ -110,40 +110,40 @@ read_counted_string (unsigned short *countp, char **stringp, FILE *file) Xauth * XauReadAuth (auth_file) -FILE *auth_file; +FILE *auth_file; { Xauth local; Xauth *ret; if (read_short (&local.family, auth_file) == 0) - return 0; + return 0; if (read_counted_string (&local.address_length, &local.address, auth_file) == 0) - return 0; + return 0; if (read_counted_string (&local.number_length, &local.number, auth_file) == 0) { - if (local.address) free (local.address); - return 0; + if (local.address) free (local.address); + return 0; } if (read_counted_string (&local.name_length, &local.name, auth_file) == 0) { - if (local.address) free (local.address); - if (local.number) free (local.number); - return 0; + if (local.address) free (local.address); + if (local.number) free (local.number); + return 0; } if (read_counted_string (&local.data_length, &local.data, auth_file) == 0) { - if (local.address) free (local.address); - if (local.number) free (local.number); - if (local.name) free (local.name); - return 0; + if (local.address) free (local.address); + if (local.number) free (local.number); + if (local.name) free (local.name); + return 0; } ret = (Xauth *) malloc (sizeof (Xauth)); if (!ret) { - if (local.address) free (local.address); - if (local.number) free (local.number); - if (local.name) free (local.name); - if (local.data) { - bzero (local.data, local.data_length); - free (local.data); - } - return 0; + if (local.address) free (local.address); + if (local.number) free (local.number); + if (local.name) free (local.name); + if (local.data) { + bzero (local.data, local.data_length); + free (local.data); + } + return 0; } *ret = local; return ret; -- cgit v1.2.3 From 5929dfdebf9c9914187f1b41f70d0b721def7122 Mon Sep 17 00:00:00 2001 From: Ulrich Sibiller Date: Fri, 4 Oct 2019 21:05:49 +0200 Subject: AuRead.c: remove redundant null check on calling free() Backport of this commit: commit 7ba7085b4f01f3cd72008712a5333ea3f0edfd88 Author: walter harms Date: Sat Oct 28 19:14:22 2017 +0200 AuRead.c: remove redundant null check on calling free() this removes simply unneeded code from XauReadAuth Signed-off-by: Walter Harms Reviewed-by: Daniel Martin Reviewed-by: Emil Velikov --- nx-X11/lib/src/AuRead.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/nx-X11/lib/src/AuRead.c b/nx-X11/lib/src/AuRead.c index d63dc96e9..993aa24b9 100644 --- a/nx-X11/lib/src/AuRead.c +++ b/nx-X11/lib/src/AuRead.c @@ -120,25 +120,25 @@ FILE *auth_file; if (read_counted_string (&local.address_length, &local.address, auth_file) == 0) return 0; if (read_counted_string (&local.number_length, &local.number, auth_file) == 0) { - if (local.address) free (local.address); + free (local.address); return 0; } if (read_counted_string (&local.name_length, &local.name, auth_file) == 0) { - if (local.address) free (local.address); - if (local.number) free (local.number); + free (local.address); + free (local.number); return 0; } if (read_counted_string (&local.data_length, &local.data, auth_file) == 0) { - if (local.address) free (local.address); - if (local.number) free (local.number); - if (local.name) free (local.name); + free (local.address); + free (local.number); + free (local.name); return 0; } ret = (Xauth *) malloc (sizeof (Xauth)); if (!ret) { - if (local.address) free (local.address); - if (local.number) free (local.number); - if (local.name) free (local.name); + free (local.address); + free (local.number); + free (local.name); if (local.data) { bzero (local.data, local.data_length); free (local.data); -- cgit v1.2.3 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/AuDispose.c | 10 ++++----- nx-X11/lib/src/AuFileName.c | 50 ++++++++++++++++++++++++++++++--------------- nx-X11/lib/src/AuGetBest.c | 22 +++++++------------- nx-X11/lib/src/AuRead.c | 25 +++++++++++------------ 4 files changed, 56 insertions(+), 51 deletions(-) diff --git a/nx-X11/lib/src/AuDispose.c b/nx-X11/lib/src/AuDispose.c index bb890201d..215401ae9 100644 --- a/nx-X11/lib/src/AuDispose.c +++ b/nx-X11/lib/src/AuDispose.c @@ -1,4 +1,3 @@ - /* Copyright 1988, 1998 The Open Group @@ -32,13 +31,12 @@ in this Software without prior written authorization from The Open Group. #include void -XauDisposeAuth (auth) -Xauth *auth; +XauDisposeAuth (Xauth *auth) { if (auth) { - if (auth->address) (void) free (auth->address); - if (auth->number) (void) free (auth->number); - if (auth->name) (void) free (auth->name); + free (auth->address); + free (auth->number); + free (auth->name); if (auth->data) { (void) bzero (auth->data, auth->data_length); (void) free (auth->data); 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; } diff --git a/nx-X11/lib/src/AuGetBest.c b/nx-X11/lib/src/AuGetBest.c index 2518805cc..3e0231b13 100644 --- a/nx-X11/lib/src/AuGetBest.c +++ b/nx-X11/lib/src/AuGetBest.c @@ -1,4 +1,3 @@ - /* Copyright 1988, 1998 The Open Group @@ -34,14 +33,7 @@ in this Software without prior written authorization from The Open Group. #include #endif -static int -binaryEqual (_Xconst char *a, _Xconst char *b, int len) -{ - while (len--) - if (*a++ != *b++) - return 0; - return 1; -} +#define binaryEqual(a, b, len) (memcmp(a, b, len) == 0) Xauth * XauGetBestAuthByAddr ( @@ -72,14 +64,14 @@ XauGetBestAuthByAddr ( auth_name = XauFileName (); if (!auth_name) - return 0; + return NULL; if (access (auth_name, R_OK) != 0) /* checks REAL id */ - return 0; + return NULL; auth_file = fopen (auth_name, "rb"); if (!auth_file) - return 0; + return NULL; - best = 0; + best = NULL; best_type = types_length; for (;;) { entry = XauReadAuth (auth_file); @@ -101,11 +93,11 @@ XauGetBestAuthByAddr ( if ((family == FamilyWild || entry->family == FamilyWild || (entry->family == family && ((address_length == entry->address_length && - binaryEqual (entry->address, address, (int)address_length)) + binaryEqual (entry->address, address, address_length)) ))) && (number_length == 0 || entry->number_length == 0 || (number_length == entry->number_length && - binaryEqual (entry->number, number, (int)number_length)))) + binaryEqual (entry->number, number, number_length)))) { if (best_type == 0) { diff --git a/nx-X11/lib/src/AuRead.c b/nx-X11/lib/src/AuRead.c index 993aa24b9..2e21cf0a5 100644 --- a/nx-X11/lib/src/AuRead.c +++ b/nx-X11/lib/src/AuRead.c @@ -46,7 +46,7 @@ read_short (unsigned short *shortp, FILE *file) */ for (;;) { - if (fread ((char *) file_short, (int) sizeof (file_short), 1, file) != 1) { + if (fread ((char *) file_short, sizeof (file_short), 1, file) != 1) { if (errno == EINTR && ferror (file)) { perror ("Reading from auth file"); clearerr (file); @@ -57,7 +57,7 @@ read_short (unsigned short *shortp, FILE *file) break; } #else - if (fread ((char *) file_short, (int) sizeof (file_short), 1, file) != 1) + if (fread ((char *) file_short, sizeof (file_short), 1, file) != 1) return 0; #endif *shortp = file_short[0] * 256 + file_short[1]; @@ -73,7 +73,7 @@ read_counted_string (unsigned short *countp, char **stringp, FILE *file) if (read_short (&len, file) == 0) return 0; if (len == 0) { - data = 0; + data = NULL; } else { data = malloc ((unsigned) len); if (!data) @@ -81,7 +81,7 @@ read_counted_string (unsigned short *countp, char **stringp, FILE *file) #ifdef NX_TRANS_SOCKET for (;;) { - if (fread (data, (int) sizeof (char), (int) len, file) != len) + if (fread (data, sizeof (char), len, file) != len) { if (errno == EINTR && ferror (file)) { @@ -96,7 +96,7 @@ read_counted_string (unsigned short *countp, char **stringp, FILE *file) break; } #else - if (fread (data, (int) sizeof (char), (int) len, file) != len) { + if (fread (data, sizeof (char), len, file) != len) { bzero (data, len); free (data); return 0; @@ -109,30 +109,29 @@ read_counted_string (unsigned short *countp, char **stringp, FILE *file) } Xauth * -XauReadAuth (auth_file) -FILE *auth_file; +XauReadAuth (FILE *auth_file) { Xauth local; Xauth *ret; if (read_short (&local.family, auth_file) == 0) - return 0; + return NULL; if (read_counted_string (&local.address_length, &local.address, auth_file) == 0) - return 0; + return NULL; if (read_counted_string (&local.number_length, &local.number, auth_file) == 0) { free (local.address); - return 0; + return NULL; } if (read_counted_string (&local.name_length, &local.name, auth_file) == 0) { free (local.address); free (local.number); - return 0; + return NULL; } if (read_counted_string (&local.data_length, &local.data, auth_file) == 0) { free (local.address); free (local.number); free (local.name); - return 0; + return NULL; } ret = (Xauth *) malloc (sizeof (Xauth)); if (!ret) { @@ -143,7 +142,7 @@ FILE *auth_file; bzero (local.data, local.data_length); free (local.data); } - return 0; + return NULL; } *ret = local; return ret; -- cgit v1.2.3