changeset 31199:a540c36c3396

Avoid test failures on AIX and OSF/1.
author Bruno Haible <bruno@clisp.org>
date Sun, 26 Apr 2009 12:24:08 +0200
parents ba3299adf742
children 77113b8e5bf6
files ChangeLog doc/posix-functions/malloc.texi lib/uniconv/u-conv-to-enc.h lib/uniconv/u8-conv-from-enc.c lib/uniconv/u8-conv-to-enc.c lib/unilbrk/ulc-possible-linebreaks.c lib/unilbrk/ulc-width-linebreaks.c lib/unistr/u-cpy-alloc.h lib/uniwbrk/ulc-wordbreaks.c
diffstat 9 files changed, 41 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Sun Apr 26 12:00:43 2009 +0200
+++ b/ChangeLog	Sun Apr 26 12:24:08 2009 +0200
@@ -1,3 +1,18 @@
+2009-04-26  Bruno Haible  <bruno@clisp.org>
+
+	Avoid test failures on AIX and OSF/1.
+	* lib/uniconv/u8-conv-from-enc.c (u8_conv_from_encoding): Avoid calling
+	malloc(0).
+	* lib/uniconv/u8-conv-to-enc.c (u8_conv_to_encoding): Likewise.
+	* lib/unilbrk/ulc-possible-linebreaks.c (ulc_possible_linebreaks):
+	Likewise.
+	* lib/unilbrk/ulc-width-linebreaks.c (ulc_width_linebreaks): Likewise.
+	* lib/uniwbrk/ulc-wordbreaks.c (ulc_wordbreaks): Likewise.
+	* lib/uniconv/u-conv-to-enc.h (FUNC): Likewise. Fix memory leak.
+	* lib/unistr/u-cpy-alloc.h (FUNC): Call malloc(1) instead of malloc(0).
+	* doc/posix-functions/malloc.texi: Document the portability problem
+	related to malloc(0).
+
 2009-04-26  Bruno Haible  <bruno@clisp.org>
 
 	* modules/unistr/u8-cpy-alloc (Depends-on): Add malloc-posix.
--- a/doc/posix-functions/malloc.texi	Sun Apr 26 12:00:43 2009 +0200
+++ b/doc/posix-functions/malloc.texi	Sun Apr 26 12:24:08 2009 +0200
@@ -16,7 +16,10 @@
 
 Portability problems not fixed by Gnulib:
 @itemize
+@code{malloc (0)} always returns a NULL pointer on some platforms:
+AIX 5.1, OSF/1 5.1.
 @end itemize
 
 Extension: Gnulib provides a module @samp{malloc} that substitutes a
-@code{malloc} implementation that behaves more like the glibc implementation.
+@code{malloc} implementation that behaves more like the glibc implementation,
+regarding the result of @code{malloc (0)}.
--- a/lib/uniconv/u-conv-to-enc.h	Sun Apr 26 12:00:43 2009 +0200
+++ b/lib/uniconv/u-conv-to-enc.h	Sun Apr 26 12:24:08 2009 +0200
@@ -1,5 +1,5 @@
 /* Conversion from UTF-16/UTF-32 to legacy encodings.
-   Copyright (C) 2002, 2006-2008 Free Software Foundation, Inc.
+   Copyright (C) 2002, 2006-2009 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify it
    under the terms of the GNU Lesser General Public License as published
@@ -25,7 +25,7 @@
   size_t *scaled_offsets;
   int retval;
 
-  if (offsets != NULL)
+  if (offsets != NULL && srclen > 0)
     {
       scaled_offsets =
 	(size_t *) malloc (srclen * sizeof (UNIT) * sizeof (size_t));
@@ -70,7 +70,7 @@
     return -1;
   utf8_srclen = tmpbufsize;
 
-  if (offsets != NULL)
+  if (offsets != NULL && utf8_srclen > 0)
     {
       scaled_offsets = (size_t *) malloc (utf8_srclen * sizeof (size_t));
       if (scaled_offsets == NULL)
@@ -88,12 +88,11 @@
 				scaled_offsets, resultp, lengthp);
   if (retval < 0)
     {
+      int saved_errno = errno;
+      free (scaled_offsets);
       if (utf8_src != tmpbuf)
-	{
-	  int saved_errno = errno;
-	  free (utf8_src);
-	  errno = saved_errno;
-	}
+	free (utf8_src);
+      errno = saved_errno;
       return -1;
     }
   if (offsets != NULL)
--- a/lib/uniconv/u8-conv-from-enc.c	Sun Apr 26 12:00:43 2009 +0200
+++ b/lib/uniconv/u8-conv-from-enc.c	Sun Apr 26 12:24:08 2009 +0200
@@ -1,5 +1,5 @@
 /* Conversion to UTF-8 from legacy encodings.
-   Copyright (C) 2002, 2006-2007 Free Software Foundation, Inc.
+   Copyright (C) 2002, 2006-2007, 2009 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify it
    under the terms of the GNU Lesser General Public License as published
@@ -65,7 +65,7 @@
 	}
 
       /* Memory allocation.  */
