Skip to content

Commit dd7666e

Browse files
ebannernico
authored andcommitted
lspci: Propagate errors
1 parent 91a83de commit dd7666e

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

Userland/Utilities/lspci.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,15 @@ static constexpr StringView format_numerical = "{:04x}:{:02x}:{:02x}.{} {}: {}:{
2525
static constexpr StringView format_textual = "{:04x}:{:02x}:{:02x}.{} {}: {} {} (rev {:02x})"sv;
2626
static constexpr StringView format_region = "\tBAR {}: {} region @ {:#x}"sv;
2727

28-
static u32 read_hex_string_from_bytebuffer(ByteBuffer const& buf)
28+
static ErrorOr<u32> read_hex_string_from_bytebuffer(ByteBuffer const& buf)
2929
{
30-
// FIXME: Propagate errors.
31-
return AK::StringUtils::convert_to_uint_from_hex(
32-
ByteString(MUST(buf.slice(2, buf.size() - 2)).bytes()))
33-
.release_value();
30+
auto slice_result = TRY(buf.slice(2, buf.size() - 2));
31+
auto hex_string = ByteString(slice_result.bytes());
32+
auto result = AK::StringUtils::convert_to_uint_from_hex(hex_string);
33+
if (!result.has_value())
34+
return Error::from_string_literal("Failed to convert hex string to number");
35+
36+
return result.release_value();
3437
}
3538

3639
static u32 convert_sysfs_value_to_uint(ByteString const& value)
@@ -125,35 +128,35 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
125128
dbgln("Error: Could not read {}: {}", vendor_id_filename, vendor_id_contents.error());
126129
continue;
127130
}
128-
u32 vendor_id = read_hex_string_from_bytebuffer(vendor_id_contents.value());
131+
u32 vendor_id = TRY(read_hex_string_from_bytebuffer(vendor_id_contents.value()));
129132

130133
auto device_id_contents = device_id_file.value()->read_until_eof();
131134
if (device_id_contents.is_error()) {
132135
dbgln("Error: Could not read {}: {}", device_id_filename, device_id_contents.error());
133136
continue;
134137
}
135-
u32 device_id = read_hex_string_from_bytebuffer(device_id_contents.value());
138+
u32 device_id = TRY(read_hex_string_from_bytebuffer(device_id_contents.value()));
136139

137140
auto revision_id_contents = revision_id_file.value()->read_until_eof();
138141
if (revision_id_contents.is_error()) {
139142
dbgln("Error: Could not read {}: {}", revision_id_filename, revision_id_contents.error());
140143
continue;
141144
}
142-
u32 revision_id = read_hex_string_from_bytebuffer(revision_id_contents.value());
145+
u32 revision_id = TRY(read_hex_string_from_bytebuffer(revision_id_contents.value()));
143146

144147
auto class_id_contents = class_id_file.value()->read_until_eof();
145148
if (class_id_contents.is_error()) {
146149
dbgln("Error: Could not read {}: {}", class_id_filename, class_id_contents.error());
147150
continue;
148151
}
149-
u32 class_id = read_hex_string_from_bytebuffer(class_id_contents.value());
152+
u32 class_id = TRY(read_hex_string_from_bytebuffer(class_id_contents.value()));
150153

151154
auto subclass_id_contents = subclass_id_file.value()->read_until_eof();
152155
if (subclass_id_contents.is_error()) {
153156
dbgln("Error: Could not read {}: {}", subclass_id_filename, subclass_id_contents.error());
154157
continue;
155158
}
156-
u32 subclass_id = read_hex_string_from_bytebuffer(subclass_id_contents.value());
159+
u32 subclass_id = TRY(read_hex_string_from_bytebuffer(subclass_id_contents.value()));
157160

158161
ByteString vendor_name;
159162
ByteString device_name;
@@ -190,7 +193,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
190193
continue;
191194
}
192195

193-
u32 bar_value = read_hex_string_from_bytebuffer(bar_value_contents.value());
196+
u32 bar_value = TRY(read_hex_string_from_bytebuffer(bar_value_contents.value()));
194197
if (bar_value == 0)
195198
continue;
196199
bool memory_region = ((bar_value & 1) == 0);

0 commit comments

Comments
 (0)