Skip to content

bug: get_public_pmtiles unsigned integer underflow on zero-byte PMTiles file #374

Description

@evan-zhang11

Description

The get_public_pmtiles endpoint in backend/src/public.rs performs file_size - 1 using unsigned (u64) arithmetic without checking for zero. If a zero-byte .pmtiles file is uploaded and published, requesting it (or sending a Range header) causes unsigned integer underflow.

Location

backend/src/public.rs, function get_public_pmtiles:

let end: u64 = if parts[1].is_empty() {
    file_size - 1  // underflow when file_size == 0
} else {
    parts[1].parse()...
};

let actual_end = end.min(file_size - 1);  // same underflow

Impact

  • Debug builds: Panic (server crash)
  • Release builds: wraps to u64::MAX, leading to incorrect Range parsing and potential huge memory allocation

A zero-byte .pmtiles file passes upload validation (the pmtiles arm returns Ok(()) without any validation), and can then be published.

Suggested Fix

Add a guard for empty files before Range parsing:

if file_size == 0 {
    return Ok((
        StatusCode::OK,
        [
            (header::CONTENT_TYPE, "application/octet-stream"),
            (header::CONTENT_LENGTH, "0"),
            (header::ACCEPT_RANGES, "bytes"),
        ],
        Vec::<u8>::new(),
    ).into_response());
}

Also consider adding PMTiles header validation in upload.rs.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions