Index | Thread | Search

From:
Crystal Kolipe <kolipe.c@exoticsilicon.com>
Subject:
Re: /sbin/scsi bugfixes
To:
Ted Unangst <tedu@tedunangst.com>
Cc:
tech@openbsd.org
Date:
Thu, 15 May 2025 17:12:41 -0300

Download raw body.

Thread
On Thu, May 15, 2025 at 03:19:15PM -0400, Ted Unangst wrote:
> On 2025-05-15, Crystal Kolipe wrote:
> 
> I think this code would benefit from the introduction of some variables.

Agreed.  Updated version reads much better:

--- scsi.c.dist	Sun Dec  4 23:50:47 2022
+++ scsi.c	Thu May 15 21:10:44 2025
@@ -695,6 +695,8 @@
 mode_edit(int fd, int page, int edit, int argc, char *argv[])
 {
 	int i;
+	int flag_all;
+	int offset;
 	u_char data[255];
 	u_char *mode_pars;
 	struct mode_header
@@ -804,10 +806,26 @@
 	 */
 	mh = (struct mode_header *)data;
 	mph = (struct mode_page_header *)(((char *)mh) + sizeof(*mh) + mh->bdl);
-	mode_pars = (char *)mph + sizeof(*mph);
 
+	/*
+	 * If the data length was just block descriptors and the three header
+	 * bytes, we're done.
+	 */
+
+	if (mh->bdl + 3 >= mh->mdl)
+		return ;
+
+	/*
+	 * Skip mode page header unless we are requesting page 0x3f, (all pages)
+	 */
+
+	flag_all = ((page & 0x3f) == 0x3f);
+	offset = (flag_all ? 3 : 5);
+
+	mode_pars = (char *)mph + ( flag_all ? 0 : sizeof(*mph) );
+
 	if (!fmt) {
-		for (i = 0; i < mh->mdl; i++) {
+		for (i = 0; i < (mh->mdl - mh->bdl - offset); i++) {
 			printf("%02x%c",mode_pars[i],
 			(((i + 1) % 8) == 0) ? '\n' : ' ');
 		}