base icon indicating copy to clipboard operation
base copied to clipboard

Segmentation fault with `String.filter_map`

Open smondet opened this issue 1 year ago • 2 comments

(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 avatar Feb 02 '24 19:02 smondet