grub-probe: restructure code to make static analysis easier

Current code in probe() could not be verified to not contain memory leaks.
Restructure code and ensure grub_device_close is always called at the end of
loop.

Calms down Coverity scan.
CID: 73739
This commit is contained in:
Andrei Borzenkov 2015-06-19 20:47:43 +03:00
parent fbdd37e7f9
commit 03f1f24e37

View file

@ -485,23 +485,18 @@ probe (const char *path, char **device_names, char delim)
printf (" "); printf (" ");
else else
printf ("\n"); printf ("\n");
grub_device_close (dev);
continue;
} }
if ((print == PRINT_COMPATIBILITY_HINT || print == PRINT_BIOS_HINT else if ((print == PRINT_COMPATIBILITY_HINT || print == PRINT_BIOS_HINT
|| print == PRINT_IEEE1275_HINT || print == PRINT_BAREMETAL_HINT || print == PRINT_IEEE1275_HINT || print == PRINT_BAREMETAL_HINT
|| print == PRINT_EFI_HINT || print == PRINT_ARC_HINT) || print == PRINT_EFI_HINT || print == PRINT_ARC_HINT)
&& dev->disk->dev->id != GRUB_DISK_DEVICE_HOSTDISK_ID) && dev->disk->dev->id != GRUB_DISK_DEVICE_HOSTDISK_ID)
{ {
grub_util_fprint_full_disk_name (stdout, dev->disk->name, dev); grub_util_fprint_full_disk_name (stdout, dev->disk->name, dev);
putchar (delim); putchar (delim);
grub_device_close (dev);
continue;
} }
if (print == PRINT_COMPATIBILITY_HINT) else if (print == PRINT_COMPATIBILITY_HINT)
{ {
const char *map; const char *map;
char *biosname; char *biosname;
@ -519,16 +514,14 @@ probe (const char *path, char **device_names, char delim)
{ {
grub_util_fprint_full_disk_name (stdout, biosname, dev); grub_util_fprint_full_disk_name (stdout, biosname, dev);
putchar (delim); putchar (delim);
free (biosname);
/* Compatibility hint is one device only. */
grub_device_close (dev);
break;
} }
free (biosname);
grub_device_close (dev);
/* Compatibility hint is one device only. */
if (biosname)
break;
continue;
} }
if (print == PRINT_BIOS_HINT) else if (print == PRINT_BIOS_HINT)
{ {
char *biosname; char *biosname;
biosname = grub_util_guess_bios_drive (*curdev); biosname = grub_util_guess_bios_drive (*curdev);
@ -536,12 +529,10 @@ probe (const char *path, char **device_names, char delim)
{ {
grub_util_fprint_full_disk_name (stdout, biosname, dev); grub_util_fprint_full_disk_name (stdout, biosname, dev);
putchar (delim); putchar (delim);
free (biosname);
} }
free (biosname);
grub_device_close (dev);
continue;
} }
if (print == PRINT_IEEE1275_HINT) else if (print == PRINT_IEEE1275_HINT)
{ {
const char *osdev = grub_util_biosdisk_get_osdev (dev->disk); const char *osdev = grub_util_biosdisk_get_osdev (dev->disk);
char *ofpath = grub_util_devname_to_ofpath (osdev); char *ofpath = grub_util_devname_to_ofpath (osdev);
@ -565,11 +556,8 @@ probe (const char *path, char **device_names, char delim)
free (ofpath); free (ofpath);
putchar (delim); putchar (delim);
} }
grub_device_close (dev);
continue;
} }
if (print == PRINT_EFI_HINT) else if (print == PRINT_EFI_HINT)
{ {
char *biosname; char *biosname;
const char *map; const char *map;
@ -585,14 +573,11 @@ probe (const char *path, char **device_names, char delim)
{ {
grub_util_fprint_full_disk_name (stdout, biosname, dev); grub_util_fprint_full_disk_name (stdout, biosname, dev);
putchar (delim); putchar (delim);
free (biosname);
} }
free (biosname);
grub_device_close (dev);
continue;
} }
if (print == PRINT_BAREMETAL_HINT) else if (print == PRINT_BAREMETAL_HINT)
{ {
char *biosname; char *biosname;
const char *map; const char *map;
@ -609,14 +594,11 @@ probe (const char *path, char **device_names, char delim)
{ {
grub_util_fprint_full_disk_name (stdout, biosname, dev); grub_util_fprint_full_disk_name (stdout, biosname, dev);
putchar (delim); putchar (delim);
free (biosname);
} }
free (biosname);
grub_device_close (dev);
continue;
} }
if (print == PRINT_ARC_HINT) else if (print == PRINT_ARC_HINT)
{ {
const char *map; const char *map;
@ -626,46 +608,28 @@ probe (const char *path, char **device_names, char delim)
grub_util_fprint_full_disk_name (stdout, map, dev); grub_util_fprint_full_disk_name (stdout, map, dev);
putchar (delim); putchar (delim);
} }
/* FIXME */
grub_device_close (dev);
continue;
} }
if (print == PRINT_ABSTRACTION) else if (print == PRINT_ABSTRACTION)
{ probe_abstraction (dev->disk, delim);
probe_abstraction (dev->disk, delim);
grub_device_close (dev);
continue;
}
if (print == PRINT_CRYPTODISK_UUID) else if (print == PRINT_CRYPTODISK_UUID)
{ probe_cryptodisk_uuid (dev->disk, delim);
probe_cryptodisk_uuid (dev->disk, delim);
grub_device_close (dev);
continue;
}
if (print == PRINT_PARTMAP) else if (print == PRINT_PARTMAP)
{ /* Check if dev->disk itself is contained in a partmap. */
/* Check if dev->disk itself is contained in a partmap. */ probe_partmap (dev->disk, delim);
probe_partmap (dev->disk, delim);
grub_device_close (dev);
continue;
}
if (print == PRINT_MSDOS_PARTTYPE) else if (print == PRINT_MSDOS_PARTTYPE)
{ {
if (dev->disk->partition if (dev->disk->partition
&& strcmp(dev->disk->partition->partmap->name, "msdos") == 0) && strcmp(dev->disk->partition->partmap->name, "msdos") == 0)
printf ("%02x", dev->disk->partition->msdostype); printf ("%02x", dev->disk->partition->msdostype);
putchar (delim); putchar (delim);
grub_device_close (dev);
continue;
} }
if (print == PRINT_GPT_PARTTYPE) else if (print == PRINT_GPT_PARTTYPE)
{ {
if (dev->disk->partition if (dev->disk->partition
&& strcmp (dev->disk->partition->partmap->name, "gpt") == 0) && strcmp (dev->disk->partition->partmap->name, "gpt") == 0)
@ -694,9 +658,9 @@ probe (const char *path, char **device_names, char delim)
dev->disk->partition = p; dev->disk->partition = p;
} }
putchar (delim); putchar (delim);
grub_device_close (dev);
continue;
} }
grub_device_close (dev);
} }
end: end: