www.delorie.com/djgpp/bugs/show.cgi   search  
Bug 000366

When Created: 04/26/2004 10:42:43
Against DJGPP version: 2.03
By whom: lode.leroy@hotmail.com
Abstract: mbstowcs causes SIGSEGV when passed a NULL pointer
According to the documentation, the following should work

len = mbstowcs(NULL, s, 0);

However, this is implemented in libc/ansi/locale/mbstowcs.c, which is:

size_t
mbstowcs(wchar_t *wcs, const char *s, size_t n)
{
  int i;
  for (i=0; s[i] && (i<n-1); i++)
    wcs[i] = s[i];
  wcs[i] = 0;
  return i;
}

and should be

size_t
mbstowcs(wchar_t *wcs, const char *s, size_t n)
{
  int i;
  for (i=0; s[i] && (i<n-1); i++)
    if (wcs) wcs[i] = s[i];
  if (wcs) wcs[i] = 0;
  return i;
}

Solution added: 04/26/2004 10:45:07
By whom: lode.leroy@hotmail.com
size_t
mbstowcs(wchar_t *wcs, const char *s, size_t n)
{
  int i;
  for (i=0; s[i] && (i<n-1); i++)
    if (wcs) wcs[i] = s[i];
  if (wcs) wcs[i] = 0;
  return i;
}

Solution added: 04/27/2004 03:08:19
By whom: lode.leroy@hotmail.com
Or maybe, the solution should be

size_t
mbstowcs(wchar_t *wcs, const char *s, size_t n)
{
  int i;
  for (i=0; s[i]; i++)
    if ((wcs) && (i<n-1)) wcs[i] = s[i];
  if (wcs) 
    if (i<n-1) wcs[i] = 0; else wcs[n-1] = 0;
  return i;
}

Fixed in version cvs on 05/11/2005 16:06:40
By whom: ams@ludd.ltu.se



  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2010   by DJ Delorie     Updated Jul 2010