Fix a few potential segfaults found in device.c

This commit is contained in:
Jasmine Iwanek
2025-01-06 21:48:38 -05:00
parent 3d7df5f87c
commit 29b6cd484c

View File

@@ -660,14 +660,16 @@ device_get_config_string(const char *str)
int int
device_get_config_int(const char *str) device_get_config_int(const char *str)
{ {
if (device_current.dev != NULL) {
const device_config_t *cfg = device_current.dev->config; const device_config_t *cfg = device_current.dev->config;
while (cfg && cfg->type != CONFIG_END) { while ((cfg != NULL) && (cfg->type != CONFIG_END)) {
if (!strcmp(str, cfg->name)) if (!strcmp(str, cfg->name))
return (config_get_int((char *) device_current.name, (char *) str, cfg->default_int)); return (config_get_int((char *) device_current.name, (char *) str, cfg->default_int));
cfg++; cfg++;
} }
}
return 0; return 0;
} }
@@ -675,14 +677,16 @@ device_get_config_int(const char *str)
int int
device_get_config_int_ex(const char *str, int def) device_get_config_int_ex(const char *str, int def)
{ {
if (device_current.dev != NULL) {
const device_config_t *cfg = device_current.dev->config; const device_config_t *cfg = device_current.dev->config;
while (cfg && cfg->type != CONFIG_END) { while ((cfg != NULL) && (cfg->type != CONFIG_END)) {
if (!strcmp(str, cfg->name)) if (!strcmp(str, cfg->name))
return (config_get_int((char *) device_current.name, (char *) str, def)); return (config_get_int((char *) device_current.name, (char *) str, def));
cfg++; cfg++;
} }
}
return def; return def;
} }
@@ -690,14 +694,16 @@ device_get_config_int_ex(const char *str, int def)
int int
device_get_config_hex16(const char *str) device_get_config_hex16(const char *str)
{ {
if (device_current.dev != NULL) {
const device_config_t *cfg = device_current.dev->config; const device_config_t *cfg = device_current.dev->config;
while (cfg && cfg->type != CONFIG_END) { while ((cfg != NULL) && (cfg->type != CONFIG_END)) {
if (!strcmp(str, cfg->name)) if (!strcmp(str, cfg->name))
return (config_get_hex16((char *) device_current.name, (char *) str, cfg->default_int)); return (config_get_hex16((char *) device_current.name, (char *) str, cfg->default_int));
cfg++; cfg++;
} }
}
return 0; return 0;
} }
@@ -705,14 +711,16 @@ device_get_config_hex16(const char *str)
int int
device_get_config_hex20(const char *str) device_get_config_hex20(const char *str)
{ {
if (device_current.dev != NULL) {
const device_config_t *cfg = device_current.dev->config; const device_config_t *cfg = device_current.dev->config;
while (cfg && cfg->type != CONFIG_END) { while ((cfg != NULL) && (cfg->type != CONFIG_END)) {
if (!strcmp(str, cfg->name)) if (!strcmp(str, cfg->name))
return (config_get_hex20((char *) device_current.name, (char *) str, cfg->default_int)); return (config_get_hex20((char *) device_current.name, (char *) str, cfg->default_int));
cfg++; cfg++;
} }
}
return 0; return 0;
} }
@@ -720,14 +728,16 @@ device_get_config_hex20(const char *str)
int int
device_get_config_mac(const char *str, int def) device_get_config_mac(const char *str, int def)
{ {
if (device_current.dev != NULL) {
const device_config_t *cfg = device_current.dev->config; const device_config_t *cfg = device_current.dev->config;
while (cfg && cfg->type != CONFIG_END) { while ((cfg != NULL) && (cfg->type != CONFIG_END)) {
if (!strcmp(str, cfg->name)) if (!strcmp(str, cfg->name))
return (config_get_mac((char *) device_current.name, (char *) str, def)); return (config_get_mac((char *) device_current.name, (char *) str, def));
cfg++; cfg++;
} }
}
return def; return def;
} }
@@ -735,9 +745,10 @@ device_get_config_mac(const char *str, int def)
void void
device_set_config_int(const char *str, int val) device_set_config_int(const char *str, int val)
{ {
if (device_current.dev != NULL) {
const device_config_t *cfg = device_current.dev->config; const device_config_t *cfg = device_current.dev->config;
while (cfg && cfg->type != CONFIG_END) { while ((cfg != NULL) && (cfg->type != CONFIG_END)) {
if (!strcmp(str, cfg->name)) { if (!strcmp(str, cfg->name)) {
config_set_int((char *) device_current.name, (char *) str, val); config_set_int((char *) device_current.name, (char *) str, val);
break; break;
@@ -746,13 +757,15 @@ device_set_config_int(const char *str, int val)
cfg++; cfg++;
} }
} }
}
void void
device_set_config_hex16(const char *str, int val) device_set_config_hex16(const char *str, int val)
{ {
if (device_current.dev != NULL) {
const device_config_t *cfg = device_current.dev->config; const device_config_t *cfg = device_current.dev->config;
while (cfg && cfg->type != CONFIG_END) { while ((cfg != NULL) && (cfg->type != CONFIG_END)) {
if (!strcmp(str, cfg->name)) { if (!strcmp(str, cfg->name)) {
config_set_hex16((char *) device_current.name, (char *) str, val); config_set_hex16((char *) device_current.name, (char *) str, val);
break; break;
@@ -761,13 +774,15 @@ device_set_config_hex16(const char *str, int val)
cfg++; cfg++;
} }
} }
}
void void
device_set_config_hex20(const char *str, int val) device_set_config_hex20(const char *str, int val)
{ {
if (device_current.dev != NULL) {
const device_config_t *cfg = device_current.dev->config; const device_config_t *cfg = device_current.dev->config;
while (cfg && cfg->type != CONFIG_END) { while ((cfg != NULL) && (cfg->type != CONFIG_END)) {
if (!strcmp(str, cfg->name)) { if (!strcmp(str, cfg->name)) {
config_set_hex20((char *) device_current.name, (char *) str, val); config_set_hex20((char *) device_current.name, (char *) str, val);
break; break;
@@ -776,13 +791,15 @@ device_set_config_hex20(const char *str, int val)
cfg++; cfg++;
} }
} }
}
void void
device_set_config_mac(const char *str, int val) device_set_config_mac(const char *str, int val)
{ {
if (device_current.dev != NULL) {
const device_config_t *cfg = device_current.dev->config; const device_config_t *cfg = device_current.dev->config;
while (cfg && cfg->type != CONFIG_END) { while ((cfg != NULL) && (cfg->type != CONFIG_END)) {
if (!strcmp(str, cfg->name)) { if (!strcmp(str, cfg->name)) {
config_set_mac((char *) device_current.name, (char *) str, val); config_set_mac((char *) device_current.name, (char *) str, val);
break; break;
@@ -791,6 +808,7 @@ device_set_config_mac(const char *str, int val)
cfg++; cfg++;
} }
} }
}
int int
device_is_valid(const device_t *device, int mch) device_is_valid(const device_t *device, int mch)
@@ -807,19 +825,17 @@ int
machine_get_config_int(char *str) machine_get_config_int(char *str)
{ {
const device_t *dev = machine_get_device(machine); const device_t *dev = machine_get_device(machine);
const device_config_t *cfg;
if (dev == NULL) if (dev != NULL) {
return 0; const device_config_t *cfg = dev->config;
cfg = dev->config; while ((cfg != NULL) && (cfg->type != CONFIG_END)) {
while (cfg && cfg->type != CONFIG_END) {
if (!strcmp(str, cfg->name)) if (!strcmp(str, cfg->name))
return (config_get_int((char *) dev->name, str, cfg->default_int)); return (config_get_int((char *) dev->name, str, cfg->default_int));
cfg++; cfg++;
} }
}
return 0; return 0;
} }
@@ -830,9 +846,8 @@ machine_get_config_string(char *str)
const char *ret = ""; const char *ret = "";
if (dev != NULL) { if (dev != NULL) {
const device_config_t *cfg; const device_config_t *cfg = dev->config;
cfg = dev->config;
while ((cfg != NULL) && (cfg->type != CONFIG_END)) { while ((cfg != NULL) && (cfg->type != CONFIG_END)) {
if (!strcmp(str, cfg->name)) { if (!strcmp(str, cfg->name)) {
const char *s = config_get_string((char *) dev->name, str, const char *s = config_get_string((char *) dev->name, str,