From 18d002cfb2bc22c9a9dfe25e4c1e56d2d4bcdc59 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Thu, 18 May 2017 22:04:54 +0200 Subject: [PATCH] Update from gnulib. --- ChangeLog | 5 +++++ lib/relocatable.c | 26 +++++++++++++++++++------- lib/relocatable.h | 26 +++++++++++++++++++++++--- libcharset/lib/ChangeLog | 13 +++++++++++++ libcharset/lib/localcharset.c | 10 +++++++--- libcharset/lib/relocatable.c | 26 +++++++++++++++++++------- libcharset/lib/relocatable.h | 26 +++++++++++++++++++++++--- 7 files changed, 109 insertions(+), 23 deletions(-) diff --git a/ChangeLog b/ChangeLog index c46dd8c..e991248 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2017-05-18 Bruno Haible + + * lib/relocatable.h: Update from gnulib. + * lib/relocatable.c: Update from gnulib. + 2017-01-30 Bruno Haible Prepare for version 1.15. diff --git a/lib/relocatable.c b/lib/relocatable.c index 6d3658f..63ef316 100644 --- a/lib/relocatable.c +++ b/lib/relocatable.c @@ -1,5 +1,5 @@ /* Provide relocatable packages. - Copyright (C) 2003-2006, 2008-2012 Free Software Foundation, Inc. + Copyright (C) 2003-2006, 2008-2017 Free Software Foundation, Inc. Written by Bruno Haible , 2003. This program is free software; you can redistribute it and/or modify it @@ -542,27 +542,26 @@ relocate (const char *pathname) # ifdef __KLIBC__ # undef strncmp - if (pathname && strncmp (pathname, "/@unixroot", 10) == 0 - && (pathname[10] == '\0' || pathname[10] == '/' || pathname[10] == '\\')) + if (strncmp (pathname, "/@unixroot", 10) == 0 + && (pathname[10] == '\0' || ISSLASH (pathname[10]))) { /* kLIBC itself processes /@unixroot prefix */ - return pathname; } else # endif - if (pathname && ISSLASH (pathname[0])) + if (ISSLASH (pathname[0])) { const char *unixroot = getenv ("UNIXROOT"); - if (unixroot && HAS_DEVICE (unixroot) && !unixroot[2]) + if (unixroot && HAS_DEVICE (unixroot) && unixroot[2] == '\0') { char *result = (char *) xmalloc (2 + strlen (pathname) + 1); #ifdef NO_XMALLOC if (result != NULL) #endif { - strcpy (result, unixroot); + memcpy (result, unixroot, 2); strcpy (result + 2, pathname); return result; } @@ -574,4 +573,17 @@ relocate (const char *pathname) return pathname; } +/* Returns the pathname, relocated according to the current installation + directory. + This function sets *ALLOCATEDP to the allocated memory, or to NULL if + no memory allocation occurs. So that, after you're done with the return + value, to reclaim allocated memory, you can do: free (*ALLOCATEDP). */ +const char * +relocate2 (const char *pathname, char **allocatedp) +{ + const char *result = relocate (pathname); + *allocatedp = (result != pathname ? (char *) result : NULL); + return result; +} + #endif diff --git a/lib/relocatable.h b/lib/relocatable.h index d83fe37..37a3d1f 100644 --- a/lib/relocatable.h +++ b/lib/relocatable.h @@ -1,5 +1,5 @@ /* Provide relocatable packages. - Copyright (C) 2003, 2005, 2008-2011 Free Software Foundation, Inc. + Copyright (C) 2003, 2005, 2008-2017 Free Software Foundation, Inc. Written by Bruno Haible , 2003. This program is free software; you can redistribute it and/or modify it @@ -52,10 +52,29 @@ extern RELOCATABLE_DLL_EXPORTED void string that you can free with free() after casting it to 'char *'. */ extern const char * relocate (const char *pathname); +/* Returns the pathname, relocated according to the current installation + directory. + This function sets *ALLOCATEDP to the allocated memory, or to NULL if + no memory allocation occurs. So that, after you're done with the return + value, to reclaim allocated memory, you can do: free (*ALLOCATEDP). */ +extern const char * relocate2 (const char *pathname, char **allocatedp); + /* Memory management: relocate() potentially allocates memory, because it has to construct a fresh pathname. If this is a problem because your program - calls relocate() frequently, think about caching the result. Or free the - return value if it was different from the argument pathname. */ + calls relocate() frequently or because you want to fix all potential memory + leaks anyway, you have three options: + 1) Use this idiom: + const char *pathname = ...; + const char *rel_pathname = relocate (pathname); + ... + if (rel_pathname != pathname) + free ((char *) rel_pathname); + 2) Use this idiom: + char *allocated; + const char *rel_pathname = relocate2 (..., &allocated); + ... + free (allocated); + 3) Think about caching the result. */ /* Convenience function: Computes the current installation prefix, based on the original @@ -70,6 +89,7 @@ extern char * compute_curr_prefix (const char *orig_installprefix, /* By default, we use the hardwired pathnames. */ #define relocate(pathname) (pathname) +#define relocate2(pathname,allocatedp) (*(allocatedp) = NULL, (pathname)) #endif diff --git a/libcharset/lib/ChangeLog b/libcharset/lib/ChangeLog index 63cd184..cc0df68 100644 --- a/libcharset/lib/ChangeLog +++ b/libcharset/lib/ChangeLog @@ -1,3 +1,16 @@ +2017-05-18 Bruno Haible + + * localcharset.c: Update from gnulib. + 2017-05-16 Bruno Haible + * localcharset.c (relocate2): Define fallback. + (get_charset_aliases): Invoke relocate2 instead of relocate. + Free the allocated memory. + 2017-04-21 Bruno Haible + * localcharset.c (WINDOWS_NATIVE): Don't define on Cygwin. + + * relocatable.h: Update from gnulib. + * relocatable.c: Update from gnulib. + 2016-12-14 Bruno Haible Cleanup useless removals. diff --git a/libcharset/lib/localcharset.c b/libcharset/lib/localcharset.c index f3d7f27..ae98472 100644 --- a/libcharset/lib/localcharset.c +++ b/libcharset/lib/localcharset.c @@ -1,6 +1,6 @@ /* Determine a canonical name for the current locale's character encoding. - Copyright (C) 2000-2006, 2008-2012 Free Software Foundation, Inc. + Copyright (C) 2000-2006, 2008-2017 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published @@ -32,7 +32,7 @@ # define DARWIN7 /* Darwin 7 or newer, i.e. Mac OS X 10.3 or newer */ #endif -#if defined _WIN32 || defined __WIN32__ +#if (defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__ # define WINDOWS_NATIVE # include #endif @@ -75,6 +75,7 @@ # include "relocatable.h" #else # define relocate(pathname) (pathname) +# define relocate2(pathname,allocatedp) (*(allocatedp) = NULL, (pathname)) #endif /* Get LIBDIR. */ @@ -129,6 +130,7 @@ get_charset_aliases (void) if (cp == NULL) { #if !(defined DARWIN7 || defined VMS || defined WINDOWS_NATIVE || defined __CYGWIN__ || defined OS2) + char *malloc_dir = NULL; const char *dir; const char *base = "charset.alias"; char *file_name; @@ -137,7 +139,7 @@ get_charset_aliases (void) necessary for running the testsuite before "make install". */ dir = getenv ("CHARSETALIASDIR"); if (dir == NULL || dir[0] == '\0') - dir = relocate (LIBDIR); + dir = relocate2 (LIBDIR, &malloc_dir); /* Concatenate dir and base into freshly allocated file_name. */ { @@ -154,6 +156,8 @@ get_charset_aliases (void) } } + free (malloc_dir); + if (file_name == NULL) /* Out of memory. Treat the file as empty. */ cp = ""; diff --git a/libcharset/lib/relocatable.c b/libcharset/lib/relocatable.c index 6d3658f..63ef316 100644 --- a/libcharset/lib/relocatable.c +++ b/libcharset/lib/relocatable.c @@ -1,5 +1,5 @@ /* Provide relocatable packages. - Copyright (C) 2003-2006, 2008-2012 Free Software Foundation, Inc. + Copyright (C) 2003-2006, 2008-2017 Free Software Foundation, Inc. Written by Bruno Haible , 2003. This program is free software; you can redistribute it and/or modify it @@ -542,27 +542,26 @@ relocate (const char *pathname) # ifdef __KLIBC__ # undef strncmp - if (pathname && strncmp (pathname, "/@unixroot", 10) == 0 - && (pathname[10] == '\0' || pathname[10] == '/' || pathname[10] == '\\')) + if (strncmp (pathname, "/@unixroot", 10) == 0 + && (pathname[10] == '\0' || ISSLASH (pathname[10]))) { /* kLIBC itself processes /@unixroot prefix */ - return pathname; } else # endif - if (pathname && ISSLASH (pathname[0])) + if (ISSLASH (pathname[0])) { const char *unixroot = getenv ("UNIXROOT"); - if (unixroot && HAS_DEVICE (unixroot) && !unixroot[2]) + if (unixroot && HAS_DEVICE (unixroot) && unixroot[2] == '\0') { char *result = (char *) xmalloc (2 + strlen (pathname) + 1); #ifdef NO_XMALLOC if (result != NULL) #endif { - strcpy (result, unixroot); + memcpy (result, unixroot, 2); strcpy (result + 2, pathname); return result; } @@ -574,4 +573,17 @@ relocate (const char *pathname) return pathname; } +/* Returns the pathname, relocated according to the current installation + directory. + This function sets *ALLOCATEDP to the allocated memory, or to NULL if + no memory allocation occurs. So that, after you're done with the return + value, to reclaim allocated memory, you can do: free (*ALLOCATEDP). */ +const char * +relocate2 (const char *pathname, char **allocatedp) +{ + const char *result = relocate (pathname); + *allocatedp = (result != pathname ? (char *) result : NULL); + return result; +} + #endif diff --git a/libcharset/lib/relocatable.h b/libcharset/lib/relocatable.h index d83fe37..37a3d1f 100644 --- a/libcharset/lib/relocatable.h +++ b/libcharset/lib/relocatable.h @@ -1,5 +1,5 @@ /* Provide relocatable packages. - Copyright (C) 2003, 2005, 2008-2011 Free Software Foundation, Inc. + Copyright (C) 2003, 2005, 2008-2017 Free Software Foundation, Inc. Written by Bruno Haible , 2003. This program is free software; you can redistribute it and/or modify it @@ -52,10 +52,29 @@ extern RELOCATABLE_DLL_EXPORTED void string that you can free with free() after casting it to 'char *'. */ extern const char * relocate (const char *pathname); +/* Returns the pathname, relocated according to the current installation + directory. + This function sets *ALLOCATEDP to the allocated memory, or to NULL if + no memory allocation occurs. So that, after you're done with the return + value, to reclaim allocated memory, you can do: free (*ALLOCATEDP). */ +extern const char * relocate2 (const char *pathname, char **allocatedp); + /* Memory management: relocate() potentially allocates memory, because it has to construct a fresh pathname. If this is a problem because your program - calls relocate() frequently, think about caching the result. Or free the - return value if it was different from the argument pathname. */ + calls relocate() frequently or because you want to fix all potential memory + leaks anyway, you have three options: + 1) Use this idiom: + const char *pathname = ...; + const char *rel_pathname = relocate (pathname); + ... + if (rel_pathname != pathname) + free ((char *) rel_pathname); + 2) Use this idiom: + char *allocated; + const char *rel_pathname = relocate2 (..., &allocated); + ... + free (allocated); + 3) Think about caching the result. */ /* Convenience function: Computes the current installation prefix, based on the original @@ -70,6 +89,7 @@ extern char * compute_curr_prefix (const char *orig_installprefix, /* By default, we use the hardwired pathnames. */ #define relocate(pathname) (pathname) +#define relocate2(pathname,allocatedp) (*(allocatedp) = NULL, (pathname)) #endif -- 2.11.4.GIT