From c4c413f5daea4b6b8ffcf4825e7e627500d19b73 Mon Sep 17 00:00:00 2001
From: PieterPan <haiku@panman.eu>
Date: Sat, 16 Nov 2013 23:17:44 +0000
Subject: [PATCH] Devices: Fix crash for devices that returned unexpected
categoryindex * Store the length of the category strings array * Check that
before indexing the array * Improve comment * Should fix #10186
---
src/apps/devices/Device.cpp | 3 ++-
src/apps/devices/Device.h | 1 +
src/apps/devices/DevicesView.cpp | 7 +++++++
3 files changed, 10 insertions(+), 1 deletion(-)
diff --git a/src/apps/devices/Device.cpp b/src/apps/devices/Device.cpp
index d306bc8..bc92b7b 100644
a
|
b
|
|
16 | 16 | #undef B_TRANSLATION_CONTEXT |
17 | 17 | #define B_TRANSLATION_CONTEXT "Device" |
18 | 18 | |
19 | | // This list comes from the pciid list, except for the last one |
| 19 | // This list comes from the pciid list, except for the last ones |
20 | 20 | const char* kCategoryString[] = { |
21 | 21 | B_TRANSLATE("Unclassified device"), // 0x00 |
22 | 22 | B_TRANSLATE("Mass storage controller"), // 0x01 |
… |
… |
const char* kCategoryString[] = {
|
39 | 39 | B_TRANSLATE("Computer"), // 0x12 (added later) |
40 | 40 | B_TRANSLATE("ACPI controller") // 0x13 (added later) |
41 | 41 | }; |
| 42 | const int kCategoryStringLength = sizeof(kCategoryString)/sizeof(char *); |
42 | 43 | |
43 | 44 | // This list is only used to translate Device properties |
44 | 45 | B_TRANSLATE_MARK_VOID("unknown"); |
diff --git a/src/apps/devices/Device.h b/src/apps/devices/Device.h
index caf36db..32b2e03 100644
a
|
b
|
typedef enum {
|
68 | 68 | |
69 | 69 | |
70 | 70 | extern const char* kCategoryString[]; |
| 71 | extern const int kCategoryStringLength; |
71 | 72 | |
72 | 73 | |
73 | 74 | class Device : public BStringItem { |
diff --git a/src/apps/devices/DevicesView.cpp b/src/apps/devices/DevicesView.cpp
index c14111b..96db804 100644
a
|
b
|
DevicesView::CreateCategoryMap()
|
146 | 146 | CategoryMapIterator iter; |
147 | 147 | for (unsigned int i = 0; i < fDevices.size(); i++) { |
148 | 148 | Category category = fDevices[i]->GetCategory(); |
| 149 | if (category < 0 || category >= kCategoryStringLength) { |
| 150 | std::cerr << "CreateCategoryMap: device " << fDevices[i]->GetName() |
| 151 | << " returned category index " << category << ", which is not known. " |
| 152 | << "Skipping device." << std::endl; |
| 153 | continue; |
| 154 | } |
| 155 | |
149 | 156 | const char* categoryName = kCategoryString[category]; |
150 | 157 | |
151 | 158 | iter = fCategoryMap.find(category); |