Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 28 May 2017 00:47:03 +0000 (UTC)
From:      Ngie Cooper <ngie@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-10@freebsd.org
Subject:   svn commit: r319023 - stable/10/lib/libcam
Message-ID:  <201705280047.v4S0l3Hd099273@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: ngie
Date: Sun May 28 00:47:02 2017
New Revision: 319023
URL: https://svnweb.freebsd.org/changeset/base/319023

Log:
  MFC r316131:
  
  Fix up r316081 by using nitems(cam_errbuf) instead of sizeof(cam_errbuf)
  
  Part of my original reasoning as far as converting the snprintf
  calls was to permit switching over from char[] to wchar_t[] in the
  future, as well as futureproof in case cam_errbuf's size was ever
  changed.
  
  Unfortunately, my approach was bugged because it conflated the
  number of items with the size of the buffer, instead of the number of
  elements being a fixed size != 1 byte.
  
  Use nitems(..) instead which counts the quantity of items of a specific
  type, as opposed to an unqualified sizeof(..) (which assumes that the
  number of characters is equal to the buffer size).
  
  Noted by:	cem

Modified:
  stable/10/lib/libcam/camlib.c
Directory Properties:
  stable/10/   (props changed)

Modified: stable/10/lib/libcam/camlib.c
==============================================================================
--- stable/10/lib/libcam/camlib.c	Sun May 28 00:45:28 2017	(r319022)
+++ stable/10/lib/libcam/camlib.c	Sun May 28 00:47:02 2017	(r319023)
@@ -120,7 +120,7 @@ cam_get_device(const char *path, char *d
 	int i;
 
 	if (path == NULL) {
-		snprintf(cam_errbuf, sizeof(cam_errbuf),
+		snprintf(cam_errbuf, nitems(cam_errbuf),
 		    "%s: device pathname was NULL", __func__);
 		return(-1);
 	}
@@ -143,7 +143,7 @@ cam_get_device(const char *path, char *d
 	}
 
 	if (*tmpstr == '\0') {
-		snprintf(cam_errbuf, sizeof(cam_errbuf),
+		snprintf(cam_errbuf, nitems(cam_errbuf),
 		    "%s: no text after slash", __func__);
 		free(newpath);
 		return(-1);
@@ -171,7 +171,7 @@ cam_get_device(const char *path, char *d
 	 * If we only have 1, we don't have a valid device name.
 	 */
 	if (strlen(tmpstr) < 2) {
-		snprintf(cam_errbuf, sizeof(cam_errbuf),
+		snprintf(cam_errbuf, nitems(cam_errbuf),
 		    "%s: must have both device name and unit number",
 		    __func__);
 		free(newpath);
@@ -183,7 +183,7 @@ cam_get_device(const char *path, char *d
 	 * has probably given us all numbers.  Point out the error.
 	 */
 	if (isdigit(*tmpstr)) {
-		snprintf(cam_errbuf, sizeof(cam_errbuf),
+		snprintf(cam_errbuf, nitems(cam_errbuf),
 		    "%s: device name cannot begin with a number",
 		    __func__);
 		free(newpath);
@@ -196,7 +196,7 @@ cam_get_device(const char *path, char *d
 	 * or he gave us a device name/number format we don't recognize.
 	 */
 	if (!isdigit(tmpstr[strlen(tmpstr) - 1])) {
-		snprintf(cam_errbuf, sizeof(cam_errbuf),
+		snprintf(cam_errbuf, nitems(cam_errbuf),
 		    "%s: unable to find device unit number", __func__);
 		free(newpath);
 		return(-1);
@@ -272,7 +272,7 @@ cam_open_btl(path_id_t path_id, target_i
 	int fd, bufsize;
 
 	if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
-		snprintf(cam_errbuf, sizeof(cam_errbuf),
+		snprintf(cam_errbuf, nitems(cam_errbuf),
 		    "%s: couldn't open %s\n%s: %s", __func__, XPT_DEVICE,
 		    __func__, strerror(errno));
 		return(NULL);
@@ -289,7 +289,7 @@ cam_open_btl(path_id_t path_id, target_i
 	ccb.cdm.match_buf_len = bufsize;
 	ccb.cdm.matches = (struct dev_match_result *)malloc(bufsize);
 	if (ccb.cdm.matches == NULL) {
-		snprintf(cam_errbuf, sizeof(cam_errbuf),
+		snprintf(cam_errbuf, nitems(cam_errbuf),
 		    "%s: couldn't malloc match buffer", __func__);
 		close(fd);
 		return(NULL);
@@ -302,7 +302,7 @@ cam_open_btl(path_id_t path_id, target_i
 	ccb.cdm.patterns = (struct dev_match_pattern *)malloc(
 		sizeof(struct dev_match_pattern));
 	if (ccb.cdm.patterns == NULL) {
-		snprintf(cam_errbuf, sizeof(cam_errbuf),
+		snprintf(cam_errbuf, nitems(cam_errbuf),
 		    "%s: couldn't malloc pattern buffer", __func__);
 		free(ccb.cdm.matches);
 		ccb.cdm.matches = NULL;
@@ -325,7 +325,7 @@ cam_open_btl(path_id_t path_id, target_i
 			   PERIPH_MATCH_LUN | PERIPH_MATCH_NAME;
 
 	if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
-		snprintf(cam_errbuf, sizeof(cam_errbuf),
+		snprintf(cam_errbuf, nitems(cam_errbuf),
 		    "%s: CAMIOCOMMAND ioctl failed\n"
 		    "%s: %s", __func__, __func__, strerror(errno));
 		goto btl_bailout;
@@ -337,7 +337,7 @@ cam_open_btl(path_id_t path_id, target_i
 	if ((ccb.ccb_h.status != CAM_REQ_CMP)
 	 || ((ccb.cdm.status != CAM_DEV_MATCH_LAST)
 	   && (ccb.cdm.status != CAM_DEV_MATCH_MORE))) {
-		snprintf(cam_errbuf, sizeof(cam_errbuf),
+		snprintf(cam_errbuf, nitems(cam_errbuf),
 		    "%s: CAM error %#x, CDM error %d "
 		    "returned from XPT_DEV_MATCH ccb", __func__,
 		    ccb.ccb_h.status, ccb.cdm.status);
@@ -345,7 +345,7 @@ cam_open_btl(path_id_t path_id, target_i
 	}
 
 	if (ccb.cdm.status == CAM_DEV_MATCH_MORE) {
-		snprintf(cam_errbuf, sizeof(cam_errbuf),
+		snprintf(cam_errbuf, nitems(cam_errbuf),
 		    "%s: CDM reported more than one"
 		    " passthrough device at %d:%d:%jx!!\n",
 		    __func__, path_id, target_id, (uintmax_t)target_lun);
@@ -353,7 +353,7 @@ cam_open_btl(path_id_t path_id, target_i
 	}
 
 	if (ccb.cdm.num_matches == 0) {
-		snprintf(cam_errbuf, sizeof(cam_errbuf),
+		snprintf(cam_errbuf, nitems(cam_errbuf),
 		    "%s: no passthrough device found at"
 		    " %d:%d:%jx", __func__, path_id, target_id,
 		    (uintmax_t)target_lun);
@@ -379,7 +379,7 @@ cam_open_btl(path_id_t path_id, target_i
 		break; /* NOTREACHED */
 	}
 	default:
-		snprintf(cam_errbuf, sizeof(cam_errbuf),
+		snprintf(cam_errbuf, nitems(cam_errbuf),
 		    "%s: asked for a peripheral match, but"
 		    " got a bus or device match", __func__);
 		goto btl_bailout;
@@ -422,7 +422,7 @@ cam_lookup_pass(const char *dev_name, in
 	 * passthrough device.
 	 */
 	if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
-		snprintf(cam_errbuf, sizeof(cam_errbuf),
+		snprintf(cam_errbuf, nitems(cam_errbuf),
 		    "%s: couldn't open %s\n%s: %s", __func__, XPT_DEVICE,
 		    __func__, strerror(errno));
 		return(NULL);
@@ -455,7 +455,7 @@ cam_lookup_pass(const char *dev_name, in
 				 "your kernel\n%s: or %s%d doesn't exist",
 				 __func__, __func__, dev_name, unit);
 		}
-		snprintf(cam_errbuf, sizeof(cam_errbuf),
+		snprintf(cam_errbuf, nitems(cam_errbuf),
 		    "%s: CAMGETPASSTHRU ioctl failed\n"
 		    "%s: %s%s", __func__, __func__, strerror(errno),
 		    (errno == ENOENT) ? tmpstr : "");
@@ -473,7 +473,7 @@ cam_lookup_pass(const char *dev_name, in
 	 * the device the user gave us.
 	 */
 	if (ccb.cgdl.status == CAM_GDEVLIST_ERROR) {
-		snprintf(cam_errbuf, sizeof(cam_errbuf),
+		snprintf(cam_errbuf, nitems(cam_errbuf),
 		    "%s: device %s%d does not exist!",
 		    __func__, dev_name, unit);
 		return(NULL);
@@ -504,7 +504,7 @@ cam_real_open_device(const char *path, i
 	if (device == NULL) {
 		if ((device = (struct cam_device *)malloc(
 		     sizeof(struct cam_device))) == NULL) {
-			snprintf(cam_errbuf, sizeof(cam_errbuf),
+			snprintf(cam_errbuf, nitems(cam_errbuf),
 			    "%s: device structure malloc"
 			    " failed\n%s: %s", __func__, __func__,
 			    strerror(errno));
@@ -535,7 +535,7 @@ cam_real_open_device(const char *path, i
 	device->given_unit_number = given_unit_number;
 
 	if ((fd = open(path, flags)) < 0) {
-		snprintf(cam_errbuf, sizeof(cam_errbuf),
+		snprintf(cam_errbuf, nitems(cam_errbuf),
 		    "%s: couldn't open passthrough device %s\n"
 		    "%s: %s", __func__, path, __func__,
 		    strerror(errno));
@@ -563,7 +563,7 @@ cam_real_open_device(const char *path, i
 		 * because we just opened it above.  The only way this
 		 * ioctl can fail is if the ccb size is wrong.
 		 */
-		snprintf(cam_errbuf, sizeof(cam_errbuf),
+		snprintf(cam_errbuf, nitems(cam_errbuf),
 		    "%s: CAMGETPASSTHRU ioctl failed\n"
 		    "%s: %s", __func__, __func__, strerror(errno));
 		goto crod_bailout;
@@ -576,7 +576,7 @@ cam_real_open_device(const char *path, i
 	 * the device the user gave us.
 	 */
 	if (ccb.cgdl.status == CAM_GDEVLIST_ERROR) {
-		snprintf(cam_errbuf, sizeof(cam_errbuf),
+		snprintf(cam_errbuf, nitems(cam_errbuf),
 		    "%s: passthrough device does not exist!", __func__);
 		goto crod_bailout;
 	}
@@ -590,7 +590,7 @@ cam_real_open_device(const char *path, i
 
 	ccb.ccb_h.func_code = XPT_PATH_INQ;
 	if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
-		snprintf(cam_errbuf, sizeof(cam_errbuf),
+		snprintf(cam_errbuf, nitems(cam_errbuf),
 		    "%s: Path Inquiry CCB failed\n"
 		    "%s: %s", __func__, __func__, strerror(errno));
 		goto crod_bailout;
@@ -605,7 +605,7 @@ cam_real_open_device(const char *path, i
 	 */
 	ccb.ccb_h.func_code = XPT_GDEV_TYPE;
 	if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
-		snprintf(cam_errbuf, sizeof(cam_errbuf),
+		snprintf(cam_errbuf, nitems(cam_errbuf),
 		    "%s: Get Device Type CCB failed\n"
 		    "%s: %s", __func__, __func__, strerror(errno));
 		goto crod_bailout;
@@ -629,7 +629,7 @@ cam_real_open_device(const char *path, i
 	ccb.cts.type = CTS_TYPE_CURRENT_SETTINGS;
 
 	if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
-		snprintf(cam_errbuf, sizeof(cam_errbuf),
+		snprintf(cam_errbuf, nitems(cam_errbuf),
 		    "%s: Get Transfer Settings CCB failed\n"
 		    "%s: %s", __func__, __func__, strerror(errno));
 		goto crod_bailout;
@@ -711,14 +711,14 @@ cam_device_dup(struct cam_device *device
 	struct cam_device *newdev;
 
 	if (device == NULL) {
-		snprintf(cam_errbuf, sizeof(cam_errbuf),
+		snprintf(cam_errbuf, nitems(cam_errbuf),
 		    "%s: device is NULL", __func__);
 		return (NULL);
 	}
 
 	newdev = malloc(sizeof(struct cam_device));
 	if (newdev == NULL) {
-		snprintf(cam_errbuf, sizeof(cam_errbuf),
+		snprintf(cam_errbuf, nitems(cam_errbuf),
 		    "%s: couldn't malloc CAM device structure", __func__);
 		return (NULL);
 	}
@@ -736,13 +736,13 @@ cam_device_copy(struct cam_device *src, 
 {
 
 	if (src == NULL) {
-		snprintf(cam_errbuf, sizeof(cam_errbuf),
+		snprintf(cam_errbuf, nitems(cam_errbuf),
 		    "%s: source device struct was NULL", __func__);
 		return;
 	}
 
 	if (dst == NULL) {
-		snprintf(cam_errbuf, sizeof(cam_errbuf),
+		snprintf(cam_errbuf, nitems(cam_errbuf),
 		    "%s: destination device struct was NULL", __func__);
 		return;
 	}



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201705280047.v4S0l3Hd099273>