summaryrefslogtreecommitdiff
path: root/Userland/Libraries
diff options
context:
space:
mode:
authorLucas CHOLLET <lucas.chollet@free.fr>2023-02-26 00:24:51 -0500
committerLinus Groh <mail@linusgroh.de>2023-03-04 23:39:41 +0000
commit342c94c4d7edac11c1fece1c4d636bb8e71304ad (patch)
tree59ad28a8808904a883c7cbdc466c9a378e40d3dc /Userland/Libraries
parent18064fdb928bfd121ac6f5b9608c2bb1582bdfb7 (diff)
downloadserenity-342c94c4d7edac11c1fece1c4d636bb8e71304ad.zip
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.
Diffstat (limited to 'Userland/Libraries')
-rw-r--r--Userland/Libraries/LibGfx/JPEGLoader.cpp21
1 files changed, 14 insertions, 7 deletions
diff --git a/Userland/Libraries/LibGfx/JPEGLoader.cpp b/Userland/Libraries/LibGfx/JPEGLoader.cpp
index ae5e7e3b83..79e6787c77 100644
--- a/Userland/Libraries/LibGfx/JPEGLoader.cpp
+++ b/Userland/Libraries/LibGfx/JPEGLoader.cpp
@@ -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)
{
- 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;
// 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)
{
- 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& 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)
{
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 hfactor_i = 0; hfactor_i < scan_component.component.hsample_factor; hfactor_i++) {
// A.2.3 - Interleaved order