Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed TLV decoder issue #22948

Merged
merged 4 commits into from
Sep 30, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fixed TLV decoder issue
Fixed issue of the TLV decoder:

When giving tlv_bytes = b"\0256\001\0255\001&\000\031\346x\2077\001$\002\001$\003\006$\004\000\030(\002\030\030\030\030", the following errors occur:

Traceback (most recent call last):
  File "/home/chingtzuchen/exp/tlv2.py", line 743, in <module>
    out = reader.get()
  File "/home/chingtzuchen/exp/tlv2.py", line 459, in get
    self._get(self._tlv, self._decodings, out)
  File "/home/chingtzuchen/exp/tlv2.py", line 674, in _get
    self._decodeVal(tlv, decoding)
  File "/home/chingtzuchen/exp/tlv2.py", line 574, in _decodeVal
    self._get(tlv, decoding["Structure"], decoding["value"])
  File "/home/chingtzuchen/exp/tlv2.py", line 674, in _get
    self._decodeVal(tlv, decoding)
  File "/home/chingtzuchen/exp/tlv2.py", line 578, in _decodeVal
    self._get(tlv, decoding["Array"], decoding["value"])
  File "/home/chingtzuchen/exp/tlv2.py", line 674, in _get
    self._decodeVal(tlv, decoding)
  File "/home/chingtzuchen/exp/tlv2.py", line 574, in _decodeVal
    self._get(tlv, decoding["Structure"], decoding["value"])
  File "/home/chingtzuchen/exp/tlv2.py", line 674, in _get
    self._decodeVal(tlv, decoding)
  File "/home/chingtzuchen/exp/tlv2.py", line 574, in _decodeVal
    self._get(tlv, decoding["Structure"], decoding["value"])
  File "/home/chingtzuchen/exp/tlv2.py", line 674, in _get
    self._decodeVal(tlv, decoding)
  File "/home/chingtzuchen/exp/tlv2.py", line 582, in _decodeVal
    self._get(tlv, decoding["Path"], decoding["value"])
  File "/home/chingtzuchen/exp/tlv2.py", line 685, in _get
    out[decoding["tag"]] = decoding["value"]
IndexError: list assignment index out of range

Added condition and made it more clean to fix the issue, after this fix the bytes can be decoded:

{'Any': {1: [{1: {'Any': 2272847385, 1: [1, 6, 0], 2: False}}]}}
  • Loading branch information
falay committed Sep 30, 2022
commit 0a06ea3886eff32dde734a5dfbbfa773cfde09cb
10 changes: 4 additions & 6 deletions src/controller/python/chip/tlv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,13 +680,11 @@ def _get(self, tlv, decodings, out):
if "profileTag" in list(decoding.keys()):
out[decoding["profileTag"]] = decoding["value"]
elif "tag" in list(decoding.keys()):
if decoding["tag"] is not None:
out[decoding["tag"]] = decoding["value"]
if isinstance(out, Mapping):
tag = decoding["tag"] or "Any"
out[tag] = decoding["value"]
else:
if isinstance(out, Mapping):
out["Any"] = decoding["value"]
elif isinstance(out, Sequence):
out.append(decoding["value"])
out.append(decoding["value"])
else:
raise ValueError("Attempt to decode unsupported TLV tag")

Expand Down