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 c++/12627] New: v_arg with short and unsigned short seg faults.


PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org.

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

           Summary: v_arg with short and unsigned short seg faults.
           Product: gcc
           Version: 3.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: masoud dot kermani at latticesemi dot com
                CC: gcc-bugs at gcc dot gnu dot org,masoud dot kermani at
                    latticesemi dot com

///////////////////////////////////////////////////////////////////////
// Here is a bug in gcc with va_args.
// To compile:
//    $ c++ --version
//        Reading specs from
/tools/gnu/bin/../lib/gcc-lib/i686-pc-linux-gnu/3.3.1/specs
//        Configured with: ../configure 
//        Thread model: posix
//        gcc version 3.3.1
//    $ c++ showbug.c -save-temps
//      In file included from /tools/gnu/include/c++/3.3.1/backward/iostream.h:31,
//                       from showbug.c:11:
//      /tools/gnu/include/c++/3.3.1/backward/backward_warning.h:32:2: warning:
#warning This file includes at least one deprecated or antiquated header. Please
consider using one of the 32 headers found in section 17.4.1.2 of the C++
standard. Examples include substituting the <X> header for the <X.h> header for
C++ includes, or <sstream> instead of the deprecated header <strstream.h>. To
disable this warning use -Wno-deprecated.
//      showbug.c: In function `void bugHere(const char*, char*)':
//      showbug.c:30: warning: `short int' is promoted to `int' when passed through 
//         `...'
//      showbug.c:30: warning: (so you should pass `int' not `short int' to
`va_arg')
//      showbug.c:31: warning: `short unsigned int' is promoted to `int' when
passed 
//         through `...'
//    $ a.out
//      Here we go:
//      Segmentation fault
//
// This program on Solaris and NT produces the following output
// with and compiles with no errors and/or warning.
//    Here we go:
//       1 2 3 4 5 6
//
///////////////////////////////////////////////////////////////////////

#include <iostream.h>
#include <stdarg.h>
 
void
bugHere(const char *fmt, va_list rest)
{
   while (*fmt) 
   {
      if (*fmt != '%') 
      {   
         cout << *fmt;
      }
      else
      {
         fmt++;
         if (*fmt) 
         {
            switch(*fmt)
            {
            case 'h': {          short x = va_arg(rest,          short); cout <<
x;   break; }
            case 'H': { unsigned short x = va_arg(rest, unsigned short); cout <<
x;   break; } 
            case 'l': {          long  x = va_arg(rest,          long ); cout <<
x;   break; } 
            case 'L': { unsigned long  x = va_arg(rest, unsigned long ); cout <<
x;   break; } 
            case 'i': {          int   x = va_arg(rest,          int  ); cout <<
x;   break; } 
            case 'I': { unsigned int   x = va_arg(rest, unsigned int  ); cout <<
x;   break; } 
            case 's': { const char *   x = va_arg(rest, const char *  ); cout <<
x;   break; } 
            case '%': {                                                  cout <<
'%'; break; } 
            default:  { cout << '%' << *fmt; }
            }                       
         }
      }
      fmt++;                     
   }                             
}                                

void
bugStart(const char *fmt, ...)
{
   va_list ap;
   va_start(ap, fmt);
   bugHere(fmt, ap);
   va_end( ap);
}      

int
main()
{
   const char *format = "   %h %h %H %H %H %H";
   short s0 = 1;
   short s1 = 2;

   unsigned short us0 = 3;
   unsigned short us1 = 4;
   unsigned short us2 = 5;
   unsigned short us3 = 6;
   cout << "Here we go:" << endl;
   bugStart(format, s0, s1, us0, us1, us2, us3);
   cout << endl;
   return(0);
}


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