Repository: SerenityOS/serenity
File: Userland/Libraries/LibCoredump/Reader.cpp (C++)

Code before:
        m_current = bit_cast<const ELF::Core::NotesEntry*>(current + 1);
        break;
    }
    case ELF::Core::NotesEntryHeader::Type::MemoryRegionInfo: {
        auto const* current = bit_cast<const ELF::Core::MemoryRegionInfo*>(m_current);
        m_current = bit_cast<const ELF::Core::NotesEntry*>(current->region_name + strlen(current->region_name) + 1);
        break;
    }
    case ELF::Core::NotesEntryHeader::Type::Metadata: {
        auto const* current = bit_cast<const ELF::Core::Metadata*>(m_current);
        m_current = bit_cast<const ELF::Core::NotesEntry*>(current->json_data + strlen(current->json_data) + 1);
        break;
    }
    default:
        VERIFY_NOT_REACHED();
    }
}

bool Reader::NotesEntryIterator::at_end() const
{
    return type() == ELF::Core::NotesEntryHeader::Type::Null;
}

ReadonlyBytes Reader::get_serenity_note_section() const
{
    auto raw_note_section = bit_cast<u8 const*>(m_coredump_image.program_header(m_notes_segment_index).raw_data());
    auto note_section_bytes = ReadonlyBytes { raw_note_section, m_coredump_image.program_header(m_notes_segment_index).size_in_memory() };
    FixedMemoryStream section_stream { note_section_bytes };

    auto note = section_stream.read_value<Elf_Note>().release_value_but_fixme_should_propagate_errors();
    auto name = section_stream.read_in_place<u8 const>(note.namesz);

    VERIFY(note.type == 0);
    VERIFY(name.release_value_but_fixme_should_propagate_errors() == "SerenityOS\0"sv.bytes());

    if (auto pad_size = note.namesz % 4; pad_size != 0)
        section_stream.discard(4 - pad_size).release_value_but_fixme_should_propagate_errors();

    return section_stream.read_in_place<u8 const>(note.descsz).release_value_but_fixme_should_propagate_errors();
}

Optional<FlatPtr> Reader::peek_memory(FlatPtr address) const
{
    auto region = region_containing(address);
    if (!region.has_value())
        return {};

    FlatPtr offset_in_region = address - region->region_start;
    auto* region_data = bit_cast<u8 const*>(image().program_header(region->program_header_index).raw_data());
    FlatPtr value { 0 };
    ByteReader::load(region_data + offset_in_region, value);

Review comment (refactor):
nit: I'd prefer `reinterpret_cast` over `bit_cast` for pointer -> pointer casts. `bit_cast` isn't any safer for this and allows you to break const correctness.
But do you even need any cast here? You are directly passing it to the `ReadonlyBytes` constructor.

I'd also maybe extract `m_coredump_image.program_header(m_notes_segment_index)` into a variable.

Code after:
        m_current = bit_cast<const ELF::Core::NotesEntry*>(current + 1);
        break;
    }
    case ELF::Core::NotesEntryHeader::Type::MemoryRegionInfo: {
        auto const* current = bit_cast<const ELF::Core::MemoryRegionInfo*>(m_current);
        m_current = bit_cast<const ELF::Core::NotesEntry*>(current->region_name + strlen(current->region_name) + 1);
        break;
    }
    case ELF::Core::NotesEntryHeader::Type::Metadata: {
        auto const* current = bit_cast<const ELF::Core::Metadata*>(m_current);
        m_current = bit_cast<const ELF::Core::NotesEntry*>(current->json_data + strlen(current->json_data) + 1);
        break;
    }
    default:
        VERIFY_NOT_REACHED();
    }
}

bool Reader::NotesEntryIterator::at_end() const
{
    return type() == ELF::Core::NotesEntryHeader::Type::Null;
}

ReadonlyBytes Reader::get_serenity_note_section() const
{
    auto const& program_header = m_coredump_image.program_header(m_notes_segment_index);
    auto note_section_bytes = ReadonlyBytes { program_header.raw_data(), program_header.size_in_memory() };
    FixedMemoryStream section_stream { note_section_bytes };

    auto note = section_stream.read_value<Elf_Note>().release_value_but_fixme_should_propagate_errors();
    auto name = section_stream.read_in_place<u8 const>(note.namesz);

    VERIFY(note.type == 0);
    VERIFY(name.release_value_but_fixme_should_propagate_errors() == "SerenityOS\0"sv.bytes());

    section_stream.discard(mod(-note.namesz, 4)).release_value_but_fixme_should_propagate_errors();

    return section_stream.read_in_place<u8 const>(note.descsz).release_value_but_fixme_should_propagate_errors();
}

Optional<FlatPtr> Reader::peek_memory(FlatPtr address) const
{
    auto region = region_containing(address);
    if (!region.has_value())
        return {};

    FlatPtr offset_in_region = address - region->region_start;
    auto* region_data = bit_cast<u8 const*>(image().program_header(region->program_header_index).raw_data());
    FlatPtr value { 0 };
    ByteReader::load(region_data + offset_in_region, value);
    return value;