koka-lang / koka

Koka language compiler and interpreter
http://koka-lang.org
Other
3.26k stars 162 forks source link

to-lower on an empty string crashes #287

Closed JaniM closed 2 weeks ago

JaniM commented 2 years ago

Full reproduction:

❯ koka
 _         _ 
| |       | |
| | _ ___ | | _ __ _
| |/ / _ \| |/ / _' |  welcome to the koka interactive compiler
|   ( (_) |   ( (_| |  version 2.3.8, Dec 27 2021, libc x64 (gcc)
|_|\_\___/|_|\_\__,_|  type :? for help, and :q to quit

loading: std/core
loading: std/core/types
loading: std/core/hnd

> to-lower("")
check  : interactive
check  : interactive
linking: interactive
created: /tmp/.koka/v2.3.8/gcc-debug/interactive

failure during program run:

   "/tmp/.koka/v2.3.8/gcc-debug/interactive"
pmahoney commented 2 years ago

Looks like the error is in an optimization in kk_string_to_lower (and to_upper) failing to check if str.bytes is a singleton (i.e. the empty bytes singleton) before the call to kk_datatype_is_unique.

https://github.com/koka-lang/koka/blob/b1670308f88dd1fc6c22cad28385fcb185d5b27d/kklib/src/string.c#L806-L810

kk_datatype_is_unique includes an assertion that I would expect to fail if koka was compiled with KK_DEBUG_FULL. If I'm understanding this correctly, a datatype can be either a ptr or singleton, and "unique" only makes sense for ptr.

https://github.com/koka-lang/koka/blob/b1670308f88dd1fc6c22cad28385fcb185d5b27d/kklib/include/kklib.h#L861-L865

daanx commented 2 weeks ago

Fixed in the latest koka. Thanks!