# RUSTSEC-2026-0197: Matrix{2,3,4}::swap\_columns can trigger undefined behavior for identical indices

- Database: RustSec
- Published: 2026-03-11
- Modified: 2026-07-03

## Affected versions

- cgmath (crates.io): all versions

## Description

The `Matrix2::swap_columns`, `Matrix3::swap_columns`, and `Matrix4::swap_columns`
implementations call `ptr::swap(&mut self[a], &mut self[b])`.

When `a == b`, these safe APIs create two mutable references to the same matrix
column and pass them to `ptr::swap`. This violates Rust's aliasing rules and can
trigger undefined behavior. The issue can be reproduced from safe Rust by calling
`swap_columns` with identical column indices, for example `m.swap_columns(0, 0)`.

A minimal fix is to return early when the two column indices are equal before
calling `ptr::swap`.

## References

- https://crates.io/crates/cgmath
- https://rustsec.org/advisories/RUSTSEC-2026-0197.html
- https://github.com/rustgd/cgmath/issues/565

Page: https://cve.now/RUSTSEC-2026-0197

Wrong or missing data? Send feedback: POST https://cve.now/api/v1/feedback (see https://cve.now/agents#feedback).
