1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 12:07:45 +00:00

LibGfx/JPEG: Check for element presence in HashMaps before dereferencing

This patch replaces vague checks with more precise ones and uses `get`
instead of `find` to retrieve an element of a hashmap.
This commit is contained in:
Lucas CHOLLET 2023-02-26 00:24:51 -05:00 committed by Linus Groh
parent 18064fdb92
commit 342c94c4d7

View file

@ -313,7 +313,13 @@ static inline i32* get_component(Macroblock& block, unsigned component)
static ErrorOr<void> add_dc(JPEGLoadingContext& context, Macroblock& macroblock, ScanComponent const& scan_component) static ErrorOr<void> add_dc(JPEGLoadingContext& context, Macroblock& macroblock, ScanComponent const& scan_component)
{ {
auto& dc_table = context.dc_tables.find(scan_component.dc_destination_id)->value; auto maybe_table = context.dc_tables.get(scan_component.dc_destination_id);
if (!maybe_table.has_value()) {
dbgln_if(JPEG_DEBUG, "Unable to find a DC table with id: {}", scan_component.dc_destination_id);
return Error::from_string_literal("Unable to find corresponding DC table");
}
auto& dc_table = maybe_table.value();
auto& scan = context.current_scan; auto& scan = context.current_scan;
// For DC coefficients, symbol encodes the length of the coefficient. // For DC coefficients, symbol encodes the length of the coefficient.
@ -357,7 +363,13 @@ static ErrorOr<bool> read_eob(Scan& scan, u32 symbol)
static ErrorOr<void> add_ac(JPEGLoadingContext& context, Macroblock& macroblock, ScanComponent const& scan_component) static ErrorOr<void> add_ac(JPEGLoadingContext& context, Macroblock& macroblock, ScanComponent const& scan_component)
{ {
auto& ac_table = context.ac_tables.find(scan_component.ac_destination_id)->value; auto maybe_table = context.ac_tables.get(scan_component.ac_destination_id);
if (!maybe_table.has_value()) {
dbgln_if(JPEG_DEBUG, "Unable to find a AC table with id: {}", scan_component.ac_destination_id);
return Error::from_string_literal("Unable to find corresponding AC table");
}
auto& ac_table = maybe_table.value();
auto* select_component = get_component(macroblock, scan_component.component.index); auto* select_component = get_component(macroblock, scan_component.component.index);
auto& scan = context.current_scan; auto& scan = context.current_scan;
@ -421,11 +433,6 @@ static ErrorOr<void> add_ac(JPEGLoadingContext& context, Macroblock& macroblock,
static ErrorOr<void> build_macroblocks(JPEGLoadingContext& context, Vector<Macroblock>& macroblocks, u32 hcursor, u32 vcursor) static ErrorOr<void> build_macroblocks(JPEGLoadingContext& context, Vector<Macroblock>& macroblocks, u32 hcursor, u32 vcursor)
{ {
for (auto const& scan_component : context.current_scan.components) { for (auto const& scan_component : context.current_scan.components) {
if (scan_component.dc_destination_id >= context.dc_tables.size())
return Error::from_string_literal("DC destination ID is greater than number of DC tables");
if (scan_component.ac_destination_id >= context.ac_tables.size())
return Error::from_string_literal("AC destination ID is greater than number of AC tables");
for (u8 vfactor_i = 0; vfactor_i < scan_component.component.vsample_factor; vfactor_i++) { for (u8 vfactor_i = 0; vfactor_i < scan_component.component.vsample_factor; vfactor_i++) {
for (u8 hfactor_i = 0; hfactor_i < scan_component.component.hsample_factor; hfactor_i++) { for (u8 hfactor_i = 0; hfactor_i < scan_component.component.hsample_factor; hfactor_i++) {
// A.2.3 - Interleaved order // A.2.3 - Interleaved order