@@ -1178,14 +1178,28 @@ struct foo {
11781178 s16 array [] __counted_by (counter );
11791179};
11801180
1181+ struct bar {
1182+ int a ;
1183+ u32 counter ;
1184+ s16 array [];
1185+ };
1186+
11811187static void DEFINE_FLEX_test (struct kunit * test )
11821188{
1183- DEFINE_RAW_FLEX (struct foo , two , array , 2 );
1189+ /* Using _RAW_ on a __counted_by struct will initialize "counter" to zero */
1190+ DEFINE_RAW_FLEX (struct foo , two_but_zero , array , 2 );
1191+ #if __has_attribute (__counted_by__ )
1192+ int expected_raw_size = sizeof (struct foo );
1193+ #else
1194+ int expected_raw_size = sizeof (struct foo ) + 2 * sizeof (s16 );
1195+ #endif
1196+ /* Without annotation, it will always be on-stack size. */
1197+ DEFINE_RAW_FLEX (struct bar , two , array , 2 );
11841198 DEFINE_FLEX (struct foo , eight , array , counter , 8 );
11851199 DEFINE_FLEX (struct foo , empty , array , counter , 0 );
11861200
1187- KUNIT_EXPECT_EQ (test , __struct_size (two ),
1188- sizeof ( struct foo ) + sizeof (s16 ) + sizeof (s16 ));
1201+ KUNIT_EXPECT_EQ (test , __struct_size (two_but_zero ), expected_raw_size );
1202+ KUNIT_EXPECT_EQ ( test , __struct_size ( two ), sizeof (struct bar ) + 2 * sizeof (s16 ));
11891203 KUNIT_EXPECT_EQ (test , __struct_size (eight ), 24 );
11901204 KUNIT_EXPECT_EQ (test , __struct_size (empty ), sizeof (struct foo ));
11911205}
0 commit comments