Bug 38104

Summary: gcc fails to compile programs if va_arg macro is used with types other than int
Product: [Retired] Red Hat Linux Reporter: Michael McTernan <mm7323>
Component: gccAssignee: Jakub Jelinek <jakub>
Status: CLOSED NOTABUG QA Contact: David Lawrence <dkl>
Severity: medium Docs Contact:
Priority: medium    
Version: 7.1   
Target Milestone: ---   
Target Release: ---   
Hardware: i386   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2001-04-27 19:13:49 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Michael McTernan 2001-04-27 19:13:44 UTC
From Bugzilla Helper:
User-Agent: Mozilla/4.76 [en] (X11; U; Linux 2.4.2-2 i686)


[mm7323@mike bug]$ cat bug.c 
#include <stdarg.h>
#include <stdio.h>

typedef unsigned short  myType;

void func(int n,...) {
 va_list ap;
 myType b;

 va_start(ap,n);
 b=va_arg(ap,myType);
 va_end(ap);
 
 printf("%d\n",b); 
}

int main(int argC,char *argV[]) {
  func(1,12345);
  return 0;
}
[mm7323@mike bug]$ gcc bug.c
bug.c: In function `func':
bug.c:11: `myType' is promoted to `int' when passed through `...'
bug.c:11: (so you should pass `int' not `myType' to `va_arg')
[mm7323@mike bug]$ 


Reproducible: Always
Steps to Reproduce:
1. Attempt to compile the test program given in the description
2.
3.
	

Actual Results:  bug.c: In function `func':
bug.c:11: `myType' is promoted to `int' when passed through `...'
bug.c:11: (so you should pass `int' not `myType' to `va_arg')

Expected Results:  It should just compile making a.out.  I understood that
anything can be passed to va_arg, not just ints. 

This prevents some software from being compiled e.g. lib icq...

Comment 1 Jakub Jelinek 2001-04-27 23:41:33 UTC
C promotes char and short to int when passing to ... arguments. The compiler
is right to make it obvious this is a bug. A varargs function will simply
never get a char or short as vararg, therefore if you want to pass a short
through varargs, you should use (myType)va_arg(ap, int).
Similarly with float (which is promoted to double).
If there are programs which don't do this, they should be fixed.