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).

MFC after:	2 months
Noted by:	cem
Sponsored by:	Dell EMC Isilon
This commit is contained in:
Enji Cooper 2017-03-29 08:38:31 +00:00
parent e39babf8f7
commit 33193da27e
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=316131

View File

@ -120,7 +120,7 @@ cam_get_device(const char *path, char *dev_name, int devnamelen, int *unit)
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 *dev_name, int devnamelen, int *unit)
}
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 *dev_name, int devnamelen, int *unit)
* 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 *dev_name, int devnamelen, int *unit)
* 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 *dev_name, int devnamelen, int *unit)
* 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_id_t target_id, lun_id_t target_lun,
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_id_t target_id, lun_id_t target_lun,
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_id_t target_id, lun_id_t target_lun,
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_id_t target_id, lun_id_t target_lun,
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_id_t target_id, lun_id_t target_lun,
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_id_t target_id, lun_id_t target_lun,
}
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_id_t target_id, lun_id_t target_lun,
}
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_id_t target_id, lun_id_t target_lun,
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, int unit, int flags,
* 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, int unit, int flags,
"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, int unit, int flags,
* 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, int flags, struct cam_device *device,
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, int flags, struct cam_device *device,
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, int flags, struct cam_device *device,
* 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, int flags, struct cam_device *device,
* 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, int flags, struct cam_device *device,
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, int flags, struct cam_device *device,
*/
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, int flags, struct cam_device *device,
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, struct cam_device *dst)
{
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;
}