cbor icon indicating copy to clipboard operation
cbor copied to clipboard

feature: Options to reject floating-point NaN and Inf values on encode and decode

Open benluddy opened this issue 1 year ago • 1 comments

Is your feature request related to a problem? Please describe.

I am implementing CBOR support as a binary alternative to an existing (encoding/json-based) JSON serialization layer. It's essential that all objects entering the system can continue to be JSON-encoded for consumption by existing clients. The JSON serializer refuses to encode or decode floating-point NaN or +/- Inf values, so I want to be able to configure the CBOR serializer to reject them as well.

Describe the solution you'd like

Based on https://www.rfc-editor.org/rfc/rfc8949.html#section-5-3:

CBOR-based protocols MAY specify that they treat arbitrary valid data as unexpected.

I would like to be able to configure both EncMode and DecMode to return errors rather than encode or decode NaN and Inf floating-point values. Specifically:

  1. Extend the existing NaNConvertMode and InfConvertMode with new modes NaNConvertReject and InfConvertReject, respectively, because configuring an accepted conversion is mutually exclusive with rejection. If so configured, marshaling will return UnsupportedValueError when asked to encode a NaN or infinity value. The default mode for both options will remain unchanged.
  2. Add new decode options NaNDec and InfDec with "Allow" (default) and "Reject" modes. In the reject modes, unmarshaling will return UnacceptableDataItemError if a NaN or infinity floating-point value is encountered in the input.

Describe alternatives you've considered

Additional context

benluddy avatar Apr 02 '24 20:04 benluddy

I have a complete implementation in https://github.com/fxamacker/cbor/pull/513 if this proposal seems reasonable and useful.

benluddy avatar Apr 02 '24 20:04 benluddy

Thanks Ben! :tada: Closed by #513.

fxamacker avatar May 19 '24 17:05 fxamacker