bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [bug-libunistring] Avoiding heap allocations in ‘u32_conv_from_encod


From: Bruno Haible
Subject: Re: [bug-libunistring] Avoiding heap allocations in ‘u32_conv_from_encoding’
Date: Tue, 13 Jul 2010 23:44:01 +0200
User-agent: KMail/1.9.9

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  <address@hidden>

        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 <address@hidden>.

*** 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

[Prev in Thread] Current Thread [Next in Thread]