Guard against divide-by-zero.
authorPeter Avalos <pavalos@dragonflybsd.org>
Sun, 2 Dec 2007 03:16:52 +0000 (03:16 +0000)
committerPeter Avalos <pavalos@dragonflybsd.org>
Sun, 2 Dec 2007 03:16:52 +0000 (03:16 +0000)
Obtained-from: FreeBSD

sys/bus/cam/cam.c

index e31d399..920070c 100644 (file)
@@ -26,7 +26,7 @@
  * SUCH DAMAGE.
  *
  * $FreeBSD: src/sys/cam/cam.c,v 1.4 2001/03/27 05:45:09 ken Exp $
- * $DragonFly: src/sys/bus/cam/cam.c,v 1.7 2007/11/18 17:53:01 pavalos Exp $
+ * $DragonFly: src/sys/bus/cam/cam.c,v 1.8 2007/12/02 03:16:52 pavalos Exp $
  */
 #include <sys/param.h>
 #ifdef _KERNEL
@@ -370,7 +370,16 @@ cam_calc_geometry(struct ccb_calc_geometry *ccg, int extended)
 {
        uint32_t size_mb, secs_per_cylinder;
 
-       size_mb = ccg->volume_size / ((1024L * 1024L) / ccg->block_size);
+       if (ccg->block_size == 0) {
+               ccg->ccb_h.status = CAM_REQ_CMP_ERR;
+               return;
+       }
+       size_mb = (1024L * 1024L) / ccg->block_size;
+       if (size_mb == 0) {
+               ccg->ccb_h.status = CAM_REQ_CMP_ERR;
+               return;
+       }
+       size_mb = ccg->volume_size / size_mb;
        if (size_mb > 1024 && extended) {
                ccg->heads = 255;
                ccg->secs_per_track = 63;
@@ -379,6 +388,10 @@ cam_calc_geometry(struct ccb_calc_geometry *ccg, int extended)
                ccg->secs_per_track = 32;
        }
        secs_per_cylinder = ccg->heads * ccg->secs_per_track;
+       if (secs_per_cylinder == 0) {
+               ccg->ccb_h.status = CAM_REQ_CMP_ERR;
+               return;
+       }
        ccg->cylinders = ccg->volume_size / secs_per_cylinder;
        ccg->ccb_h.status = CAM_REQ_CMP;
 }