From f592cfa7fe7f866969837b641a893801b3e8bbce Mon Sep 17 00:00:00 2001 From: Alexandru M Stan Date: Fri, 22 Jul 2022 18:28:16 +0000 Subject: [PATCH] pw_rpc: Fix if IDs are negative Service and method IDs are unsigned 32-bit integers. It seems that these occasionally appear as negative values in the Python client. We can't just modify .service_id and .method_id since the underlying storage is still erroneously a signed type. Let's just make it unsigned just before we use it! See also, CL that yells about this problem: Ia1bf3013940d995b88e65b569634fc0f7e66abbe Bug: b/239712573, b/239224232 Change-Id: I5b78aa0804bdb1f1d87f725d85adb0cdfab4d218 Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/102760 Reviewed-by: Wyatt Hepler Commit-Queue: Anthony DiGirolamo --- pw_rpc/py/pw_rpc/client.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/pw_rpc/py/pw_rpc/client.py b/pw_rpc/py/pw_rpc/client.py index 182434d6bb..f2a57e6ca8 100644 --- a/pw_rpc/py/pw_rpc/client.py +++ b/pw_rpc/py/pw_rpc/client.py @@ -548,16 +548,22 @@ def process_packet(self, pw_rpc_raw_packet_data: bytes, *impl_args, def _look_up_service_and_method( self, packet: RpcPacket, channel_client: ChannelClient) -> PendingRpc: + # Protobuf is sometimes silly so the 32 bit python bindings return + # signed values from `fixed32` fields. Let's convert back to unsigned. + # b/239712573 + service_id = packet.service_id & 0xffffffff try: - service = self.services[packet.service_id] + service = self.services[service_id] except KeyError: - raise ValueError(f'Unrecognized service ID {packet.service_id}') + raise ValueError(f'Unrecognized service ID {service_id}') + # See above, also for b/239712573 + method_id = packet.method_id & 0xffffffff try: - method = service.methods[packet.method_id] + method = service.methods[method_id] except KeyError: raise ValueError( - f'No method ID {packet.method_id} in service {service.name}') + f'No method ID {method_id} in service {service.name}') return PendingRpc(channel_client.channel, service, method)