git: 1d21e67e720a - main - libthr: Do not rely on UB in counter

From: ShengYi Hung <aokblast_at_FreeBSD.org>
Date: Wed, 07 Oct 2026 16:59:29 UTC
The branch main has been updated by aokblast:

URL: https://cgit.FreeBSD.org/src/commit/?id=1d21e67e720a08992e81d626cff9d76524a81f22

commit 1d21e67e720a08992e81d626cff9d76524a81f22
Author:     ShengYi Hung <aokblast@FreeBSD.org>
AuthorDate: 2026-10-07 01:41:55 +0000
Commit:     ShengYi Hung <aokblast@FreeBSD.org>
CommitDate: 2026-10-07 16:59:10 +0000

    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