Spot the bug in the memory mapping helper

from Virtual memory
C++23 intermediate 7 min 3 issues to find

Find the correctness and lifetime bugs in this generated mapping helper.

C++
Record* copy_records(const Record* input, std::size_t count) {
    const std::size_t bytes = count * sizeof(Record);
    void* region = mmap(nullptr, bytes, PROT_READ | PROT_WRITE,
                        MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    std::memcpy(region, input, bytes);
    munmap(region, bytes);
    return static_cast<Record*>(region);
}
Open in playground
Report an error