core icon indicating copy to clipboard operation
core copied to clipboard

`Char::from_int` is unsafe

Open Yoorkin opened this issue 1 year ago • 3 comments

This code will crash:

fn init {
  let a = from_int(-1)
  println(a.to_string())
}

The Char::from_int is a primitive %char_from_int, which is an unsafe %identity.

pub fn Char::from_int(val : Int) -> Char = "%char_from_int"

We should verify the input similar to UChar.of_int in OCaml.

Yoorkin avatar Jul 24 '24 08:07 Yoorkin

UChar.of_int will Raises Invalid_argument if i does not satisfy Uchar.is_valid.

is_valid n is true iff n is a Unicode scalar value (i.e. in the ranges 0x0000...0xD7FF or 0xE000...0x10FFFF).

So should it also raise exception?

qazxcdswe123 avatar Jul 26 '24 09:07 qazxcdswe123

#338

Yoorkin avatar Oct 11 '24 09:10 Yoorkin

So should it also raise exception?

Like the APIs List::head/List::unsafe_head and Queue::peek/Queue::unsafe_peek, returning Option[Char] and providing an unchecked version is sufficient for me.

Yoorkin avatar Oct 11 '24 10:10 Yoorkin