Subject: port-i386/6862: Problems with va_start and gcc
To: None <gnats-bugs@gnats.netbsd.org>
From: None <steffen.stempel@xlink.net>
List: netbsd-bugs
Date: 01/22/1999 14:22:08
>Number:         6862
>Category:       port-i386
>Synopsis:       Problems with va_start and gcc
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    port-i386-maintainer (NetBSD/i386 Portmaster)
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Fri Jan 22 05:35:00 1999
>Last-Modified:
>Originator:     
>Organization:
Xlink Internet Consulting GmbH
	
>Release:        1.3.2
>Environment:


>Description:
    if the second parameter of va_start() is of type unsigned char
    the macro does not calculate the address of the variable argument
    list correctly, because the address of the unsigned char variable
    is "of sequence".
    This seems to be standard behavior with GCC (tested on Linux/gcc-2.7.2,
    Solaris 2.6/gcc-2.8.1, AIX4.2/gcc-2.8.1) but on these systems va_start()
    takes care of the "wrong" address.
>How-To-Repeat:
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

/*
** functions f works fine and prints: "0xefbfd680 0xefbfd684 0xefbfd68c 47 11"
**                                     &x         &a         ap         a
*/
void f(int x, int a, ...)
{
  va_list ap;
  va_start(ap,a);
  printf("%p %p %p %d %d\n",&x,&a,ap,a,va_arg(ap,int));
  va_end(ap);
}

/*
** function g fails and prints: "0xefbfd680 0xefbfd677 0xefbfd67f 47 1614319"
**                               &x         &a         ap         a
**   &a is __LESS__ than &x which means that a is stored incorrecly in
**   thge call stack.
*/

void g(int x, unsigned char a, ...)
{
  va_list ap;
  va_start(ap,a);
  printf("%p %p %p %d %d\n",&x,&a,ap,(int) a,va_arg(ap,int));
  va_end(ap);
}

int main(int argc, char **argv)
{
  f(0,47,11);
  g(0,47,11);

  return 0;
}
>Fix:
>Audit-Trail:
>Unformatted: