Problems with va_arg... Severe problems

void
test(const char *str1, short num1, …) {
const char *str2;
va_list argv;
va_start(argv, num1);
str2 = va_arg(argv, const char *);
printf("%s %d %s\n", str1, num1, str2);
va_end(argv);
}
int
main(int argc, char *argv[]) {
test(“A”, 1, “B”);
return 0;
}

this program does SIGSEGV on my qnx 6.2.1 NC.
it’s my hands or it is really a bug?

Ilyak Kasnacheev <ilyak@online.ru> wrote:

void
test(const char *str1, short num1, …) {
const char *str2;
va_list argv;
va_start(argv, num1);
str2 = va_arg(argv, const char *);
printf("%s %d %s\n", str1, num1, str2);
va_end(argv);
}
int
main(int argc, char *argv[]) {
test(“A”, 1, “B”);
return 0;
}

this program does SIGSEGV on my qnx 6.2.1 NC.
it’s my hands or it is really a bug?

Yours, I’m afraid. Section 7.8.1.1 (1989 version) of the standard says:

If the parameter “parmN” is declared with the register storage class, with
a function or array type, or with a type that is not compatable with the
type that results after the application of the default argument promotion
rules, the behaviour is undefined.

“parmN” is the parm passed to va_start(). Since the default argument
promotion rules appied to “short” gives “int”, and “short” is not compatable
with “int”, the behaviour is undefined. If you change the type of “num1”
to an “int”, it works as intended.

\

Brian Stecher (bstecher@qnx.com) QNX Software Systems, Ltd.
phone: +1 (613) 591-0931 (voice) 175 Terence Matthews Cr.
+1 (613) 591-3579 (fax) Kanata, Ontario, Canada K2M 1W8

Logged on as Ilyak(Online)

If the parameter “parmN” is declared with the register storage class, with
a function or array type, or with a type that is not compatable with the
type that results after the application of the default argument promotion
rules, the behaviour is undefined.

“parmN” is the parm passed to va_start(). Since the default argument
promotion rules appied to “short” gives “int”, and “short” is not compatable
with “int”, the behaviour is undefined. If you change the type of “num1”
to an “int”, it works as intended.
Oh, i see…

But now, we have another piece of software running under QNX! The
Ur-Quan Masters, sc2.sf.net

Ilyak Kaznacheev <ilyak@online.ru> wrote:

Logged on as Ilyak(Online)

If the parameter “parmN” is declared with the register storage class, with
a function or array type, or with a type that is not compatable with the
type that results after the application of the default argument promotion
rules, the behaviour is undefined.

“parmN” is the parm passed to va_start(). Since the default argument
promotion rules appied to “short” gives “int”, and “short” is not compatable
with “int”, the behaviour is undefined. If you change the type of “num1”
to an “int”, it works as intended.

Oh, i see…

But now, we have another piece of software running under QNX! The
Ur-Quan Masters, sc2.sf.net

Cool. I actually helped someone else track this same bug down on irc
one night. Seems that GNU’s libc’s behavior is defined to “work regardless”.
So there is a decent amount of code written on Linux that doesn’t have any
issues until you build it on QNX. :wink:

chris


Chris McKillop <cdm@qnx.com> “The faster I go, the behinder I get.”
Software Engineer, QSSL – Lewis Carroll –
http://qnx.wox.org/