Hello Ludo,

Ludovic Courtès wrote in
<http://lists.gnu.org/archive/html/bug-libunistring/2010-07/msg00003.html>:
> In the example below, conversion from UTF-8 to UCS-4 succeeds but
> ‘u32_conv_from_encoding’ allocates memory on the heap although the
> supplied buffer appears to be large enough:

I'm applying this fix in gnulib. The fix will be contained in the next
libunistring release.

Thanks for a perfectly written and reproducible test case!


2010-07-13  Bruno Haible  <br...@clisp.org>

        striconveh: Don't malloc memory if the result buffer is sufficient.
        * lib/striconveh.c (mem_cd_iconveh_internal): Use the provided result
        buffer if its size is sufficient.
        Reported by Ludovic Courtès <l...@gnu.org>.

*** lib/striconveh.c.orig       Tue Jul 13 23:34:23 2010
--- lib/striconveh.c    Tue Jul 13 23:29:21 2010
***************
*** 970,987 ****
    if (result == tmpbuf)
      {
        size_t memsize = length + extra_alloc;
-       char *memory;
  
!       memory = (char *) malloc (memsize > 0 ? memsize : 1);
!       if (memory != NULL)
          {
!           memcpy (memory, tmpbuf, length);
!           result = memory;
          }
        else
          {
!           errno = ENOMEM;
!           return -1;
          }
      }
    else if (result != *resultp && length + extra_alloc < allocated)
--- 970,996 ----
    if (result == tmpbuf)
      {
        size_t memsize = length + extra_alloc;
  
!       if (*resultp != NULL && *lengthp >= memsize)
          {
!           result = *resultp;
!           memcpy (result, tmpbuf, length);
          }
        else
          {
!           char *memory;
! 
!           memory = (char *) malloc (memsize > 0 ? memsize : 1);
!           if (memory != NULL)
!             {
!               memcpy (memory, tmpbuf, length);
!               result = memory;
!             }
!           else
!             {
!               errno = ENOMEM;
!               return -1;
!             }
          }
      }
    else if (result != *resultp && length + extra_alloc < allocated)

Reply via email to