Skip to content

Commit

Permalink
fix: reduce debug logging overhead by adding missing checks to datagr…
Browse files Browse the repository at this point in the history
…am_received (#1188)
  • Loading branch information
bdraco committed Jun 18, 2023
1 parent 9ee3019 commit ac5c50a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/zeroconf/_core.py
Expand Up @@ -282,18 +282,20 @@ def datagram_received(
else:
# https://github.com/python/mypy/issues/1178
addr, port, flow, scope = addrs # type: ignore
log.debug('IPv6 scope_id %d associated to the receiving interface', scope)
if debug:
log.debug('IPv6 scope_id %d associated to the receiving interface', scope)
v6_flow_scope = (flow, scope)

if data_len > _MAX_MSG_ABSOLUTE:
# Guard against oversized packets to ensure bad implementations cannot overwhelm
# the system.
log.debug(
"Discarding incoming packet with length %s, which is larger "
"than the absolute maximum size of %s",
data_len,
_MAX_MSG_ABSOLUTE,
)
if debug:
log.debug(
"Discarding incoming packet with length %s, which is larger "
"than the absolute maximum size of %s",
data_len,
_MAX_MSG_ABSOLUTE,
)
return

now = current_time_millis()
Expand Down
16 changes: 16 additions & 0 deletions tests/test_core.py
Expand Up @@ -761,6 +761,22 @@ def test_guard_against_oversized_packets():
is None
)

logging.getLogger('zeroconf').setLevel(logging.INFO)

listener.datagram_received(over_sized_packet, ('::1', const._MDNS_PORT, 1, 1))
assert (
zc.cache.async_get_unique(
r.DNSText(
"packet0.local.",
const._TYPE_TXT,
const._CLASS_IN | const._CLASS_UNIQUE,
500,
b'path=/~paulsm/',
)
)
is None
)

zc.close()


Expand Down

0 comments on commit ac5c50a

Please sign in to comment.