git: 79e2a46a6afd - main - nvmecontrol: Fix APST transition encoding

From: Warner Losh <imp_at_FreeBSD.org>
Date: Sat, 12 Sep 2026 18:19:08 UTC
The branch main has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=79e2a46a6afda1b762c75cb308a474c9ed0cda4c

commit 79e2a46a6afda1b762c75cb308a474c9ed0cda4c
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2026-09-11 06:14:00 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2026-09-12 16:59:16 +0000

    nvmecontrol: Fix APST transition encoding
    
    Use uin64_t casts to encode data sent to avoid overflows.
    
    Fixes: 35793364d722
    Sponsored by: Netflix
---
 sbin/nvmecontrol/power.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/sbin/nvmecontrol/power.c b/sbin/nvmecontrol/power.c
index fca152c3924a..1ba28deb6c16 100644
--- a/sbin/nvmecontrol/power.c
+++ b/sbin/nvmecontrol/power.c
@@ -174,7 +174,8 @@ power_apst_data_generate(struct nvme_controller_data *cdata,
 
 		/* Wait 50x the latency before each transition. */
 		itpt = MIN(latency * 50, (1 << 24) - 1);
-		data[i - 1] = htole64(itpt << 8 | i << 3);
+		data[i - 1] = htole64((uint64_t)itpt << 8 |
+		    (uint64_t)i << 3);
 	}
 }
 
@@ -191,14 +192,15 @@ power_apst_data_parse(struct nvme_controller_data *cdata,
 		if (sscanf(token, "%i:%i", &itps, &itpt) != 2)
 			errx(EX_USAGE, "cannot parse provided configuration");
 
-		if (itps < 0 || itps >= cdata->npss)
+		if (itps < 0 || itps > cdata->npss)
 			errx(EX_USAGE, "invalid ITPS=%d (must be 0..%d)",
 			    itps, cdata->npss);
 		if (itpt < 0 || itpt >= 1 << 24)
 			errx(EX_USAGE, "invalid ITPT=%d (must be 0..%d)",
-			    itpt, 1 << 24);
+			    itpt, (1 << 24) - 1);
 
-		data[i] = htole64(itpt << 8 | itps << 3);
+		data[i] = htole64((uint64_t)itpt << 8 |
+		    (uint64_t)itps << 3);
 	}
 }