Introduce DEVICE_INIT, DEVICE_GET, and DEVICE_SHUTDOWN macros. As the

names suggest, they perform methods on Device's.  In addition, they
check that the pointer passed to them is valid; if it isn't, they
pretend that the action failed.  This fixes some crashes due to NULL
dereferences (e.g., PR 26509).

Approved by:	jkh (some time ago)
This commit is contained in:
Dima Dorfman
2001-07-02 00:18:04 +00:00
parent 0c2e00bb8f
commit 13704ca508
16 changed files with 64 additions and 48 deletions
+1 -1
View File
@@ -236,7 +236,7 @@ deviceReset(void)
int i;
for (i = 0; i < numDevs; i++) {
Devices[i]->shutdown(Devices[i]);
DEVICE_SHUTDOWN(Devices[i]);
/* XXX this potentially leaks Devices[i]->private if it's being
* used to point to something dynamic, but you're not supposed
+2 -2
View File
@@ -406,14 +406,14 @@ dispatch_load_floppy(dialogMenuItem *self)
return what;
}
if (!mediaDevice->init(mediaDevice)) {
if (!DEVICE_INIT(mediaDevice)) {
msgConfirm("Unable to mount floppy filesystem.");
what |= DITEM_FAILURE;
mediaClose();
return what;
}
fp = mediaDevice->get(mediaDevice, cp, TRUE);
fp = DEVICE_GET(mediaDevice, cp, TRUE);
if (fp) {
list = dispatch_load_fp(fp);
fclose(fp);
+6 -5
View File
@@ -269,8 +269,9 @@ installFixitCDROM(dialogMenuItem *self)
(void)rmdir("/mnt2");
while (1) {
msgConfirm("Please insert a FreeBSD live filesystem CD/DVD and press return");
if (DITEM_STATUS(mediaSetCDROM(NULL)) != DITEM_SUCCESS || !mediaDevice || !mediaDevice->init(mediaDevice)) {
msgConfirm("Please insert a FreeBSD live filesystem CDROM and press return");
if (DITEM_STATUS(mediaSetCDROM(NULL)) != DITEM_SUCCESS
|| !DEVICE_INIT(mediaDevice)) {
/* If we can't initialize it, it's probably not a FreeBSD CDROM so punt on it */
mediaClose();
if (msgYesNo("Unable to mount the disc - do you want to try again?") != 0)
@@ -357,7 +358,7 @@ installFixitFloppy(dialogMenuItem *self)
variable_set2(SYSTEM_STATE, "fixit", 0);
while (1) {
if (!mediaDevice->init(mediaDevice)) {
if (!DEVICE_INIT(mediaDevice)) {
if (msgYesNo("The attempt to mount the fixit floppy failed, bad floppy\n"
"or unclean filesystem. Do you want to try again?"))
return DITEM_FAILURE;
@@ -562,7 +563,7 @@ installStandard(dialogMenuItem *self)
Device *tmp = tcpDeviceSelect();
if (tmp && !((DevInfo *)tmp->private)->use_dhcp && !msgYesNo("Would you like to bring the %s interface up right now?", tmp->name))
if (!tmp->init(tmp))
if (!DEVICE_INIT(tmp))
msgConfirm("Initialization of %s device failed.", tmp->name);
}
dialog_clear_norefresh();
@@ -702,7 +703,7 @@ installCommit(dialogMenuItem *self)
return i;
try_media:
if (!mediaDevice->init(mediaDevice)) {
if (!DEVICE_INIT(mediaDevice)) {
if (!msgYesNo("Unable to initialize selected media. Would you like to\n"
"adjust your media configuration and try again?")) {
mediaDevice = NULL;
+7
View File
@@ -770,5 +770,12 @@ extern int dump_variables(dialogMenuItem *self);
/* wizard.c */
extern void slice_wizard(Disk *d);
/*
* Macros. Please find a better place for us!
*/
#define DEVICE_INIT(d) ((d) != NULL ? (d)->init((d)) : NULL)
#define DEVICE_GET(d, b, f) ((d) != NULL ? (d)->get((d), (b), (f)) : NULL)
#define DEVICE_SHUTDOWN(d) ((d) != NULL ? (d)->shutdown((d)) : (void)0)
#endif
/* _SYSINSTALL_H_INCLUDE */
+1 -1
View File
@@ -236,7 +236,7 @@ deviceReset(void)
int i;
for (i = 0; i < numDevs; i++) {
Devices[i]->shutdown(Devices[i]);
DEVICE_SHUTDOWN(Devices[i]);
/* XXX this potentially leaks Devices[i]->private if it's being
* used to point to something dynamic, but you're not supposed
+2 -2
View File
@@ -406,14 +406,14 @@ dispatch_load_floppy(dialogMenuItem *self)
return what;
}
if (!mediaDevice->init(mediaDevice)) {
if (!DEVICE_INIT(mediaDevice)) {
msgConfirm("Unable to mount floppy filesystem.");
what |= DITEM_FAILURE;
mediaClose();
return what;
}
fp = mediaDevice->get(mediaDevice, cp, TRUE);
fp = DEVICE_GET(mediaDevice, cp, TRUE);
if (fp) {
list = dispatch_load_fp(fp);
fclose(fp);
+10 -10
View File
@@ -605,14 +605,14 @@ distExtract(char *parent, Distribution *me)
snprintf(buf, sizeof buf, "%s/%s.inf", path, dist);
getinfo:
fp = mediaDevice->get(mediaDevice, buf, TRUE);
fp = DEVICE_GET(mediaDevice, buf, TRUE);
intr = check_for_interrupt();
if (fp == (FILE *)IO_ERROR || intr || !mediaDevice) {
/* Hard error, can't continue */
if (!msgYesNo("Unable to open %s: %s.\nReinitialize media?",
buf, !intr ? "I/O error." : "User interrupt.")) {
mediaDevice->shutdown(mediaDevice);
if (!mediaDevice->init(mediaDevice)) {
DEVICE_SHUTDOWN(mediaDevice);
if (!DEVICE_INIT(mediaDevice)) {
status = FALSE;
goto done;
}
@@ -652,7 +652,7 @@ distExtract(char *parent, Distribution *me)
* are not considered too significant.
*/
getsingle:
fp = mediaDevice->get(mediaDevice, buf, TRUE);
fp = DEVICE_GET(mediaDevice, buf, TRUE);
intr = check_for_interrupt();
if (fp == (FILE *)IO_ERROR || intr || !mediaDevice) {
/* Hard error, can't continue */
@@ -660,8 +660,8 @@ distExtract(char *parent, Distribution *me)
msgConfirm("Unable to open %s: User interrupt", buf);
else
msgConfirm("Unable to open %s: I/O error", buf);
mediaDevice->shutdown(mediaDevice);
if (!mediaDevice->init(mediaDevice)) {
DEVICE_SHUTDOWN(mediaDevice);
if (!DEVICE_INIT(mediaDevice)) {
status = FALSE;
goto done;
}
@@ -715,7 +715,7 @@ distExtract(char *parent, Distribution *me)
snprintf(buf, sizeof buf, "%s/%s.%c%c", path, dist, (chunk / 26) + 'a', (chunk % 26) + 'a');
if (isDebug())
msgDebug("trying for piece %d of %d: %s\n", chunk + 1, numchunks, buf);
fp = mediaDevice->get(mediaDevice, buf, FALSE);
fp = DEVICE_GET(mediaDevice, buf, FALSE);
intr = check_for_interrupt();
if (fp <= (FILE *)0 || intr) {
if (fp == (FILE *)0)
@@ -723,8 +723,8 @@ distExtract(char *parent, Distribution *me)
else
msgConfirm("failed to retreive piece file %s.\n"
"%s: Reinitializing media.", buf, !intr ? "I/O error" : "User interrupt");
mediaDevice->shutdown(mediaDevice);
if (!mediaDevice->init(mediaDevice))
DEVICE_SHUTDOWN(mediaDevice);
if (!DEVICE_INIT(mediaDevice))
goto punt;
else
goto getchunk;
@@ -867,7 +867,7 @@ distExtractAll(dialogMenuItem *self)
return DITEM_FAILURE;
}
if (!mediaVerify() || !mediaDevice->init(mediaDevice))
if (!mediaVerify() || !DEVICE_INIT(mediaDevice))
return DITEM_FAILURE;
old_dists = Dists;
+4 -4
View File
@@ -60,7 +60,7 @@ netUp(Device *dev)
Device *netdev = (Device *)dev->private;
if (netdev)
return netdev->init(netdev);
return DEVICE_INIT(netdev);
else
return TRUE; /* No net == happy net */
}
@@ -72,7 +72,7 @@ netDown(Device *dev)
Device *netdev = (Device *)dev->private;
if (netdev)
netdev->shutdown(netdev);
DEVICE_SHUTDOWN(netdev);
}
Boolean
@@ -228,8 +228,8 @@ mediaGetFTP(Device *dev, char *file, Boolean probe)
if (ftperr != 421) /* Timeout? */
variable_unset(VAR_FTP_PATH);
/* If we can't re-initialize, just forget it */
dev->shutdown(dev);
if (!dev->init(dev)) {
DEVICE_SHUTDOWN(dev);
if (!DEVICE_INIT(dev)) {
netDown(dev);
if (OpenConn) {
fclose(OpenConn);
+3 -3
View File
@@ -727,21 +727,21 @@ index_initialize(char *path)
}
/* Does it move when you kick it? */
if (!mediaDevice->init(mediaDevice)) {
if (!DEVICE_INIT(mediaDevice)) {
restorescr(w);
return DITEM_FAILURE;
}
dialog_clear_norefresh();
msgNotify("Attempting to fetch %s file from selected media.", path);
fp = mediaDevice->get(mediaDevice, path, TRUE);
fp = DEVICE_GET(mediaDevice, path, TRUE);
if (!fp) {
msgConfirm("Unable to get packages/INDEX file from selected media.\n\n"
"This may be because the packages collection is not available\n"
"on the distribution media you've chosen, most likely an FTP site\n"
"without the packages collection mirrored. Please verify that\n"
"your media, or your path to the media, is correct and try again.");
mediaDevice->shutdown(mediaDevice);
DEVICE_SHUTDOWN(mediaDevice);
restorescr(w);
return DITEM_FAILURE;
}
+6 -5
View File
@@ -269,8 +269,9 @@ installFixitCDROM(dialogMenuItem *self)
(void)rmdir("/mnt2");
while (1) {
msgConfirm("Please insert a FreeBSD live filesystem CD/DVD and press return");
if (DITEM_STATUS(mediaSetCDROM(NULL)) != DITEM_SUCCESS || !mediaDevice || !mediaDevice->init(mediaDevice)) {
msgConfirm("Please insert a FreeBSD live filesystem CDROM and press return");
if (DITEM_STATUS(mediaSetCDROM(NULL)) != DITEM_SUCCESS
|| !DEVICE_INIT(mediaDevice)) {
/* If we can't initialize it, it's probably not a FreeBSD CDROM so punt on it */
mediaClose();
if (msgYesNo("Unable to mount the disc - do you want to try again?") != 0)
@@ -357,7 +358,7 @@ installFixitFloppy(dialogMenuItem *self)
variable_set2(SYSTEM_STATE, "fixit", 0);
while (1) {
if (!mediaDevice->init(mediaDevice)) {
if (!DEVICE_INIT(mediaDevice)) {
if (msgYesNo("The attempt to mount the fixit floppy failed, bad floppy\n"
"or unclean filesystem. Do you want to try again?"))
return DITEM_FAILURE;
@@ -562,7 +563,7 @@ installStandard(dialogMenuItem *self)
Device *tmp = tcpDeviceSelect();
if (tmp && !((DevInfo *)tmp->private)->use_dhcp && !msgYesNo("Would you like to bring the %s interface up right now?", tmp->name))
if (!tmp->init(tmp))
if (!DEVICE_INIT(tmp))
msgConfirm("Initialization of %s device failed.", tmp->name);
}
dialog_clear_norefresh();
@@ -702,7 +703,7 @@ installCommit(dialogMenuItem *self)
return i;
try_media:
if (!mediaDevice->init(mediaDevice)) {
if (!DEVICE_INIT(mediaDevice)) {
if (!msgYesNo("Unable to initialize selected media. Would you like to\n"
"adjust your media configuration and try again?")) {
mediaDevice = NULL;
+2 -2
View File
@@ -308,7 +308,7 @@ installUpgrade(dialogMenuItem *self)
if (!mediaVerify())
return DITEM_FAILURE;
if (!mediaDevice->init(mediaDevice)) {
if (!DEVICE_INIT(mediaDevice)) {
if (!msgYesNo("Couldn't initialize the media. Would you like\n"
"to adjust your media selection and try again?")) {
mediaDevice = NULL;
@@ -442,7 +442,7 @@ installUpgradeNonInteractive(dialogMenuItem *self)
systemCreateHoloshell();
}
if (!mediaVerify() || !mediaDevice->init(mediaDevice)) {
if (!mediaVerify() || !DEVICE_INIT(mediaDevice)) {
msgNotify("Upgrade: Couldn't initialize media.");
return DITEM_FAILURE;
}
+7 -7
View File
@@ -125,7 +125,7 @@ void
mediaClose(void)
{
if (mediaDevice)
mediaDevice->shutdown(mediaDevice);
DEVICE_SHUTDOWN(mediaDevice);
mediaDevice = NULL;
}
@@ -361,13 +361,13 @@ mediaSetFTP(dialogMenuItem *self)
if (!networkDev || msgYesNo("You've already done the network configuration once,\n"
"would you like to skip over it now?") != 0) {
if (networkDev)
networkDev->shutdown(networkDev);
DEVICE_SHUTDOWN(networkDev);
if (!(networkDev = tcpDeviceSelect())) {
variable_unset(VAR_FTP_PATH);
return DITEM_FAILURE;
}
}
if (!networkDev->init(networkDev)) {
if (!DEVICE_INIT(networkDev)) {
if (isDebug())
msgDebug("mediaSetFTP: Net device init failed.\n");
variable_unset(VAR_FTP_PATH);
@@ -415,7 +415,7 @@ mediaSetFTP(dialogMenuItem *self)
" your\nname server, gateway and network interface are"
" correctly configured?", hostname);
if (networkDev)
networkDev->shutdown(networkDev);
DEVICE_SHUTDOWN(networkDev);
networkDev = NULL;
variable_unset(VAR_FTP_PATH);
return DITEM_FAILURE;
@@ -558,11 +558,11 @@ mediaSetNFS(dialogMenuItem *self)
if (!networkDev || msgYesNo("You've already done the network configuration once,\n"
"would you like to skip over it now?") != 0) {
if (networkDev)
networkDev->shutdown(networkDev);
DEVICE_SHUTDOWN(networkDev);
if (!(networkDev = tcpDeviceSelect()))
return DITEM_FAILURE;
}
if (!networkDev->init(networkDev)) {
if (!DEVICE_INIT(networkDev)) {
if (isDebug())
msgDebug("mediaSetNFS: Net device init failed\n");
}
@@ -572,7 +572,7 @@ mediaSetNFS(dialogMenuItem *self)
msgConfirm("Cannot resolve hostname `%s'! Are you sure that your\n"
"name server, gateway and network interface are correctly configured?", hostname);
if (networkDev)
networkDev->shutdown(networkDev);
DEVICE_SHUTDOWN(networkDev);
networkDev = NULL;
variable_unset(VAR_NFS_PATH);
return DITEM_FAILURE;
+2 -2
View File
@@ -53,7 +53,7 @@ mediaInitNFS(Device *dev)
if (NFSMounted)
return TRUE;
if (netDevice && !netDevice->init(netDevice))
if (!DEVICE_INIT(netDevice))
return FALSE;
if (Mkdir(mountpoint))
@@ -65,7 +65,7 @@ mediaInitNFS(Device *dev)
variable_get(VAR_NFS_SECURE) ? "-P" : "", dev->name, mountpoint)) {
msgConfirm("Error mounting %s on %s: %s.", dev->name, mountpoint, strerror(errno));
if (netDevice)
netDevice->shutdown(netDevice);
DEVICE_SHUTDOWN(netDevice);
restorescr(w);
return FALSE;
}
+3 -3
View File
@@ -61,7 +61,7 @@ package_add(char *name)
if (!mediaVerify())
return DITEM_FAILURE;
if (!mediaDevice->init(mediaDevice))
if (!DEVICE_INIT(mediaDevice))
return DITEM_FAILURE;
i = index_initialize("packages/INDEX");
@@ -123,7 +123,7 @@ package_extract(Device *dev, char *name, Boolean depended)
if (package_exists(name))
return DITEM_SUCCESS;
if (!dev->init(dev)) {
if (!DEVICE_INIT(dev)) {
msgConfirm("Unable to initialize media type for package extract.");
return DITEM_FAILURE;
}
@@ -154,7 +154,7 @@ package_extract(Device *dev, char *name, Boolean depended)
sprintf(path, "%s%s", name, strstr(name, ".tgz") ? "" : ".tgz");
/* We have a path, call the device strategy routine to get the file */
fp = dev->get(dev, path, TRUE);
fp = DEVICE_GET(dev, path, TRUE);
if (fp) {
int i = 0, tot, pfd[2];
pid_t pid;
+7
View File
@@ -770,5 +770,12 @@ extern int dump_variables(dialogMenuItem *self);
/* wizard.c */
extern void slice_wizard(Disk *d);
/*
* Macros. Please find a better place for us!
*/
#define DEVICE_INIT(d) ((d) != NULL ? (d)->init((d)) : NULL)
#define DEVICE_GET(d, b, f) ((d) != NULL ? (d)->get((d), (b), (f)) : NULL)
#define DEVICE_SHUTDOWN(d) ((d) != NULL ? (d)->shutdown((d)) : (void)0)
#endif
/* _SYSINSTALL_H_INCLUDE */
+1 -1
View File
@@ -644,7 +644,7 @@ tcpMenuSelect(dialogMenuItem *self)
variable_unset("NETWORK_CONFIGURED");
save = savescr();
if (tmp && tmp->private && !((DevInfo *)tmp->private)->use_dhcp && !msgYesNo("Would you like to bring the %s interface up right now?", tmp->name))
if (!tmp->init(tmp))
if (!DEVICE_INIT(tmp))
msgConfirm("Initialization of %s device failed.", tmp->name);
restorescr(save);
return DITEM_SUCCESS;