Skip to content
This repository was archived by the owner on Apr 5, 2024. It is now read-only.
This repository was archived by the owner on Apr 5, 2024. It is now read-only.

Code sample: nalgebra's swap_rows #50

@HadrienG2

Description

@HadrienG2

The nalgebra linear algebra library has a swap_rows method which allows someone to swap two rows of a matrix. Unfortunately, I'm currently encountering an UB-smelling issue which causes data corruption in some circumstances and seems closely related to that method.

The implementation of swap_rows is a loop that iterates over the container cells associated with the source and destination rows of the matrix, then creates two &mut from pairs of cells, and calls mem::swap on the pairs. One of the things which @sebcrozet noticed while experimenting around is that replacing this mem::swap with a ptr::swap seems to make the UB go away. The issue is highly sensitive to many factors, though, so we can't tell for sure if the bug is really gone with that.

One suspicion of mine is that the two &mut which swap_rows create on the same array slice might be consider by either rustc or LLVM to alias, even if from our perspective they don't (as they point to different scalars), which would trigger UB. What is your opinion on this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions