PRB:va_arg Fails on Odd-Size Structs Packed on 1-Byte Boundary

ID Number: Q67086

6.00 6.00a 6.00ax | 6.00 6.00a

MS-DOS | OS/2

buglist5.10 buglist6.00 buglist6.00a buglist6.00ax fixlist7.00

Summary:

SYMPTOMS

In Microsoft C versions 5.1, 6.0, 6.0a, and 6.0ax, because the

va_arg() macro does not handle odd-sized arguments properly, odd-

sized structures should not be passed by value to variable argument

functions if the structures are packed on 1-byte boundaries.

Because an even number of bytes is always put on the stack for each

argument passed, a packed structure with an odd size will not be

retrieved properly. Using an even packing size or passing the

structure by reference will eliminate this problem.

STATUS

Microsoft has confirmed this to be a problem in C versions 5.1,

6.0, 6.0a, and 6.0ax and QuickC versions 2.0, 2.01, 2.5, and 2.51

(buglist2.00, buglist2.01, buglist2.50, and buglist2.51). This

problem was corrected in C/C++ version 7.0.

More Information:

For more information on the va_arg() problem with odd-sized arguments,

query on the following words in the Microsoft Knowledge Base:

va_arg and va_start_char and char

To demonstrate this problem, assume a program contains code resembling

the following:

#pragma pack(1)

struct s1 {

int x;

char y[5];

} oddstruct;

If the structure "oddstruct" is passed to a function by value and that

function takes a variable number of arguments, then accessing the

structure from within the function with va_arg() will fail. The

sizeof(oddstruct) is actually 7 in this case, but 8 bytes will have

been pushed on the stack.

Additional reference words: 5.10 6.00 6.00a 6.00ax