input-output-hk / input-output-hk/cuddle

genControl .lt crashes when controller literal is 0

Open
#168 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Haskell
Stars
7
Forks
5
PR merge metrics
No merged PRs in 30d

Description

## Description

`genControl` crashes when the `.lt` operator is used with a controller literal of 0, because `choose` receives an invalid range.

## Location

`src/Codec/CBOR/Cuddle/CBOR/Gen.hs`, `genControl` function

## Details

```haskell
(CtlOp.Lt, CTree.Literal (Value (VUInt n) _)) -> case target of
CTree.Postlude PTUInt -> S . TInteger <$> choose (0, fromIntegral n - 1)
```

When `n == 0`, `choose (0, fromIntegral n - 1)` becomes `choose (0, -1)`, which is an invalid range.

## Suggested fix

Handle `n == 0` explicitly with a clear error message about the empty range (there are no unsigned integers less than 0).

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.