Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segmentation fault with String.filter_map #162

Closed
smondet opened this issue Feb 2, 2024 · 3 comments
Closed

Segmentation fault with String.filter_map #162

smondet opened this issue Feb 2, 2024 · 3 comments
Labels
forwarded-to-js-devs This report has been forwarded to Jane Street's internal review system.

Comments

@smondet
Copy link

smondet commented Feb 2, 2024

(I see a bunch of unsafe_get & co in the implementation so I try here before bothering ocaml/ocaml)

OCaml 5.0.0, Base v0.16.3

Noticed the [@nontail] in the implementation of String.filter_map so I also tried String.filter_mapi which does not segfault but still seems to add a \000 character (???).

Reproduction:

open Base

let sanitize_mapi s =
  "Hello"
  ^ String.filter_mapi s ~f:(fun _ -> function
      | ('a' .. 'z' | 'A' .. 'Z' | '0' .. '9' | '-') as c -> Some c | _ -> None)

let sanitize_map s =
  "Hello"
  ^ String.filter_map s ~f:(function
      | ('a' .. 'z' | 'A' .. 'Z' | '0' .. '9' | '-') as c -> Some c
      | _ -> None)

let () =
  let open Stdlib.Printf in
  let s = " dlkjelkdje -d e E? dei !! " in
  printf "mapi: %S\n%!" (sanitize_mapi s);
  printf "map: %S\n%!" (sanitize_map s);
  ()
 $ dune exec repro/main.exe
mapi: "Hello\000lkjelkdje-deEdei"
Segmentation fault (core dumped)
 $ cat repro/dune
(executable
 (name main)
 (libraries base))

Haven't tried with OCaml 5.1 (yet)

@smondet
Copy link
Author

smondet commented Feb 2, 2024

Confirming: same behavior with OCaml 5.1.0

@smondet
Copy link
Author

smondet commented Feb 2, 2024

The \000 only happens when the first character is filtered out:

let () =
  let open Stdlib.Printf in
  let s = " dlkjelkdje -d e E? dei !! " in
  let s2 = "dlkjelkdje -d e E? dei !! " in
  printf "mapi: %S\n%!" (sanitize_mapi s);
  printf "mapi: %S\n%!" (sanitize_mapi s2);
  printf "map: %S\n%!" (sanitize_map s2);
  printf "map: %S\n%!" (sanitize_map s);
  ()
 $ dune exec repro/main.exe
mapi: "Hello\000lkjelkdje-deEdei"
mapi: "Hellodlkjelkdje-deEdei"
Segmentation fault (core dumped)

@github-iron github-iron added the forwarded-to-js-devs This report has been forwarded to Jane Street's internal review system. label Feb 5, 2024
@dkalinichenko-js
Copy link
Contributor

Hi, thanks for your report! The issue should be fixed in base.v0.17.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
forwarded-to-js-devs This report has been forwarded to Jane Street's internal review system.
Projects
None yet
Development

No branches or pull requests

3 participants