From bc0c61cd7fa9cc5b991f8def6afb5fab9d1baa95 Mon Sep 17 00:00:00 2001 From: Tim Bradshaw Date: Thu, 20 Dec 2012 22:26:34 +0000 Subject: [PATCH] linux: ensure that all CPUs have model information --- src/unix/linux/linux-core.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/unix/linux/linux-core.c b/src/unix/linux/linux-core.c index 8276bf97..b591314f 100644 --- a/src/unix/linux/linux-core.c +++ b/src/unix/linux/linux-core.c @@ -550,11 +550,13 @@ static void read_models(unsigned int numcpus, uv_cpu_info_t* ci) { static const char model_marker[] = ""; static const char speed_marker[] = ""; #endif + static const char bogus_model[] = "unknown"; unsigned int model_idx; unsigned int speed_idx; char buf[1024]; char* model; FILE* fp; + char* inferred_model; fp = fopen("/proc/cpuinfo", "r"); if (fp == NULL) @@ -583,6 +585,26 @@ static void read_models(unsigned int numcpus, uv_cpu_info_t* ci) { } } fclose(fp); + + /* Now we want to make sure that all the models contain *something*: + * it's not safe to leave them as null. + */ + if (model_idx == 0) { + /* No models at all: fake up the first one. */ + ci[0].model = strndup(bogus_model, sizeof(bogus_model) - 1); + model_idx = 1; + } + + /* Not enough models, but we do have at least one. So we'll just + * copy the rest down: it might be better to indicate somehow that + * the remaining ones have been guessed. + */ + inferred_model = ci[model_idx - 1].model; + + while (model_idx < numcpus) { + ci[model_idx].model = strndup(inferred_model, strlen(inferred_model)); + model_idx++; + } }