Skip to content

Bug: HttpCache.set with data: scheme always throws error #89

@aMOPel

Description

@aMOPel

minimal reproduction

import { HttpCache } from "@deno/cache-dir";

const httpCache = await HttpCache.create({
  root: "<path>",
  vendorRoot:
    "<path>",
  readOnly: false,
});

httpCache.set(
  new URL("data:text/javascript"),
  {},
  Deno.readFileSync(
    "<path>"
  )
);

Error

panicked at rs_lib/src/local.rs:673:67:
index out of bounds: the len is 0 but the index is 0

Problem

local.rs

    let scheme = base_parts.remove(0);
    base_parts[0] = Cow::Owned(format!("{}_{}", scheme, base_parts[0]));

https://github.com/denoland/deno_cache_dir/blob/main/rs_lib/src/local.rs#L673

common.rs

pub fn base_url_to_filename_parts<'a>(
  url: &'a Url,
  port_separator: &str,
) -> Option<Vec<Cow<'a, str>>> {

  match scheme {
    "data" | "blob" => {
      out.push(Cow::Borrowed(scheme));
    }
  };

https://github.com/denoland/deno_cache_dir/blob/main/rs_lib/src/common.rs#L34

Because of the data: scheme, there is only one Vec.push() happening, so the vector will always have length 1.
Vec.remove mutates the vector from length 1 to length 0 and then the vector is accessed at index 0.

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