[PATCH] libtest: Fix use of flexible array member

Sebastian Huber sebastian.huber at embedded-brains.de
Sun May 2 19:01:12 UTC 2021


Flexible array members must not appear in the middle of a structure.
---
 cpukit/include/rtems/test.h           | 18 +++++++++-------
 cpukit/libtest/t-test-thread-switch.c | 24 ++++++++++-----------
 testsuites/libtests/ttest02/init.c    | 30 +++++++++++++--------------
 3 files changed, 37 insertions(+), 35 deletions(-)

diff --git a/cpukit/include/rtems/test.h b/cpukit/include/rtems/test.h
index 10ff665107..f95433bc7c 100644
--- a/cpukit/include/rtems/test.h
+++ b/cpukit/include/rtems/test.h
@@ -89,10 +89,8 @@ typedef struct T_fixture_node {
 
 #if defined(__GNUC__) || __STDC_VERSION__ >= 199409L
 #define T_ZERO_LENGTH_ARRAY
-#define T_ZERO_LENGTH_ARRAY_EXTENSION(n) (n)
 #else
 #define T_ZERO_LENGTH_ARRAY 1
-#define T_ZERO_LENGTH_ARRAY_EXTENSION(n) ((n) - 1)
 #endif
 
 /** @} */
@@ -2434,22 +2432,26 @@ typedef struct {
 	size_t recorded;
 	size_t capacity;
 	uint64_t switches;
+} T_thread_switch_header;
+
+typedef struct {
+	T_thread_switch_header header;
 	T_thread_switch_event events[T_ZERO_LENGTH_ARRAY];
 } T_thread_switch_log;
 
 typedef struct {
-	T_thread_switch_log log;
-	T_thread_switch_event events[T_ZERO_LENGTH_ARRAY_EXTENSION(2)];
+	T_thread_switch_header header;
+	T_thread_switch_event events[2];
 } T_thread_switch_log_2;
 
 typedef struct {
-	T_thread_switch_log log;
-	T_thread_switch_event events[T_ZERO_LENGTH_ARRAY_EXTENSION(4)];
+	T_thread_switch_header header;
+	T_thread_switch_event events[4];
 } T_thread_switch_log_4;
 
 typedef struct {
-	T_thread_switch_log log;
-	T_thread_switch_event events[T_ZERO_LENGTH_ARRAY_EXTENSION(10)];
+	T_thread_switch_header header;
+	T_thread_switch_event events[10];
 } T_thread_switch_log_10;
 
 T_thread_switch_log *T_thread_switch_record(T_thread_switch_log *);
diff --git a/cpukit/libtest/t-test-thread-switch.c b/cpukit/libtest/t-test-thread-switch.c
index 87ad4651fc..60179aaa40 100644
--- a/cpukit/libtest/t-test-thread-switch.c
+++ b/cpukit/libtest/t-test-thread-switch.c
@@ -96,11 +96,11 @@ T_thread_switch_recorder(Thread_Control *executing, Thread_Control *heir)
 	if (log != NULL) {
 		size_t recorded;
 
-		++log->switches;
-		recorded = log->recorded;
+		++log->header.switches;
+		recorded = log->header.recorded;
 
-		if (recorded < log->capacity) {
-			log->recorded = recorded + 1;
+		if (recorded < log->header.capacity) {
+			log->header.recorded = recorded + 1;
 			log->events[recorded].executing = executing->Object.id;
 			log->events[recorded].heir = heir->Object.id;
 			log->events[recorded].cpu =
@@ -127,8 +127,8 @@ T_thread_switch_record(T_thread_switch_log *log)
 	}
 
 	if (log != NULL) {
-		log->recorded = 0;
-		log->switches = 0;
+		log->header.recorded = 0;
+		log->header.switches = 0;
 	}
 
 	rtems_interrupt_lock_acquire(&ctx->lock, &lock_context);
@@ -142,20 +142,20 @@ T_thread_switch_record(T_thread_switch_log *log)
 T_thread_switch_log *
 T_thread_switch_record_2(T_thread_switch_log_2 *log)
 {
-	log->log.capacity = 2;
-	return T_thread_switch_record(&log->log);
+	log->header.capacity = T_ARRAY_SIZE(log->events);
+	return T_thread_switch_record((T_thread_switch_log *)log);
 }
 
 T_thread_switch_log *
 T_thread_switch_record_4(T_thread_switch_log_4 *log)
 {
-	log->log.capacity = 4;
-	return T_thread_switch_record(&log->log);
+	log->header.capacity = T_ARRAY_SIZE(log->events);
+	return T_thread_switch_record((T_thread_switch_log *)log);
 }
 
 T_thread_switch_log *
 T_thread_switch_record_10(T_thread_switch_log_10 *log)
 {
-	log->log.capacity = 10;
-	return T_thread_switch_record(&log->log);
+	log->header.capacity = T_ARRAY_SIZE(log->events);
+	return T_thread_switch_record((T_thread_switch_log *)log);
 }
diff --git a/testsuites/libtests/ttest02/init.c b/testsuites/libtests/ttest02/init.c
index 7f972aec7e..4d540c9a86 100644
--- a/testsuites/libtests/ttest02/init.c
+++ b/testsuites/libtests/ttest02/init.c
@@ -222,23 +222,23 @@ T_TEST_CASE(TestThreadSwitch)
 	memset(&log_2, 0xff, sizeof(log_2));
 	log = T_thread_switch_record_2(&log_2);
 	T_null(log);
-	T_eq_sz(log_2.log.recorded, 0);
-	T_eq_sz(log_2.log.capacity, 2);
-	T_eq_u64(log_2.log.switches, 0);
+	T_eq_sz(log_2.header.recorded, 0);
+	T_eq_sz(log_2.header.capacity, 2);
+	T_eq_u64(log_2.header.switches, 0);
 
 	memset(&log_4, 0xff, sizeof(log_4));
 	log = T_thread_switch_record_4(&log_4);
-	T_eq_ptr(log, &log_2.log);
-	T_eq_sz(log_4.log.recorded, 0);
-	T_eq_sz(log_4.log.capacity, 4);
-	T_eq_u64(log_4.log.switches, 0);
+	T_eq_ptr(&log->header, &log_2.header);
+	T_eq_sz(log_4.header.recorded, 0);
+	T_eq_sz(log_4.header.capacity, 4);
+	T_eq_u64(log_4.header.switches, 0);
 
 	memset(&log_10, 0xff, sizeof(log_10));
 	log = T_thread_switch_record_10(&log_10);
-	T_eq_ptr(log, &log_4.log);
-	T_eq_sz(log_10.log.recorded, 0);
-	T_eq_sz(log_10.log.capacity, 10);
-	T_eq_u64(log_10.log.switches, 0);
+	T_eq_ptr(&log->header, &log_4.header);
+	T_eq_sz(log_10.header.recorded, 0);
+	T_eq_sz(log_10.header.capacity, 10);
+	T_eq_u64(log_10.header.switches, 0);
 
 	for (i = 0; i < 6; ++i) {
 		rtems_status_code sc;
@@ -248,10 +248,10 @@ T_TEST_CASE(TestThreadSwitch)
 	}
 
 	log = T_thread_switch_record(NULL);
-	T_eq_ptr(log, &log_10.log);
-	T_eq_sz(log->recorded, 10);
-	T_eq_sz(log->capacity, 10);
-	T_eq_u64(log->switches, 12);
+	T_eq_ptr(&log->header, &log_10.header);
+	T_eq_sz(log->header.recorded, 10);
+	T_eq_sz(log->header.capacity, 10);
+	T_eq_u64(log->header.switches, 12);
 	executing = rtems_task_self();
 	T_eq_u32(log->events[0].executing, executing);
 	T_ne_u32(log->events[0].heir, 0);
-- 
2.26.2



More information about the devel mailing list