summaryrefslogtreecommitdiffstats
path: root/lib/libdisk
diff options
context:
space:
mode:
authorsam <sam@FreeBSD.org>2002-10-17 18:34:32 +0000
committersam <sam@FreeBSD.org>2002-10-17 18:34:32 +0000
commit68349b6d1c0a257df294e63c688beae853afd989 (patch)
treea7f1620b1aa19fb0b67875b62538ab9857c7fef2 /lib/libdisk
parent8680a53acea5bef887b679581a66f2290c3cd7cf (diff)
downloadFreeBSD-src-68349b6d1c0a257df294e63c688beae853afd989.zip
FreeBSD-src-68349b6d1c0a257df294e63c688beae853afd989.tar.gz
o ioctl DIOCGDINFO error wasn't checked
o memory wasn't reclaimed in certain cases o add more msgs under #ifdef DEBUG o rewrite tangle of for loops for clarity NB: Open_Disk should redo how it malloc's memory so the caller can free everything. Documentation says the caller can free the disk list to reclaim everything but this leaks the indirect strings. Fixing this is simple for the sysctl case but adds complexity to the fallback, non-sysctl, case.
Diffstat (limited to 'lib/libdisk')
-rw-r--r--lib/libdisk/disk.c36
1 files changed, 29 insertions, 7 deletions
diff --git a/lib/libdisk/disk.c b/lib/libdisk/disk.c
index 72bbe8a..d10ade3 100644
--- a/lib/libdisk/disk.c
+++ b/lib/libdisk/disk.c
@@ -92,7 +92,13 @@ Int_Open_Disk(const char *name, u_long size)
}
memset(&dl, 0, sizeof dl);
- ioctl(fd, DIOCGDINFO, &dl);
+ if (ioctl(fd, DIOCGDINFO, &dl) < 0) {
+#ifdef DEBUG
+ warn("DIOCGDINFO(%s) failed", device);
+#endif
+ close(fd);
+ return 0;
+ }
i = ioctl(fd, DIOCGSLICEINFO, &ds);
if (i < 0) {
#ifdef DEBUG
@@ -129,8 +135,13 @@ Int_Open_Disk(const char *name, u_long size)
}
}
free (buf);
- if (sector_size > MAX_SEC_SIZE)
+ if (sector_size > MAX_SEC_SIZE) {
+#ifdef DEBUG
+ warn("Int_Open_Disk: could not determine sector size, "
+ "calculated %u, max %u\n", sector_size, MAX_SEC_SIZE);
+#endif
return NULL; /* could not determine sector size */
+ }
#ifdef PC98
p = (unsigned char*)read_block(fd, 1, sector_size);
@@ -508,20 +519,31 @@ Disk_Names()
static char **disks;
int error;
size_t listsize;
- char *disklist, **dp;
+ char *disklist;
disks = malloc(sizeof *disks * (1 + MAX_NO_DISKS));
+ if (disks == NULL)
+ return NULL;
memset(disks,0,sizeof *disks * (1 + MAX_NO_DISKS));
error = sysctlbyname("kern.disks", NULL, &listsize, NULL, 0);
if (!error) {
disklist = (char *)malloc(listsize);
+ if (disklist == NULL) {
+ free(disks);
+ return NULL;
+ }
memset(disklist, 0, listsize);
error = sysctlbyname("kern.disks", disklist, &listsize, NULL, 0);
- if (error)
+ if (error) {
+ free(disklist);
+ free(disks);
return NULL;
- disk_cnt = 0;
- for (dp = disks; ((*dp = strsep(&disklist, " ")) != NULL) &&
- disk_cnt < MAX_NO_DISKS; disk_cnt++, dp++);
+ }
+ for (disk_cnt = 0; disk_cnt < MAX_NO_DISKS; disk_cnt++) {
+ disks[disk_cnt] = strsep(&disklist, " ");
+ if (disks[disk_cnt] == NULL)
+ break;
+ }
} else {
warn("kern.disks sysctl not available");
disk_cnt = 0;
OpenPOWER on IntegriCloud