Skip to content

[BUG] [v0.0.7] semver_compare incorrectly parses prerelease versions, treating different betas/alphas as equal #13033

Description

@EnthusiasticTech

Project

cortex

Description

In cortex-cli/src/upgrade_cmd.rs, semver_compare implements version comparison by splitting on . and parsing components as u32.

    let parse = |v: &str| -> Vec<u32> {
        v.trim_start_matches('v')
            .split('.')
            .filter_map(|s| s.parse().ok())
            .collect()
    };

This logic incorrectly handles prerelease versions (e.g. 1.0.0-beta.1).
The component 0-beta fails to parse as u32 and is discarded by filter_map.
So 1.0.0-beta.1 becomes [1, 0].
1.0.0 becomes [1, 0, 0].

Comparison logic:

    match a_parts.len().cmp(&b_parts.len()) {
        std::cmp::Ordering::Less => -1, // [1, 0] < [1, 0, 0]
        // ...
    }

Thus 1.0.0-beta.1 is considered OLDER than 1.0.0. This part is accidentally correct.

However, consider 1.0.0-beta.1 vs 1.0.0-alpha.1.
Both parse to [1, 0]. They are considered EQUAL.
The upgrade command will see them as the same version and refuse to upgrade/downgrade between them (unless forced).

Also, 1.0.1 vs 1.0.0-patch. 1.0.0-patch parses as [1, 0]. 1.0.1 parses as [1, 0, 1].
[1, 0] (len 2) < [1, 0, 1] (len 3). So 1.0.0-patch < 1.0.1. Correct.

But 1.0.0-beta.2 vs 1.0.0-beta.1. Both [1, 0]. Equal.
You cannot upgrade from beta 1 to beta 2.

Error Message

N/A (Logic Error)

Debug Logs

N/A

System Information

Bounty Version: 0.1.0
OS: Ubuntu 24.04 LTS
CPU: AMD EPYC-Genoa Processor (8 cores)
RAM: 15 GB

Screenshots

No response

Steps to Reproduce

  1. Have v1.0.0-beta.1 installed.
  2. New version v1.0.0-beta.2 is available.
  3. Run cortex upgrade --channel beta.
  4. Cortex parses both as [1, 0].
  5. "Already on v1.0.0-beta.2" (or similar confusion), or "No upgrade needed".

Expected Behavior

It should use a proper semver parser (like semver crate) to handle prerelease precedence correctly.

Actual Behavior

Prerelease suffixes causes version parts to be dropped, making distinct versions appear identical.

Additional Context

  • File: cortex-cli/src/upgrade_cmd.rs
  • Function: semver_compare
  • Parsing: s.parse().ok() fails on non-numeric strings.

Metadata

Metadata

Assignees

No one assigned

    Labels

    cortexIssues related to CortexLM/cortex repositoryvalidValid issue

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions