Ejemplo n.º 1
0
size_t
__mbstowcs_chk (wchar_t *dst, const char *src, size_t len, size_t dstlen)
{
  if (__builtin_expect (dstlen < len, 0))
    __chk_fail ();

  mbstate_t state;

  memset (&state, '\0', sizeof state);
  /* Return how many we wrote (or maybe an error).  */
  return __mbsrtowcs (dst, &src, len, &state);
}
Ejemplo n.º 2
0
static void
convert_and_print (const char *format, __gnuc_va_list ap)
{
#define ALLOCA_LIMIT	2000
  size_t len;
  wchar_t *wformat = NULL;
  mbstate_t st;
  size_t res;
  const char *tmp;

  if (format == NULL)
    return;

  len = strlen (format) + 1;

  do
    {
      if (len < ALLOCA_LIMIT)
	wformat = (wchar_t *) alloca (len * sizeof (wchar_t));
      else
	{
	  if (wformat != NULL && len / 2 < ALLOCA_LIMIT)
	    wformat = NULL;

	  wformat = (wchar_t *) realloc (wformat, len * sizeof (wchar_t));

	  if (wformat == NULL)
	    {
	      fputws_unlocked (L"out of memory\n", stderr);
	      return;
	    }
	}

      memset (&st, '\0', sizeof (st));
      tmp =format;
    }
  while ((res = __mbsrtowcs (wformat, &tmp, len, &st)) == len);

  if (res == (size_t) -1)
    /* The string cannot be converted.  */
    wformat = (wchar_t *) L"???";

  __vfwprintf (stderr, wformat, ap);
}
Ejemplo n.º 3
0
static int
locked_vfxprintf (FILE *fp, const char *fmt, va_list ap,
		  unsigned int mode_flags)
{
  if (_IO_fwide (fp, 0) <= 0)
    return __vfprintf_internal (fp, fmt, ap, mode_flags);

  /* We must convert the narrow format string to a wide one.
     Each byte can produce at most one wide character.  */
  wchar_t *wfmt;
  mbstate_t mbstate;
  int res;
  int used_malloc = 0;
  size_t len = strlen (fmt) + 1;

  if (__glibc_unlikely (len > SIZE_MAX / sizeof (wchar_t)))
    {
      __set_errno (EOVERFLOW);
      return -1;
    }
  if (__libc_use_alloca (len * sizeof (wchar_t)))
    wfmt = alloca (len * sizeof (wchar_t));
  else if ((wfmt = malloc (len * sizeof (wchar_t))) == NULL)
    return -1;
  else
    used_malloc = 1;

  memset (&mbstate, 0, sizeof mbstate);
  res = __mbsrtowcs (wfmt, &fmt, len, &mbstate);

  if (res != -1)
    res = __vfwprintf_internal (fp, wfmt, ap, mode_flags);

  if (used_malloc)
    free (wfmt);

  return res;
}