C 6.0 Incorrectly Passes Odd-Size Structs > 10 Bytes If Packed

ID Number: Q61975

6.00 | 6.00

MS-DOS | OS/2

buglist6.00 fixlist6.00a

Summary:

If you declare a structure of odd length greater than 10 and pass that

structure by value, the Microsoft C Compiler version 6.0 will pass a

correct size for the structure, but will pad the size of the structure

by an extra byte in the receiving function. The next byte on the stack

after the structure will be overwritten with a null padding character.

The best workaround for this problem is to manually pad the structure

to an even size.

Microsoft has confirmed this to be a problem in C version 6.0. This

problem was corrected in C version 6.0a.

More Information:

The sample code below illustrates this problem. An 11-byte structure

is declared and passed to a function, followed by another character

argument. The character following the structure is overwritten with a

null character, as shown in the program output.

Remove the comment from the struct ODDSTRUCT definition to add in a

char pad, and recompile to demonstrate the workaround for the problem.

Sample Code

-----------

/* Compile options needed: /Zp

*/

#include <stdio.h>

#include <string.h>

struct ODDSTRUCT

{

char string[11];

// char pad ; /* Remove comments to pad structure. */

} oddstruct;

void catchastruct ( struct ODDSTRUCT , char ) ;

void main ( void )

{

strcpy ( oddstruct.string, "0123456789" ) ;

printf ("Msg = %s\n", oddstruct.string ) ;

catchastruct ( oddstruct , '!' );

}

void catchastruct ( struct ODDSTRUCT oddstruct, char corrupted_char )

{

printf ( "Msg = %s\nCorrupted char = %c(char) or %x(hex)\n",

oddstruct.string , corrupted_char, corrupted_char ) ;

}

Program Output

--------------

Msg = 0123456789

Msg = 0123456789

Corrupted char = (char) or 0(hex)