named-data / python-ndn

An NDN client library with AsyncIO support in Python 3
https://python-ndn.readthedocs.io/en/latest
Apache License 2.0
24 stars 17 forks source link

parse_data return value may violate typing #77

Closed yoursunny closed 6 months ago

yoursunny commented 6 months ago

parse_data function is declared as returning Data type: https://github.com/named-data/python-ndn/blob/7b9ab6ed23d1260f6592d4529fc15c8121456a64/src/ndn/encoding/ndn_format_0_3.py#L487 The Data type is declared as a tuple, where the second element is MetaInfo: https://github.com/named-data/python-ndn/blob/7b9ab6ed23d1260f6592d4529fc15c8121456a64/src/ndn/encoding/ndn_format_0_3.py#L369 The MetaInfo type is a class that cannot be None: https://github.com/named-data/python-ndn/blob/7b9ab6ed23d1260f6592d4529fc15c8121456a64/src/ndn/encoding/ndn_format_0_3.py#L229

However, given a Data packet encoded without MetaInfo field, the parse_data function may return a tuple whose second element is None, violating the declared typing. This can be demonstrated with the snippet below:

from ndn.encoding import parse_data

wire = b'\x06\x0f\x07\x06\x08\x01\x41\x08\x01\x31\x16\x03\x1b\x01\xc8\x17\x00'
name, meta_info, content, sig_ptrs = parse_data(wire, with_tl=True)
print(name)
print(meta_info)
print(content)
print(sig_ptrs)

Console output:

[<memory at 0x7fbad61d16c0>, <memory at 0x7fbad61d1e40>]
None
None
SignaturePtrs(signature_info=SignatureInfo(signature_type=200, key_locator=None, signature_nonce=None, signature_time=None, signature_seq_num=None), signature_covered_part=[<memory at 0x7fbad7325a80>], signature_value_buf=<memory at 0x7fbad61d1f00>, digest_covered_part=[], digest_value_buf=None)

This issue exists in latest release v0.4.1 and the current master branch.