APAR status
Closed as user error.
Error description
ABSTRACT: Incorrect behaviour of va_start()/va_arg() at -O2 ======== COMPILE COMMAND: xlC -O t.C =============== OS LEVEL: AIX 4.3.3, 5.1, and 5.2 ======== DESCRIPTION: =========== The customer's testcase is generating incorrect output at low opt, but correct output when no optimization is used or when IPA is used. The testcase uses va_start()/va_arg() to handle varying number of arguments to a function. The function, c, is defined as c(char *a1, char *a2, ...). The body of c uses va_arg to print the addresses of the 2nd and 3rd arguments. TESTCASE: ======== ******************** t.C ******************* #include <iostream> #include <stdarg.h> class t1 { public: void b(char *a1) { c(a1, "2", "3"); } protected: void c(char *a1, char *a2, ...) { va_list ml; va_start(ml, a1); char *dummy; dummy = a2; fprintf(stdout, "Adress a1 = %d/Value a1 = %s\n", a1, a1); fprintf(stdout, "Adress a2 = %d/Value a2 = %s\n", a2, a2); dummy = va_arg(ml, char*); fprintf(stdout, "Adress arg2 = %d/Value arg2 = %s\n", dummy, dummy); dummy = va_arg(ml, char*); fprintf(stdout, "Adress arg3 = %d/Value arg3 = %s\n", dummy, dummy); va_end(ml); } }; int main() { t1 mt1; mt1.b("1"); return 0; } ******************** t.C ******************* ACTUAL OUTPUT: ============= $ xlC -O t.C $ a.out Adress a1 = 268444288/Value a1 = 1 Adress a2 = 268444290/Value a2 = 2 Adress arg2 = -559038737/Value arg2 = Adress arg3 = 268444292/Value arg3 = 3 EXPECTED OUTPUT: =============== The expected output is that the values of a2 and arg2 should be the same. This correct output is seen when the testcase is compiled without optimization or with -O4 or -O5 (i.e. IPA turned on). $ xlC t.C $ a.out Adress a1 = 268444560/Value a1 = 1 Adress a2 = 268444562/Value a2 = 2 Adress arg2 = 268444562/Value arg2 = 2 Adress arg3 = 268444564/Value arg3 = 3 $
Local fix
Problem summary
Problem conclusion
Temporary fix
Comments
This problem is not a compiler defect, but is working as designed, according to the C++ Standard. The way that va_start is being used is incorrect. va_start is being called with the second parameter being the first parameter from the function definition, instead of the rightmost parameter (before the ', ...'). According to the C and C++ Standards, the second parameter of va_start() is the rightmost parameter in the parameter list of the function, right before the ", ...". In the testcase, va_start is called with the leftmost parameter instead of the rightmost one. Here is subclause 18.7 paragraph 3 from the C++ Standard, which addresses this: "... The parameter parmN is the identifier of the rightmost parameter in the variable parameter list of the function definition (the one just before the ...). If the parameter parmN is declared with a function, array, or reference type, or with a type that is not compatible with the type that results when passing an argument for which there is no parameter, the behaviour is undefined."
APAR Information
APAR number
IY51377
Reported component name
VA C++ PROF FOR
Reported component ID
5765F5600
Reported release
600
Status
CLOSED USE
PE
NoPE
HIPER
NoHIPER
Special Attention
NoSpecatt / Xsystem
Submitted date
2003-11-28
Closed date
2004-01-20
Last modified date
2004-01-20
APAR is sysrouted FROM one or more of the following:
APAR is sysrouted TO one or more of the following:
Fix information
Applicable component levels
[{"Business Unit":{"code":"BU058","label":"IBM Infrastructure w\/TPS"},"Product":{"code":"SSO198","label":"IBM Visualage C++ Professional for AIX"},"Component":"","ARM Category":[],"Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"6.0","Edition":"","Line of Business":{"code":"LOB57","label":"Power"}}]
Document Information
Modified date:
20 August 2024