-      if (*resultp != NULL && *lengthp >= srclen)
+      if ((*resultp != NULL && *lengthp >= srclen) || srclen == 0)
 	result = *resultp;
       else
 	{
--- a/lib/uniconv/u8-conv-to-enc.c	Sun Apr 26 12:00:43 2009 +0200
+++ b/lib/uniconv/u8-conv-to-enc.c	Sun Apr 26 12:24:08 2009 +0200
@@ -1,5 +1,5 @@
 /* Conversion from UTF-8 to legacy encodings.
-   Copyright (C) 2002, 2006-2007 Free Software Foundation, Inc.
+   Copyright (C) 2002, 2006-2007, 2009 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify it
    under the terms of the GNU Lesser General Public License as published
@@ -50,7 +50,7 @@
 #endif
 
       /* Memory allocation.  */
-      if (*resultp != NULL && *lengthp >= srclen)
+      if ((*resultp != NULL && *lengthp >= srclen) || srclen == 0)
 	result = *resultp;
       else
 	{
--- a/lib/unilbrk/ulc-possible-linebreaks.c	Sun Apr 26 12:00:43 2009 +0200
+++ b/lib/unilbrk/ulc-possible-linebreaks.c	Sun Apr 26 12:24:08 2009 +0200
@@ -1,5 +1,5 @@
 /* Line breaking of strings.
-   Copyright (C) 2001-2003, 2006-2008 Free Software Foundation, Inc.
+   Copyright (C) 2001-2003, 2006-2009 Free Software Foundation, Inc.
    Written by Bruno Haible <bruno@clisp.org>, 2001.
 
    This program is free software: you can redistribute it and/or modify it
@@ -62,9 +62,9 @@
 					 s, n, offsets, &t, &m)
 		  == 0)
 		{
-		  char *q = (char *) malloc (m);
+		  char *q = (char *) (m > 0 ? malloc (m) : NULL);
 
-		  if (q != NULL)
+		  if (m == 0 || q != NULL)
 		    {
 		      size_t i;
 
--- a/lib/unilbrk/ulc-width-linebreaks.c	Sun Apr 26 12:00:43 2009 +0200
+++ b/lib/unilbrk/ulc-width-linebreaks.c	Sun Apr 26 12:24:08 2009 +0200
@@ -1,5 +1,5 @@
 /* Line breaking of strings.
-   Copyright (C) 2001-2003, 2006-2008 Free Software Foundation, Inc.
+   Copyright (C) 2001-2003, 2006-2009 Free Software Foundation, Inc.
    Written by Bruno Haible <bruno@clisp.org>, 2001.
 
    This program is free software: you can redistribute it and/or modify it
@@ -64,9 +64,10 @@
 					 s, n, offsets, &t, &m)
 		  == 0)
 		{
-		  char *memory = (char *) malloc (m + (o != NULL ? m : 0));
+		  char *memory =
+		    (char *) (m > 0 ? malloc (m + (o != NULL ? m : 0)) : NULL);
 
-		  if (memory != NULL)
+		  if (m == 0 || memory != NULL)
 		    {
 		      char *q = (char *) memory;
 		      char *o8 = (o != NULL ? (char *) (q + m) : NULL);
--- a/lib/unistr/u-cpy-alloc.h	Sun Apr 26 12:00:43 2009 +0200
+++ b/lib/unistr/u-cpy-alloc.h	Sun Apr 26 12:24:08 2009 +0200
@@ -1,5 +1,5 @@
 /* Copy piece of UTF-8/UTF-16/UTF-32 string.
-   Copyright (C) 1999, 2002, 2006-2007 Free Software Foundation, Inc.
+   Copyright (C) 1999, 2002, 2006-2007, 2009 Free Software Foundation, Inc.
    Written by Bruno Haible <bruno@clisp.org>, 2002.
 
    This program is free software: you can redistribute it and/or modify it
@@ -23,7 +23,7 @@
 {
   UNIT *dest;
 
-  dest = (UNIT *) malloc (n * sizeof (UNIT));
+  dest = (UNIT *) malloc (n > 0 ? n * sizeof (UNIT) : 1);
   if (dest != NULL)
     {
 #if 0
--- a/lib/uniwbrk/ulc-wordbreaks.c	Sun Apr 26 12:00:43 2009 +0200
+++ b/lib/uniwbrk/ulc-wordbreaks.c	Sun Apr 26 12:24:08 2009 +0200
@@ -64,9 +64,9 @@
 					 s, n, offsets, &t, &m)
 		  == 0)
 		{
-		  char *q = (char *) malloc (m);
+		  char *q = (char *) (m > 0 ? malloc (m) : NULL);
 
-		  if (q != NULL)
+		  if (m == 0 || q != NULL)
 		    {
 		      size_t i;