haskell / c2hs

c2hs is a pre-processor for Haskell FFI bindings to C libraries
http://hackage.haskell.org/package/c2hs
Other
198 stars 50 forks source link

c2hs crashes on invalid input #180

Closed kolmodin closed 7 years ago

kolmodin commented 7 years ago

I had written this c2hs code;

-- | Validate the key of a metadata pair.
{#fun pure grpc_header_key_is_legal as isKeyValid
  { 'useAsCStringLen'* `B.ByteString'&} -> `Bool' #}

But the C code had been updated from char*, size_t to grpc_slice;

GRPCAPI int grpc_header_key_is_legal(grpc_slice slice);

Thus c2hs expects two arguments in the C function, but there was only one. Naturally c2hs needs to fail generating the bindings, but the failure should be informative.

Currently it crashes at runtime with an error message which doesn't tell you where in your code you have a problem;

c2hs: src/C2HS/Gen/Bind.hs:(1426,5)-(1444,66): Non-exhaustive patterns in function addDft

Turning the incomplete patterns warnings in c2hs back on;

src/C2HS/Gen/Bind.hs:1426:5: warning: [-Wincomplete-patterns]
    Pattern match(es) are non-exhaustive
    In an equation for ‘addDft’:
        Patterns not matched:
            (CHSPlusParm _:_) []
            (CHSParm _ _ True _ _ _ _:_) [_]

I also filed a bug to fix the incomplete patterns; https://github.com/haskell/c2hs/issues/179.