Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 20 Jan 2017 21:40:04 +0000 (UTC)
From:      Alan Somers <asomers@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r312559 - head/usr.sbin/camdd
Message-ID:  <201701202140.v0KLe4aK053252@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: asomers
Date: Fri Jan 20 21:40:04 2017
New Revision: 312559
URL: https://svnweb.freebsd.org/changeset/base/312559

Log:
  Fix misc Coverity defects in camdd(8)
  
  CID 1341620	Fix a small memory leak
  CID 1341630	Though this is technically a false positive, rearrange the
  		code for clarity.
  CID 1341635	Eliminate dead code
  CID 1368663	Fix a double mutex unlock in the error path
  
  Also:
  * Use sig_atomic_t for variables accessed from signal handlers
  * Don't conditionalize free(3) on its argument being non-null
  
  Reported by:	Coverity
  CID:		1341620 1341630 1341635 1368663
  Reviewed by:	ken
  MFC after:	4 weeks
  Sponsored by:	Spectra Logic Corp
  Differential Revision:	https://reviews.freebsd.org/D9237

Modified:
  head/usr.sbin/camdd/camdd.c

Modified: head/usr.sbin/camdd/camdd.c
==============================================================================
--- head/usr.sbin/camdd/camdd.c	Fri Jan 20 21:21:41 2017	(r312558)
+++ head/usr.sbin/camdd/camdd.c	Fri Jan 20 21:40:04 2017	(r312559)
@@ -420,9 +420,9 @@ struct camdd_dev {
 };
 
 static sem_t camdd_sem;
-static int need_exit = 0;
-static int error_exit = 0;
-static int need_status = 0;
+static sig_atomic_t need_exit = 0;
+static sig_atomic_t error_exit = 0;
+static sig_atomic_t need_status = 0;
 
 #ifndef min
 #define	min(a, b) (a < b) ? a : b
@@ -712,11 +712,7 @@ camdd_alloc_buf(struct camdd_dev *dev, c
 	return (buf);
 
 bailout_error:
-	if (data_ptr != NULL)
-		free(data_ptr);
-
-	if (buf != NULL)
-		free(buf);
+	free(data_ptr);
 
 	return (NULL);
 }
@@ -2262,6 +2258,7 @@ camdd_file_run(struct camdd_dev *dev)
 		if (file_dev->tmp_buf == NULL) {
 			buf->status = CAMDD_STATUS_ERROR;
 			error_count++;
+			pthread_mutex_lock(&dev->mutex);
 			goto bailout;
 		}
 		for (i = 0, cur_offset = 0; i < data->sg_count; i++) {
@@ -2984,7 +2981,6 @@ int
 camdd_rw(struct camdd_io_opts *io_opts, int num_io_opts, uint64_t max_io,
 	 int retry_count, int timeout)
 {
-	char *device = NULL;
 	struct cam_device *new_cam_dev = NULL;
 	struct camdd_dev *devs[2];
 	struct timespec start_time;
@@ -3004,12 +3000,11 @@ camdd_rw(struct camdd_io_opts *io_opts, 
 	for (i = 0; i < num_io_opts; i++) {
 		switch (io_opts[i].dev_type) {
 		case CAMDD_DEV_PASS: {
-			camdd_argmask new_arglist = CAMDD_ARG_NONE;
-			int bus = 0, target = 0, lun = 0;
-			char name[30];
-			int rv;
-
 			if (isdigit(io_opts[i].dev_name[0])) {
+				camdd_argmask new_arglist = CAMDD_ARG_NONE;
+				int bus = 0, target = 0, lun = 0;
+				int rv;
+
 				/* device specified as bus:target[:lun] */
 				rv = parse_btl(io_opts[i].dev_name, &bus,
 				    &target, &lun, &new_arglist);
@@ -3025,23 +3020,21 @@ camdd_rw(struct camdd_io_opts *io_opts, 
 					lun = 0;
 					new_arglist |= CAMDD_ARG_LUN;
 				}
+				new_cam_dev = cam_open_btl(bus, target, lun,
+				    O_RDWR, NULL);
 			} else {
+				char name[30];
+
 				if (cam_get_device(io_opts[i].dev_name, name,
 						   sizeof name, &unit) == -1) {
 					warnx("%s", cam_errbuf);
 					error = 1;
 					goto bailout;
 				}
-				device = strdup(name);
-				new_arglist |= CAMDD_ARG_DEVICE |CAMDD_ARG_UNIT;
+				new_cam_dev = cam_open_spec_device(name, unit,
+				    O_RDWR, NULL);
 			}
 
-			if (new_arglist & (CAMDD_ARG_BUS | CAMDD_ARG_TARGET))
-				new_cam_dev = cam_open_btl(bus, target, lun,
-				    O_RDWR, NULL);
-			else
-				new_cam_dev = cam_open_spec_device(device, unit,
-				    O_RDWR, NULL);
 			if (new_cam_dev == NULL) {
 				warnx("%s", cam_errbuf);
 				error = 1;



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