Skip to content

encoding/cbor does not correctly encode or decode a time.Time with epoch tag #5850

@BigBoyBarney

Description

@BigBoyBarney

Hi!

Context

When encoding time.Time fields with specific values as cbor and then parsing it, a Bad_Tag_Value error is raised.

  • Operating System & Odin Version: Linux - odin version dev-2025-10
  • Please paste odin report output:
    Odin: dev-2025-10
    OS: Debian GNU/Linux forky/sid, Linux 6.16.8-200.fc42.x86_64
    CPU: AMD Ryzen 7 5800X3D 8-Core Processor
    RAM: 32004 MiB
    Backend: LLVM 19.1.7

Expected Behavior

Encoding and decoding a time.Time field always works.

Reproduction:

If the value of the time field is less than 1e11 nanoseconds (empirical), it fails.

Test :: struct {
    time: time.Time `cbor_tag:"epoch"`,
}

main :: proc() {
	// via type declaration
	Error_Implicit: {
		test: Test
		encoded, marshal_err := cbor.marshal_into_bytes(test)
		if marshal_err != nil { fmt.printfln("%#v", "Marshal error") }

		decoded_test: Test
		decode_err := cbor.unmarshal_from_bytes(encoded[:], &decoded_test)
		fmt.printfln("%#v", decode_err) // Bad_Tag_Value
	}

	// via direct initialisation
	Error_Explicit: {
		test := Test{}

		encoded, marshal_err := cbor.marshal_into_bytes(test)
		if marshal_err != nil { fmt.printfln("%#v", "Marshal error") }

		decoded_test: Test
		decode_err := cbor.unmarshal_from_bytes(encoded[:], &decoded_test)
		fmt.printfln("%#v", decode_err) // Bad_Tag_Value
	}

	// via time.from_nanoseconds(0)
	Error_Time_Unix_0: {
		test := Test{time.from_nanoseconds(0)}

		encoded, marshal_err := cbor.marshal_into_bytes(test)
		if marshal_err != nil { fmt.printfln("%#v", "Marshal error") }

		decoded_test: Test
		decode_err := cbor.unmarshal_from_bytes(encoded[:], &decoded_test)
		fmt.printfln("%#v", decode_err) // Bad_Tag_Value
	}

	// via time.from_nanoseconds(1e11)
	Success_Time_Unix_1e11: {
		test := Test{time.from_nanoseconds(1e11)} // 1e10 still throws an error

		encoded, marshal_err := cbor.marshal_into_bytes(test)
		if marshal_err != nil { fmt.printfln("%#v", "Marshal error") }

		decoded_test: Test
		decode_err := cbor.unmarshal_from_bytes(encoded[:], &decoded_test)
		fmt.printfln("%#v", decode_err) // nil => Works as expected
	}

	// via time.now()
	Success_Time_Unix_Now: {
		test := Test{time.now()}

		encoded, marshal_err := cbor.marshal_into_bytes(test)
		if marshal_err != nil { fmt.printfln("%#v", "Marshal error") }

		decoded_test: Test
		decode_err := cbor.unmarshal_from_bytes(encoded[:], &decoded_test)
		fmt.printfln("%#v", decode_err) // nil => Works as expected
	}
}

Cheers!
Barney


Aparte: I love Odin!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions