This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug libstdc++/18678] Garbage output to std::wcout under some circumstances


------- Additional Comments From rleigh at debian dot org  2004-11-29 22:38 -------
I've done a little more investigation, and I've found out some more.  It's no
longer related to date formatting, so I changed the bug title.

Here's a C program to test wide character output:

#include <locale.h>
#include <stdio.h>
#include <string.h>
#include <wchar.h>

int main(void)
{
  setlocale(LC_ALL, "");

  const char *narrow = "Test Unicode (narrow): ïÃ?ý Ð?оÑ?!";
  fprintf(stdout, "%s\n", narrow);

  if (fwide (stderr, 1) <= 0)
    fprintf(stdout, "Failed to set stderr to wide orientation\n");

  const wchar_t *wide = L"Test Unicode (wide): ïÃ?ý Ð?оÑ?!";
  fwprintf(stderr, L"%ls\n", wide);

  return 0;
}


This is the output for an en_GB UTF-8 locale with GCC 3.4.4:
$ ./unicode
Test Unicode (narrow): ïÃ?ý Ð?оÑ?!
Test Unicode (wide): ïÃ?ý Ð?оÑ?!

Perfect.  For comparison, GCC 3.3.5:
$ ./unicode
Test Unicode (narrow): ïÃ?ý Ð?оÑ?!
Test Unicode (wide): �¯� �½ ���¾��!

Here's a C++ example:
#include <locale>
#include <string>
#include <iostream>

int main(void)
{
  // Set up locale stuff...
  std::locale::global(std::locale(""));
  std::wcout.imbue(std::locale());

  std::string narrow("Test Unicode (narrow): ïÃ?ý Ð?оÑ?!");
  std::cout << narrow << std::endl;

  std::wstring wide(L"Test Unicode (wide): ïÃ?ý Ð?оÑ?!");
  std::wcout << wide << std::endl;

  return 0;
}

This is the output with GCC 3.4.4:
$ ./unicode 
Test Unicode (narrow): ïÃ?ý Ð?оÑ?!
Test Unicode (wide): ��>O!

This is the output with GCC 3.3.5:
$ ./unicode 
Test Unicode (narrow): ïÃ?ý Ð?оÑ?!
Test Unicode (wide): 

If I remove the output to std::cout, I get this:
GCC 3.4.4:
$ ./unicode 
Test Unicode (wide): ïÃ?ý Ð?оÑ?!

GCC 3.3.5:
$ ./unicode 
Test Unicode (wide): 

So, GCC 3.3.5 is totally broken WRT wide output.  GCC 3.4.4 is *much* better,
but still not perfect.  The draft C standard says fwide() will not change a
stream's orientation once set, but I can seemingly output to mixed wide and
narrow streams without problems if I repeat the printf() lines in the C listing
(not that I'm complaining--I'm very pleased!).

The same does not appear to hold true for C++.  If I output to std::cout first,
all subsequent output to std::wcout is corrupted.  If I output to std::wcout
first, all subsequent output to std::cout is completely missing.  Although this
is probably going well outside the C++ spec, there seems to be no reason why I
can't do both: since both end up being output as UTF-8 anyway, at least on
GNU/Linux with UTF-8 locales, this seems a reasonable thing to do.

Regards,
Roger

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |UNCONFIRMED
         Resolution|INVALID                     |
            Summary|std::time_put<wchar_t> is   |Garbage output to std::wcout
                   |broken with UTF-8 locales   |under some circumstances


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18678


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]