[PATCH] v2: sr.c: handling resource allocation failure

From: Arnaldo Carvalho de Melo (acme@conectiva.com.br)
Date: Wed Aug 23 2000 - 11:57:42 EST


Hi,

    Here is it again, this time without the casts, please consider
applying. Thanks to Tigran for the comments.

        - Arnaldo

--- linux-2.4.0-test7-pre7/drivers/scsi/sr.c Wed Jul 19 01:40:47 2000
+++ linux-2.4.0-test7-pre7.acme/drivers/scsi/sr.c Wed Aug 23 13:52:13 2000
@@ -28,6 +28,9 @@
  * Modified by Jens Axboe <axboe@suse.de> - support DVD-RAM
  * transparently and loose the GHOST hack
  *
+ * Modified by Arnaldo Carvalho de Melo <acme@conectiva.com.br>
+ * check resource allocation in sr_init and some cleanups
+ *
  */
 
 #include <linux/module.h>
@@ -82,11 +85,11 @@
         init_command:sr_init_command
 };
 
-Scsi_CD *scsi_CDs = NULL;
-static int *sr_sizes = NULL;
+Scsi_CD *scsi_CDs;
+static int *sr_sizes;
 
-static int *sr_blocksizes = NULL;
-static int *sr_hardsizes = NULL;
+static int *sr_blocksizes;
+static int *sr_hardsizes;
 
 static int sr_open(struct cdrom_device_info *, int);
 void get_sectorsize(int);
@@ -710,30 +713,45 @@
         }
         if (scsi_CDs)
                 return 0;
- sr_template.dev_max =
- sr_template.dev_noticed + SR_EXTRA_DEVS;
- scsi_CDs = (Scsi_CD *) kmalloc(sr_template.dev_max * sizeof(Scsi_CD), GFP_ATOMIC);
+
+ sr_template.dev_max = sr_template.dev_noticed + SR_EXTRA_DEVS;
+ scsi_CDs = kmalloc(sr_template.dev_max * sizeof(Scsi_CD), GFP_ATOMIC);
+ if (!scsi_CDs)
+ goto cleanup_devfs;
         memset(scsi_CDs, 0, sr_template.dev_max * sizeof(Scsi_CD));
 
- sr_sizes = (int *) kmalloc(sr_template.dev_max * sizeof(int), GFP_ATOMIC);
+ sr_sizes = kmalloc(sr_template.dev_max * sizeof(int), GFP_ATOMIC);
+ if (!sr_sizes)
+ goto cleanup_cds;
         memset(sr_sizes, 0, sr_template.dev_max * sizeof(int));
 
- sr_blocksizes = (int *) kmalloc(sr_template.dev_max *
- sizeof(int), GFP_ATOMIC);
-
- sr_hardsizes = (int *) kmalloc(sr_template.dev_max *
- sizeof(int), GFP_ATOMIC);
+ sr_blocksizes = kmalloc(sr_template.dev_max * sizeof(int), GFP_ATOMIC);
+ if (!sr_blocksizes)
+ goto cleanup_sizes;
+
+ sr_hardsizes = kmalloc(sr_template.dev_max * sizeof(int), GFP_ATOMIC);
+ if (!sr_hardsizes)
+ goto cleanup_blocksizes;
         /*
          * These are good guesses for the time being.
          */
- for (i = 0; i < sr_template.dev_max; i++)
- {
+ for (i = 0; i < sr_template.dev_max; i++) {
                 sr_blocksizes[i] = 2048;
                 sr_hardsizes[i] = 2048;
         }
         blksize_size[MAJOR_NR] = sr_blocksizes;
         hardsect_size[MAJOR_NR] = sr_hardsizes;
         return 0;
+cleanup_blocksizes:
+ kfree(sr_blocksizes);
+cleanup_sizes:
+ kfree(sr_sizes);
+cleanup_cds:
+ kfree(scsi_CDs);
+cleanup_devfs:
+ devfs_unregister_blkdev(MAJOR_NR, "sr");
+ sr_registered--;
+ return 1;
 }
 
 void sr_finish()
@@ -845,14 +863,14 @@
         devfs_unregister_blkdev(MAJOR_NR, "sr");
         sr_registered--;
         if (scsi_CDs != NULL) {
- kfree((char *) scsi_CDs);
+ kfree(scsi_CDs);
 
- kfree((char *) sr_sizes);
+ kfree(sr_sizes);
                 sr_sizes = NULL;
 
- kfree((char *) sr_blocksizes);
+ kfree(sr_blocksizes);
                 sr_blocksizes = NULL;
- kfree((char *) sr_hardsizes);
+ kfree(sr_hardsizes);
                 sr_hardsizes = NULL;
         }
         blksize_size[MAJOR_NR] = NULL;
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
Please read the FAQ at http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Wed Aug 23 2000 - 21:00:09 EST