From 1d21e67e720a08992e81d626cff9d76524a81f22 Mon Sep 17 00:00:00 2001 From: ShengYi Hung Date: Wed, 7 Oct 2026 12:41:55 +1100 Subject: [PATCH] libthr: Do not rely on UB in counter counter + 1 > 0 is a undefined behavior and nobody guarantee that it will be honestly perform by the compiler that counter + 1 will always be INT_MIN. We switch to INT_MAX check instead to provide a deterministic result. Reviewed by: kib MFC after: 2 weeks Differential Revision: https://reviews.freebsd.org/D60426 --- lib/libthr/thread/thr_mutex.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/libthr/thread/thr_mutex.c b/lib/libthr/thread/thr_mutex.c index 82da8ab1ccba..1787db825bf3 100644 --- a/lib/libthr/thread/thr_mutex.c +++ b/lib/libthr/thread/thr_mutex.c @@ -865,7 +865,7 @@ mutex_self_trylock(struct pthread_mutex *m) case PTHREAD_MUTEX_RECURSIVE: /* Increment the lock count: */ - if (m->m_count + 1 > 0) { + if (m->m_count < INT_MAX) { m->m_count++; ret = 0; } else @@ -934,7 +934,7 @@ mutex_self_lock(struct pthread_mutex *m, const struct timespec *abstime) case PTHREAD_MUTEX_RECURSIVE: /* Increment the lock count: */ - if (m->m_count + 1 > 0) { + if (m->m_count < INT_MAX) { m->m_count++; ret = 0; } else