overflow: Change DEFINE_FLEX to take __counted_by member
[sfrench/cifs-2.6.git] / lib / overflow_kunit.c
index 65e8a72a83bfaa9ccfd2c47b0290eedfa46520d3..4ef31b0bb74d682dbe83997609b2b259ddd600ae 100644 (file)
@@ -1172,6 +1172,24 @@ static void castable_to_type_test(struct kunit *test)
 #undef TEST_CASTABLE_TO_TYPE
 }
 
+struct foo {
+       int a;
+       u32 counter;
+       s16 array[] __counted_by(counter);
+};
+
+static void DEFINE_FLEX_test(struct kunit *test)
+{
+       DEFINE_RAW_FLEX(struct foo, two, array, 2);
+       DEFINE_FLEX(struct foo, eight, array, counter, 8);
+       DEFINE_FLEX(struct foo, empty, array, counter, 0);
+
+       KUNIT_EXPECT_EQ(test, __struct_size(two),
+                       sizeof(struct foo) + sizeof(s16) + sizeof(s16));
+       KUNIT_EXPECT_EQ(test, __struct_size(eight), 24);
+       KUNIT_EXPECT_EQ(test, __struct_size(empty), sizeof(struct foo));
+}
+
 static struct kunit_case overflow_test_cases[] = {
        KUNIT_CASE(u8_u8__u8_overflow_test),
        KUNIT_CASE(s8_s8__s8_overflow_test),
@@ -1194,6 +1212,7 @@ static struct kunit_case overflow_test_cases[] = {
        KUNIT_CASE(overflows_type_test),
        KUNIT_CASE(same_type_test),
        KUNIT_CASE(castable_to_type_test),
+       KUNIT_CASE(DEFINE_FLEX_test),
        {}
 };