c++/10332: Template classes are not instantiated correctly in presense of #pragma pack()
igusarov@mail.ru
igusarov@mail.ru
Mon Apr 7 10:46:00 GMT 2003
>Number: 10332
>Category: c++
>Synopsis: Template classes are not instantiated correctly in presense of #pragma pack()
>Confidential: no
>Severity: serious
>Priority: medium
>Responsible: unassigned
>State: open
>Class: sw-bug
>Submitter-Id: net
>Arrival-Date: Mon Apr 07 10:46:02 UTC 2003
>Closed-Date:
>Last-Modified:
>Originator: Akella corp.
>Release: 3.2
>Organization:
>Environment:
System: FreeBSD gate.studio1001.akella.ru 4.4-RELEASE FreeBSD 4.4-RELEASE #11: Thu Jun 6 12:58:54
host: i386-unknown-freebsd4.4
build: i386-unknown-freebsd4.4
target: i386-unknown-freebsd4.4
configured with: /usr/src/gcc-3.2/configure --prefix=/usr
>Description:
The packing size used for class template instantiation is set by the #pragma pack directive which is in effect at the point of template instantiation (whether implicit or explicit), rather then by the #pragma pack directive which was in effect at the point of class template definition.
This is not a correct behaviour, because in a complex program it's hard to control the point of implicit instantiation of any given template class. And the wrong packing size is leading to incorrect code generation if a given template class was instantiadet with different packing sizes in different translation units. Consider the code examples in Howtorepeat section.
Related problem reports: other/4957
>How-To-Repeat:
1. Try to compile and run the following example (all code examples in this report could be compiled with "g++ file.cpp", no special command-line options are required)
//---------------------------- begin example
#include <stdio.h>
#pragma pack(4)
template <typename X >
struct T
{
char x1; /* Usually 3 padding bytes are added after x1 member. */
int x2;
};
#pragma pack(1)
template T<int>; /* T<int> is instantiated here */
#pragma pack(4)
template T<float>; /* T<float> is instantiated here */
int main()
{
printf("sizeof T<int> = %d\n", sizeof(T<int>));
printf("sizeof T<float> = %d\n", sizeof(T<float>));
}
//---------------------------- end example
(parameter types 'int' and 'float' are not used in any way, they are required just to separate two template instances)
The output is:
sizeof T<int> = 5
sizeof T<float> = 8
Which shows that the size (and member offsets) of the template class depends on the #pragma pack at place of instantiation.
2. There is a more complicated example which shows how to trigger this bug accidently.
Unpack the attached archive and compile two sample translation units (tu01.cpp and tu02.cpp) in alignment_bug/case_02 directory. The only difference between these translation units is the order of #include directives which causes different implicit instantiation of the same template class (include guards are skipped in such simple case). This test case repeats the behaviour reported in other/4957.
>Fix:
Do not rely on implicit instantiation. Instantiate all desired template classes explicitly (sic!).
>Release-Note:
>Audit-Trail:
>Unformatted:
----gnatsweb-attachment----
Content-Type: application/x-compressed; name="alignment_bug_02.tgz"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="alignment_bug_02.tgz"
H4sIAGdSkT4AA+1ZXWvbMBTNs3/FJWWQlJFa8hcsXWHsuWywFvYwKIqtJGb+wpa7eqX/fVe2kziJ
s4zNdVir85BE0tW9cq7Psa7FAn8RhTwSd7N8cTF4FoCpO5YFAwDiWER+I/T6u26AY5smNR3LsdCM
Og4dgPU8y9lGngmWAgz8RZ6xNL4/ZPdjyXnQx4L6BdvKv8syfqeTju+DP8+/RQhx0Mwkjq7y3wfa
888fWJgEfOImSQcxiK5jcg/n37acFf8x7xTNbJMYA9A7iH0Urzz/Z37kBrnH4TITnh9Plleadpak
bBEySJj7fWSONU1wvBuYQCNRJDxiIYevcKVlIs1dATfaoybT6C7lHynxQKb4eXEOt1nOgqAAA315
nh8tYFYIngFLOWAH94DNBU9xAoQ8nPF0AucXpTM/EvXd8UCn2tN0Z1VkvFnUzSUaX9URqwb4GXrA
zEbCRxMPlhwjouu9a2t4mQcxk35KL1XjsB+5vpD50Wgsrz5JsT0fDTP/J4/nq0UAvIc33rdo+Baq
gVE1MB5PW6bUEfenVANy0pPWef7b+E9Pqf+UlPpvm0r/+0B7/kWuk47Ef3Bc/6ltrPOPpmhmWSZR
+t8HWvR/3TUMCzdgWTZZDrVGr09kR6NNdwzCIhYolJu5vxPL0XXxSVp/lNbjPfnbGi0lsHsNfM04
yH96Iv4TWvLfcBT/+8Df8J/u8J8o/v+3aOf/OvGdxDjGf2Kt+a9TYsj6T7ctxf8+0FbarSu766Lk
3U59h9XdVokmC7StcWNn3KwKuFNfqkILDvG/qeD/GuM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