diff options
author | Burt P <pburt0@gmail.com> | 2017-07-25 14:33:49 -0500 |
---|---|---|
committer | Leandro A. F. Pereira <leandro@hardinfo.org> | 2017-07-25 19:02:32 -0700 |
commit | 81ae37c58709cf1396b09e5136acfc8a0c259476 (patch) | |
tree | f3e502ab9841dd0b1815ef4848d226cba87037ab | |
parent | 8e4746423c20b525671998b1bc94b3b6d8ec78d6 (diff) |
device tree: fix crash when device tree is not found
* check if device tree was found before add_keys()
* UNKIFNULL(model) before strcmp
* make UNKIFNULL() STRIFNULL() EMPIFNULL() macros more generic
Signed-off-by: Burt P <pburt0@gmail.com>
-rw-r--r-- | includes/cpu_util.h | 3 | ||||
-rw-r--r-- | includes/dt_util.h | 1 | ||||
-rw-r--r-- | modules/devices/arm/processor.c | 16 | ||||
-rw-r--r-- | modules/devices/devicetree.c | 7 | ||||
-rw-r--r-- | modules/devices/devicetree/dt_util.c | 23 | ||||
-rw-r--r-- | modules/devices/devicetree/pmac_data.c | 15 | ||||
-rw-r--r-- | modules/devices/ia64/processor.c | 12 | ||||
-rw-r--r-- | modules/devices/parisc/processor.c | 6 | ||||
-rw-r--r-- | modules/devices/ppc/processor.c | 4 | ||||
-rw-r--r-- | modules/devices/riscv/processor.c | 8 | ||||
-rw-r--r-- | modules/devices/s390/processor.c | 4 | ||||
-rw-r--r-- | modules/devices/sh/processor.c | 4 |
12 files changed, 60 insertions, 43 deletions
diff --git a/includes/cpu_util.h b/includes/cpu_util.h index 36ca3c2d..da581a78 100644 --- a/includes/cpu_util.h +++ b/includes/cpu_util.h @@ -7,8 +7,7 @@ #define PROC_CPUINFO "/proc/cpuinfo" #endif -/* needs a local Processor *processor */ -#define STRIFNULL(f,cs) if (processor->f == NULL) processor->f = g_strdup(cs); +#define STRIFNULL(f,cs) if (f == NULL) f = g_strdup(cs); #define UNKIFNULL(f) STRIFNULL(f, _("(Unknown)") ) #define EMPIFNULL(f) STRIFNULL(f, "") diff --git a/includes/dt_util.h b/includes/dt_util.h index e077279b..acc4ad16 100644 --- a/includes/dt_util.h +++ b/includes/dt_util.h @@ -42,6 +42,7 @@ typedef struct _dtr_obj dtr_obj; dtr *dtr_new(char *base_path); /* NULL for DTR_ROOT */ void dtr_free(dtr *); +int dtr_was_found(dtr *); const char *dtr_base_path(dtr *); char *dtr_messages(dtr *); /* returns a message log */ diff --git a/modules/devices/arm/processor.c b/modules/devices/arm/processor.c index aef683b9..24c99812 100644 --- a/modules/devices/arm/processor.c +++ b/modules/devices/arm/processor.c @@ -138,19 +138,19 @@ processor_scan(void) processor = (Processor *) pi->data; /* strings can't be null or segfault later */ - STRIFNULL(model_name, _("ARM Processor") ); - EMPIFNULL(flags); - UNKIFNULL(cpu_implementer); - UNKIFNULL(cpu_architecture); - UNKIFNULL(cpu_variant); - UNKIFNULL(cpu_part); - UNKIFNULL(cpu_revision); + STRIFNULL(processor->model_name, _("ARM Processor") ); + EMPIFNULL(processor->flags); + UNKIFNULL(processor->cpu_implementer); + UNKIFNULL(processor->cpu_architecture); + UNKIFNULL(processor->cpu_variant); + UNKIFNULL(processor->cpu_part); + UNKIFNULL(processor->cpu_revision); processor->decoded_name = arm_decoded_name( processor->cpu_implementer, processor->cpu_part, processor->cpu_variant, processor->cpu_revision, processor->cpu_architecture, processor->model_name); - UNKIFNULL(decoded_name); + UNKIFNULL(processor->decoded_name); /* topo & freq */ processor->cpufreq = cpufreq_new(processor->id); diff --git a/modules/devices/devicetree.c b/modules/devices/devicetree.c index c9b9542b..7401a7b4 100644 --- a/modules/devices/devicetree.c +++ b/modules/devices/devicetree.c @@ -25,6 +25,7 @@ #include <sys/types.h> #include <stdint.h> #include "devices.h" +#include "cpu_util.h" #include "dt_util.h" /* Hardinfo labels that have # are truncated and/or hidden. @@ -166,6 +167,8 @@ gchar *get_summary() { model = get_dt_string("/model", 0); compat = get_dt_string("/compatible", 1); + UNKIFNULL(model); + EMPIFNULL(compat); /* Expand on the DT information from known machines, like RPi. * RPi stores a revision value in /proc/cpuinfo that can be used @@ -217,6 +220,7 @@ gchar *get_summary() { /* fallback */ if (ret == NULL) { tmp[0] = get_dt_string("/serial-number", 1); + EMPIFNULL(tmp[0]); ret = g_strdup_printf( "[%s]\n" "%s=%s\n" @@ -308,7 +312,8 @@ void __scan_dtree() mi_add("Summary", summary); mi_add("Maps", maps); - add_keys("/"); + if(dtr_was_found(dt)) + add_keys("/"); messages = msg_section(0); mi_add("Messages", messages); diff --git a/modules/devices/devicetree/dt_util.c b/modules/devices/devicetree/dt_util.c index 6ef41bd6..619e63be 100644 --- a/modules/devices/devicetree/dt_util.c +++ b/modules/devices/devicetree/dt_util.c @@ -221,19 +221,24 @@ const char *dtr_find_device_tree_root() { return candidates[i]; i++; } - return "/did/not/find/device-tree"; + return NULL; } dtr *dtr_new_x(char *base_path, int fast) { dtr *dt = malloc(sizeof(dtr)); if (dt != NULL) { memset(dt, 0, sizeof(dtr)); + dt->log = strdup(""); + + if (base_path == NULL) + base_path = DTR_ROOT; + if (base_path != NULL) dt->base_path = strdup(base_path); - else - dt->base_path = strdup(DTR_ROOT); - - dt->log = strdup(""); + else { + dtr_msg(dt, "%s", "Device Tree not found."); + return dt; + } /* build alias and phandle lists */ dt->aliases = NULL; @@ -263,6 +268,14 @@ void dtr_free(dtr *s) { } } +int dtr_was_found(dtr *s) { + if (s != NULL) { + if (s->base_path != NULL) + return 1; + } + return 0; +} + void dtr_msg(dtr *s, char *fmt, ...) { gchar *buf, *tmp; va_list args; diff --git a/modules/devices/devicetree/pmac_data.c b/modules/devices/devicetree/pmac_data.c index 01b08419..b236aef1 100644 --- a/modules/devices/devicetree/pmac_data.c +++ b/modules/devices/devicetree/pmac_data.c @@ -58,14 +58,13 @@ static gchar *ppc_mac_details(void) { if (machine == NULL) goto pmd_exit; -#define _UNKIFNULL(STR) if (STR == NULL) { STR = strdup(_("(Unknown)")); } - _UNKIFNULL(platform); - _UNKIFNULL(model); - _UNKIFNULL(motherboard); - _UNKIFNULL(detected_as); - _UNKIFNULL(pmac_flags); - _UNKIFNULL(l2_cache); - _UNKIFNULL(pmac_gen); + UNKIFNULL(platform); + UNKIFNULL(model); + UNKIFNULL(motherboard); + UNKIFNULL(detected_as); + UNKIFNULL(pmac_flags); + UNKIFNULL(l2_cache); + UNKIFNULL(pmac_gen); ret = g_strdup_printf("[%s]\n" "%s=%s\n" diff --git a/modules/devices/ia64/processor.c b/modules/devices/ia64/processor.c index cbe1bc2e..c617f096 100644 --- a/modules/devices/ia64/processor.c +++ b/modules/devices/ia64/processor.c @@ -105,12 +105,12 @@ processor_scan(void) processor = (Processor *) pi->data; /* strings can't be null or segfault later */ - STRIFNULL(model_name, _("IA64 Processor") ); - UNKIFNULL(vendor_id); - STRIFNULL(arch, "IA-64"); - STRIFNULL(archrev, "0"); - UNKIFNULL(family); - UNKIFNULL(features); + STRIFNULL(processor->model_name, _("IA64 Processor") ); + UNKIFNULL(processor->vendor_id); + STRIFNULL(processor->arch, "IA-64"); + STRIFNULL(processor->archrev, "0"); + UNKIFNULL(processor->family); + UNKIFNULL(processor->features); /* topo & freq */ processor->cpufreq = cpufreq_new(processor->id); diff --git a/modules/devices/parisc/processor.c b/modules/devices/parisc/processor.c index c4eede8a..1a5b6f46 100644 --- a/modules/devices/parisc/processor.c +++ b/modules/devices/parisc/processor.c @@ -104,9 +104,9 @@ processor_scan(void) processor = (Processor *) pi->data; /* strings can't be null or segfault later */ - STRIFNULL(model_name, _("PA-RISC Processor") ); - STRIFNULL(cpu_family, "PA-RISC"); - UNKIFNULL(strmodel); + STRIFNULL(processor->model_name, _("PA-RISC Processor") ); + STRIFNULL(processor->cpu_family, "PA-RISC"); + UNKIFNULL(processor->strmodel); /* topo & freq */ processor->cpufreq = cpufreq_new(processor->id); diff --git a/modules/devices/ppc/processor.c b/modules/devices/ppc/processor.c index 2692d541..f06c90a3 100644 --- a/modules/devices/ppc/processor.c +++ b/modules/devices/ppc/processor.c @@ -114,8 +114,8 @@ processor_scan(void) processor = (Processor *) pi->data; /* strings can't be null or segfault later */ - STRIFNULL(model_name, _("POWER Processor") ); - UNKIFNULL(revision); + STRIFNULL(processor->model_name, _("POWER Processor") ); + UNKIFNULL(processor->revision); /* topo & freq */ processor->cpufreq = cpufreq_new(processor->id); diff --git a/modules/devices/riscv/processor.c b/modules/devices/riscv/processor.c index b2976a3e..740742a5 100644 --- a/modules/devices/riscv/processor.c +++ b/modules/devices/riscv/processor.c @@ -104,10 +104,10 @@ processor_scan(void) processor = (Processor *) pi->data; /* strings can't be null or segfault later */ - STRIFNULL(model_name, _("RISC-V Processor") ); - UNKIFNULL(mmu); - UNKIFNULL(isa); - UNKIFNULL(uarch); + STRIFNULL(processor->model_name, _("RISC-V Processor") ); + UNKIFNULL(processor->mmu); + UNKIFNULL(processor->isa); + UNKIFNULL(processor->uarch); processor->flags = riscv_isa_to_flags(processor->isa); diff --git a/modules/devices/s390/processor.c b/modules/devices/s390/processor.c index 9a9b789b..b075a53c 100644 --- a/modules/devices/s390/processor.c +++ b/modules/devices/s390/processor.c @@ -89,8 +89,8 @@ processor_scan(void) processor = (Processor *) pi->data; /* strings can't be null or segfault later */ - STRIFNULL(model_name, _("S390 Processor") ); - UNKIFNULL(proc_str); + STRIFNULL(processor->model_name, _("S390 Processor") ); + UNKIFNULL(processor->proc_str); /* topo & freq */ processor->cpufreq = cpufreq_new(processor->id); diff --git a/modules/devices/sh/processor.c b/modules/devices/sh/processor.c index b1f0e5d6..05a90191 100644 --- a/modules/devices/sh/processor.c +++ b/modules/devices/sh/processor.c @@ -52,8 +52,8 @@ processor_scan(void) fclose(cpuinfo); - STRIFNULL(model_name, _("SuperH Processor")); - UNKIFNULL(vendor_id); + STRIFNULL(processor->model_name, _("SuperH Processor")); + UNKIFNULL(processor->vendor_id); return g_slist_append(NULL, processor); } |