Skip to contentSkip to conversation

kselftest: vDSO: Fix accumulation of uninitialized ret when CLOCK_REALTIME is undefined

PATCH In review Colin Ian King 3 participants 4 messages Apr 17, 2023 – May 8, 2023

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kselftest: vDSO: Fix accumulation of uninitialized ret when CLOCK_REALTIME is undefined

In the unlikely case that CLOCK_REALTIME is not defined, variable ret is not initialized and further accumulation of return values to ret can leave ret in an undefined state. Fix this by initialized ret to zero and changing the assignment of ret to an accumulation for the CLOCK_REALTIME case.

Fixes 03f55c7952c9 ("kselftest: Extend vDSO selftest to clock_getres")
---
 tools/testing/selftests/vDSO/vdso_test_clock_getres.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/vDSO/vdso_test_clock_getres.c +2 -2
index 15dcee16ff72..38d46a8bf7cb 100644
@@ -84,12 +84,12 @@ static inline int vdso_test_clock(unsigned int clock_id)
 
 int main(int argc, char **argv)
 {
-	int ret;
+	int ret = 0;
 
 #if _POSIX_TIMERS > 0
 
 #ifdef CLOCK_REALTIME
-	ret = vdso_test_clock(CLOCK_REALTIME);
+	ret += vdso_test_clock(CLOCK_REALTIME);
 #endif
 
 #ifdef CLOCK_BOOTTIME
Signature · 1 line
2.30.2
maintainer Reviewed-by on v1 1/1
#1 lore raw

Hi Colin,

Quoted 5 lines · Colin Ian King

In the unlikely case that CLOCK_REALTIME is not defined, variable ret is not initialized and further accumulation of return values to ret can leave ret in an undefined state. Fix this by initialized ret to zero and changing the assignment of ret to an accumulation for the CLOCK_REALTIME case.

I was wondering how did you find this.

Apart that:

Quoted 25 lines
Fixes 03f55c7952c9 ("kselftest: Extend vDSO selftest to clock_getres")
---
 tools/testing/selftests/vDSO/vdso_test_clock_getres.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/vDSO/vdso_test_clock_getres.c +2 -2
index 15dcee16ff72..38d46a8bf7cb 100644
@@ -84,12 +84,12 @@ static inline int vdso_test_clock(unsigned int clock_id)
 
 int main(int argc, char **argv)
 {
-	int ret;
+	int ret = 0;
 
 #if _POSIX_TIMERS > 0
 
 #ifdef CLOCK_REALTIME
-	ret = vdso_test_clock(CLOCK_REALTIME);
+	ret += vdso_test_clock(CLOCK_REALTIME);
 #endif
 
 #ifdef CLOCK_BOOTTIME
Signature · 2 lines
Regards,
Vincenzo
#2 lore raw
Quoted 10 lines · Vincenzo Frascino

Hi Colin,

Quoted 5 lines · Colin Ian King

In the unlikely case that CLOCK_REALTIME is not defined, variable ret is not initialized and further accumulation of return values to ret can leave ret in an undefined state. Fix this by initialized ret to zero and changing the assignment of ret to an accumulation for the CLOCK_REALTIME case.

I was wondering how did you find this.

I used cppcheck --force --enable=all, this examines the #if defined() paths.

Quoted 31 lines

Apart that:

Quoted 25 lines
Fixes 03f55c7952c9 ("kselftest: Extend vDSO selftest to clock_getres")
---
  tools/testing/selftests/vDSO/vdso_test_clock_getres.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/vDSO/vdso_test_clock_getres.c +2 -2
index 15dcee16ff72..38d46a8bf7cb 100644
@@ -84,12 +84,12 @@ static inline int vdso_test_clock(unsigned int clock_id)
 
  int main(int argc, char **argv)
  {
-	int ret;
+	int ret = 0;
 
  #if _POSIX_TIMERS > 0
 
  #ifdef CLOCK_REALTIME
-	ret = vdso_test_clock(CLOCK_REALTIME);
+	ret += vdso_test_clock(CLOCK_REALTIME);
  #endif
 
  #ifdef CLOCK_BOOTTIME
maintainer
#3 lore raw
Quoted 10 lines · Colin King (gmail)
Quoted 9 lines · Vincenzo Frascino

Hi Colin,

Quoted 5 lines · Colin Ian King

In the unlikely case that CLOCK_REALTIME is not defined, variable ret is not initialized and further accumulation of return values to ret can leave ret in an undefined state. Fix this by initialized ret to zero and changing the assignment of ret to an accumulation for the CLOCK_REALTIME case.

Quoted 9 lines
Quoted 1 line

I was wondering how did you find this.

I used cppcheck --force --enable=all, this examines the #if defined() paths.

Quoted 5 lines

Apart that:

Applied to linux-kselftest next

thanks,
-- Shuah