From 1c0a2491317d8ea82d3d1f3e0314befe46513282 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Thu, 28 Dec 2023 15:42:51 +0100 Subject: [PATCH] commands: Ignore exceptions during READ while UPDATE If we are reading a file to check if we can skip the write to conserve writes, don't treat exceptions as fatal. The file may well have the access mode in a way that permits us to UPDATE but not to READ. Simply fall-back to unconditional UPDATE in this case. Change-Id: I7bffdaa7596e63c8f0ab04a3cb3ebe12f137d3a8 --- pySim/commands.py | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/pySim/commands.py b/pySim/commands.py index 336cf0cc..d7852513 100644 --- a/pySim/commands.py +++ b/pySim/commands.py @@ -279,9 +279,16 @@ class SimCardCommands: # Save write cycles by reading+comparing before write if conserve: - data_current, sw = self.read_binary(ef, data_length, offset) - if data_current == data: - return None, sw + try: + data_current, sw = self.read_binary(ef, data_length, offset) + if data_current == data: + return None, sw + except Exception: + # cannot read data. This is not a fatal error, as reading is just done to + # conserve the amount of smart card writes. The access conditions of the file + # may well permit us to UPDATE but not permit us to READ. So let's ignore + # any such exception during READ. + pass self.select_path(ef) total_data = '' @@ -363,10 +370,17 @@ class SimCardCommands: # Save write cycles by reading+comparing before write if conserve: - data_current, sw = self.read_record(ef, rec_no) - data_current = data_current[0:rec_length*2] - if data_current == data: - return None, sw + try: + data_current, sw = self.read_record(ef, rec_no) + data_current = data_current[0:rec_length*2] + if data_current == data: + return None, sw + except Exception: + # cannot read data. This is not a fatal error, as reading is just done to + # conserve the amount of smart card writes. The access conditions of the file + # may well permit us to UPDATE but not permit us to READ. So let's ignore + # any such exception during READ. + pass pdu = (self.cla_byte + 'dc%02x04%02x' % (rec_no, rec_length)) + data res = self._tp.send_apdu_checksw(pdu)