fix(netlink): guard string.decode() against AttributeError in NLA array mode - #1481
Open
PhinehasNarh wants to merge 2 commits into
Open
fix(netlink): guard string.decode() against AttributeError in NLA array mode#1481PhinehasNarh wants to merge 2 commits into
PhinehasNarh wants to merge 2 commits into
Conversation
When an NLA attribute is declared as *string (an array of strings), the
base class decode() path sets self.value to a list of child cell objects
rather than raw bytes. nlmsg_atoms.string.decode() then called
self.value = self.value.decode('utf-8')
on that list, raising:
AttributeError: 'list' object has no attribute 'decode'
This produced a WARNING traceback on every NL80211_CMD_TRIGGER_SCAN
event when debug logging was enabled (NL80211_ATTR_SCAN_SSIDS is
declared as *string and is present in every scan trigger message).
The fix wraps the utf-8 decode in an isinstance(self.value, bytes) guard
so it only fires when the value is raw bytes, leaving list values (from
the NLA array decode path) untouched.
Closes svinota#1330.
Owner
|
Thanks, checking! |
flake8 flagged two issues in the test file: - F401: 'pytest' was imported but not used directly (fixtures are auto-discovered, no explicit import needed) - E501: docstring on line 50 was 80 characters, one over the 79-char limit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When an NLA attribute is declared as
*string(an array of strings, e.g.NL80211_ATTR_SCAN_SSIDS), the base classnla_base.decode()setsself.valueto a list of child cell objects rather than raw bytes.nlmsg_atoms.string.decode()then unconditionally called:on that list, raising:
This produced a WARNING traceback logged on every
NL80211_CMD_TRIGGER_SCANevent when debug logging was enabled. The attribute value was still parsed correctly after the first failed call (becausecell.decodedwas already set toTrueby the base class), so the error was non-fatal but noisy.Repro from #1330:
Triggers:
Fix
One-line guard: only call
.decode('utf-8')whenself.valueis actually bytes. When_nla_array=True, the value is already a list of child cells and should be left as-is.Tests
Three unit tests added to
tests/test_unit/test_nlmsg/test_string_nla_array.py:test_string_nla_array_decode_no_attributeerror: verifies decode does not raisetest_string_nla_array_value_is_list: verifies the decoded value is a listtest_string_nla_plain_decode_still_returns_str: verifies plain (non-array) string decode is unchangedRun with:
The tests pass on Linux. They cannot run on Windows due to
fcntlbeing imported transitively throughpyroute2/__init__.py, which is consistent with the rest of thetest_unitsuite.Closes #1330.