# GHSA-c2hm-mjxv-89r4: Multiple soundness issues

> The lexical Rust library contains soundness issues allowing memory corruption and potential code execution via invalid bit patterns or out-of-bounds iterator access.

- **Severity:** Not scored
- **Exploited in the wild:** Not known
- **Login needed:** Not needed
- **Attackable:** Not stated
- **Victim action needed:** Not needed
- **Fix available:** Yes
- **Published:** 3 Sep 2023
- **Also known as:** RUSTSEC-2023-0055

## What is GHSA-c2hm-mjxv-89r4?

GHSA-c2hm-mjxv-89r4 is a memory corruption vulnerability in lexical, published on 3 Sep 2023. 

### Official description

`lexical` contains multiple soundness issues:

 1. [Bytes::read() allows creating instances of types with invalid bit patterns](https://github.com/Alexhuszagh/rust-lexical/issues/102)
 1. [BytesIter::read() advances iterators out of bounds](https://github.com/Alexhuszagh/rust-lexical/issues/101)
 1. [The `BytesIter` trait has safety invariants but is public and not marked `unsafe`](https://github.com/Alexhuszagh/rust-lexical/issues/104)
 1. [`write_float()` calls `MaybeUninit::assume_init()` on uninitialized data, which is is not allowed by the Rust abstract machine](https://github.com/Alexhuszagh/rust-lexical/issues/95)
 1. [`radix()` calls `MaybeUninit::assume_init()` on uninitialized data, which is is not allowed by the Rust abstract machine](https://github.com/Alexhuszagh/rust-lexical/issues/126)

The crate also has some correctness issues.

## Alternatives

For quickly parsing floating-point numbers third-party crates are no longer needed. A fast float parsing algorithm by the author of `lexical` has been [merged](https://github.com/rust-lang/rust/pull/86761) into libcore.

For quickly parsing integers, consider `atoi` and `btoi` crates (100% safe code). `atoi_radix10` provides even faster parsing, but only with `-C target-cpu=native`, and at the cost of some `unsafe`.

For formatting integers in a `#[no_std]` context consider the [`numtoa`](https://crates.io/crates/numtoa) crate.

For working with big numbers consider `num-bigint` and `num-traits`.

## Which versions are affected by GHSA-c2hm-mjxv-89r4?

3 open source packages ship an affected version, including lexical.


| Package | Ecosystem | Affected | Fixed in |
| --- | --- | --- | --- |
| `lexical` | crates.io | All versions | 7.0.0 |
| `lexical` | crates.io | All versions | 7.0.0 |
| `lexical` | crates.io | From 0.0.0-0 | 7.0.0 |

## How do I fix GHSA-c2hm-mjxv-89r4?

Upgrade lexical to 7.0.0 or later.

## Is GHSA-c2hm-mjxv-89r4 being exploited?

There is no known exploitation of GHSA-c2hm-mjxv-89r4 in the wild and no public exploit code.

## Fix prompt for AI coding agents

```text
Check this codebase for GHSA-c2hm-mjxv-89r4 and fix it if it is affected.

What it is: GHSA-c2hm-mjxv-89r4 is a memory corruption vulnerability in lexical, published on 3 Sep 2023. 
Exploitation: There is no known exploitation of GHSA-c2hm-mjxv-89r4 in the wild and no public exploit code.

Affected packages and fixed versions:
- lexical (crates.io): affected all versions; fixed in 7.0.0
- lexical (crates.io): affected all versions; fixed in 7.0.0
- lexical (crates.io): affected from 0.0.0-0; fixed in 7.0.0

Task:
1. Search the dependency manifests and lockfiles (for example package.json, package-lock.json, requirements.txt, poetry.lock, uv.lock, pom.xml, build.gradle, go.mod, Cargo.lock, Gemfile.lock) and any Dockerfiles for the affected packages, including transitive dependencies.
2. If an affected version is in use, upgrade it to the fixed version for its release line with the project's package manager, so the lockfile is regenerated. Prefer the smallest upgrade that fixes it.
3. If there is no fix, or the upgrade would break the build, do not guess: explain the risk and the safest workaround instead.
4. Run the tests and summarise what changed and why.
5. If nothing in the project is affected, say so and change nothing.

Reference: https://insights.sbomify.com/GHSA-c2hm-mjxv-89r4/ (Markdown: https://insights.sbomify.com/GHSA-c2hm-mjxv-89r4/index.md)
```

## Sources

- OSV: https://osv.dev/vulnerability/GHSA-c2hm-mjxv-89r4

---
From sbomify Insights: https://insights.sbomify.com/GHSA-c2hm-mjxv-89r4/
Summaries are generated and may contain mistakes; the official description above is the record.
