Repository: SerenityOS/serenity
File: Userland/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp (C++)

Code before:
    context.mblock_meta.vcount = (context.frame.height + 7) / 8;
    context.mblock_meta.hpadded_count = context.mblock_meta.hcount;
    context.mblock_meta.vpadded_count = context.mblock_meta.vcount;
    context.mblock_meta.total = context.mblock_meta.hcount * context.mblock_meta.vcount;
}

static ErrorOr<void> ensure_standard_precision(StartOfFrame const& frame)
{
    // B.2.2 - Frame header syntax
    // Table B.2 - Frame header parameter sizes and values

    if (frame.type == StartOfFrame::FrameType::Baseline_DCT && frame.precision == 8)
        return {};

    if (frame.type == StartOfFrame::FrameType::Extended_Sequential_DCT && frame.precision == 8)
        return {};

    if (frame.type == StartOfFrame::FrameType::Progressive_DCT && frame.precision == 8)
        return {};

    dbgln_if(JPEG_DEBUG, "Unsupported precision: {}, for SOF type: {}!", frame.precision, static_cast<int>(frame.type));
    return Error::from_string_literal("Unsupported SOF precision.");
}

static ErrorOr<void> read_start_of_frame(Stream& stream, JPEGLoadingContext& context)
{
    if (context.state == JPEGLoadingContext::FrameDecoded) {
        dbgln_if(JPEG_DEBUG, "SOF repeated!");
        return Error::from_string_literal("SOF repeated");
    }

    [[maybe_unused]] u16 const bytes_to_read = TRY(read_effective_chunk_size(stream));

    context.frame.precision = TRY(stream.read_value<u8>());

    TRY(ensure_standard_precision(context.frame));

    context.frame.height = TRY(stream.read_value<BigEndian<u16>>());
    context.frame.width = TRY(stream.read_value<BigEndian<u16>>());
    if (!context.frame.width || !context.frame.height) {
        dbgln_if(JPEG_DEBUG, "ERROR! Image height: {}, Image width: {}!", context.frame.height, context.frame.width);
        return Error::from_string_literal("Image frame height of width null");
    }

    if (context.frame.width > maximum_width_for_decoded_images || context.frame.height > maximum_height_for_decoded_images) {
        dbgln("This JPEG is too large for comfort: {}x{}", context.frame.width, context.frame.height);
        return Error::from_string_literal("JPEG too large for comfort");
    }

    set_macroblock_metadata(context);


Review comment (suggestion):
Is `if (frame.precision == 8) return {};` not always correct? (Instead of the first 3 explicit case.)

Code after:
    }
    return false;
}

static inline void set_macroblock_metadata(JPEGLoadingContext& context)
{
    context.mblock_meta.hcount = (context.frame.width + 7) / 8;
    context.mblock_meta.vcount = (context.frame.height + 7) / 8;
    context.mblock_meta.hpadded_count = context.mblock_meta.hcount;
    context.mblock_meta.vpadded_count = context.mblock_meta.vcount;
    context.mblock_meta.total = context.mblock_meta.hcount * context.mblock_meta.vcount;
}

static ErrorOr<void> ensure_standard_precision(StartOfFrame const& frame)
{
    // B.2.2 - Frame header syntax
    // Table B.2 - Frame header parameter sizes and values

    if (frame.precision == 8)
        return {};

    if (frame.type == StartOfFrame::FrameType::Extended_Sequential_DCT && frame.precision == 12)
        return {};

    if (frame.type == StartOfFrame::FrameType::Progressive_DCT && frame.precision == 12)
        return {};

    dbgln_if(JPEG_DEBUG, "Unsupported precision: {}, for SOF type: {}!", frame.precision, static_cast<int>(frame.type));
    return Error::from_string_literal("Unsupported SOF precision.");
}

static ErrorOr<void> read_start_of_frame(Stream& stream, JPEGLoadingContext& context)
{
    if (context.state == JPEGLoadingContext::FrameDecoded) {
        dbgln_if(JPEG_DEBUG, "SOF repeated!");
        return Error::from_string_literal("SOF repeated");
    }

    [[maybe_unused]] u16 const bytes_to_read = TRY(read_effective_chunk_size(stream));

    context.frame.precision = TRY(stream.read_value<u8>());

    TRY(ensure_standard_precision(context.frame));

    context.frame.height = TRY(stream.read_value<BigEndian<u16>>());
    context.frame.width = TRY(stream.read_value<BigEndian<u16>>());
    if (!context.frame.width || !context.frame.height) {
        dbgln_if(JPEG_DEBUG, "ERROR! Image height: {}, Image width: {}!", context.frame.height, context.frame.width);
        return Error::from_string_literal("Image frame height of width null");
    }