Skip to content

CBOR does not handle canonical NaN correctly #92080

Closed
@vcsjones

Description

@vcsjones

Description

Since System.Formats.Cbor adheres to RFC 7049, I don't believe NaN is handled correctly.

The RFC states for canonical CBOR:

Also, there are many representations for NaN. If NaN is an allowed value, it must always be represented as 0xf97e00.

CborWriter will encode it as 0xF9FE00.

Similarly, CborReader will not throw for any non-canonically encoded NaN.

Reproduction Steps

To reproduce the CborWriter issue:

using System;
using System.Formats.Cbor;

CborWriter writer = new(CborConformanceMode.Canonical);
writer.WriteSingle(float.NaN);
Console.WriteLine(Convert.ToHexString(writer.Encode())); // Outputs F9FE00

To reproduce the CborReader issue:

byte[] canonical = Convert.FromHexString("F97E00");
byte[] nonCanonical = Convert.FromHexString("F9FE00");
CborReader canonicalReader = new CborReader(canonical, CborConformanceMode.Canonical);
CborReader nonCanonicalReader = new CborReader(nonCanonical, CborConformanceMode.Canonical);
Console.WriteLine(float.IsNaN(canonicalReader.ReadSingle()));
Console.WriteLine(float.IsNaN(nonCanonicalReader.ReadSingle()));

Expected behavior

CborWriter encodes NaN as 0xf97e00.


CborReader should throw when decoding a NaN that is non-canonically encoded.

Actual behavior

CborWriter incorrectly encodes NaN as 0xF9FE00 when in canonical encoding.


CborReader incorrectly permits all encodings of NaN when in canonical encoding.

Regression?

No.

Known Workarounds

No response

Configuration

Host:
Version: 8.0.0-rc.1.23419.4
Architecture: arm64
Commit: 9295993
RID: osx-arm64

Using 7.0.0 of System.Formats.Cbor package.

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions