> Nim: 
> <https://github.com/jrfondren/chrestomathy/blob/master/pwcheck/nim/src/pwcheck.nim#L18>
>  \-- crypt() is useful as-is

Let's see...
    
    
    proc crypt(phrase, salt: cstring): cstring  {.cdecl, importc, dynlib: 
"libcrypt.so".}
    
    
    Run

It's not useful without a `cast`, which we've already established is an unsafe 
operator:
    
    
    proc crypt(phrase, salt: cstring): cstring  {.cdecl, importc, dynlib: 
"libcrypt.so".}
    echo "encrypted = " & crypt("x","y")
    
    --> NimRunner.nim(2, 21) Error: type mismatch: got <string, cstring>
    
    
    Run

It's not safe, either:
    
    
    proc crypt(phrase, salt: cstring): cstring  {.cdecl, importc, dynlib: 
"libcrypt.so".}
    echo crypt(nil, nil)
    
    --> SIGSEGV: Illegal storage access. (Attempt to read from nil?)
    
    
    
    Run

I've wrapped a few hundred functions by now, and I'd say most of them are 
absolutely _not_ safe, mostly because they (a) blow up when passed `nil`, or 
(b) require manual freeing of objects, which means use-after-free bugs are 
possible.

Nim's C FFI is very nice and convenient, but it does not magically make C APIs 
safe. And it doesn't magically make them idiomatic Nim, either; a really 
well-designed C API can come close, but most are far from that level.

Magical thinking isn't any better than a cargo cult.

Reply via email to