Package: python3-pyscard
Version: 2.0.10-1+b1
Severity: minor
Tags: patch upstream

Upstream report: https://github.com/LudovicRousseau/pyscard/pull/173

pyscard 2.0.8 has a regression in which it introduced code that
references undefined variables in case no card is connected.  So rather
than the expected CardConnectionException, there's a nested
UnboundLocalError exception in such a [not very unusual] situation where
the user has not inserted a card into the reader.

Traceback (most recent call last):
  File "/crypt/space/home/laforge/projects/git/pysim/./contrib/card2server.py", 
line 106, in <module>
    'atr': tp.get_atr(),
           ^^^^^^^^^^^^
  File 
"/crypt/space/home/laforge/projects/git/pysim/contrib/pySim/transport/pcsc.py", 
line 95, in get_atr
    return self._con.getATR()
           ^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/smartcard/CardConnectionDecorator.py", 
line 66, in getATR
    return self.component.getATR()
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/smartcard/CardConnectionDecorator.py", 
line 66, in getATR
    return self.component.getATR()
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/smartcard/pcsc/PCSCCardConnection.py", 
line 215, in getATR
    hresult=hresult)
            ^^^^^^^

-- System Information:
Debian Release: trixie/sid
  APT prefers unstable-debug
  APT policy: (500, 'unstable-debug'), (500, 'unstable')
Architecture: amd64 (x86_64)
Foreign Architectures: i386

Kernel: Linux 6.10.7-amd64 (SMP w/16 CPU threads; PREEMPT)
Kernel taint flags: TAINT_OOT_MODULE, TAINT_UNSIGNED_MODULE
Locale: LANG=en_US.UTF-8, LC_CTYPE=de_DE.UTF-8 (charmap=UTF-8), LANGUAGE not set
Shell: /bin/sh linked to /usr/bin/dash
Init: systemd (via /run/systemd/system)
LSM: AppArmor: enabled

Versions of packages python3-pyscard depends on:
ii  libc6    2.40-2
ii  python3  3.12.5-1

Versions of packages python3-pyscard recommends:
ii  libpcsclite1  2.3.0-1

Versions of packages python3-pyscard suggests:
ii  python3-wxgtk4.0  4.2.1+dfsg-4+b1

-- no debconf information
>From 21e411ac11d09895a7bf8f0bca2e842e2f37b2dd Mon Sep 17 00:00:00 2001
From: Harald Welte <lafo...@osmocom.org>
Date: Sun, 8 Sep 2024 08:53:44 +0200
Subject: [PATCH] Fix use of undefined variable 'hresult' in exceptions

The following commit introrudced the inclusion of hresult in
exceptions that are raised:

commit 395cdc46a0fda86af79523a2d18bef06b96a234b
Author: Ludovic Rousseau <ludovic.rouss...@free.fr>
Date:   Sun Apr 23 18:27:24 2023 +0200

However, it also introduces hresult into 'raise' statements where
no hresult exists [yet].  THis in turn causes double exceptions like
this:

Traceback (most recent call last):
  File "/crypt/space/home/laforge/projects/git/pysim/./contrib/card2server.py", 
line 106, in <module>
    'atr': tp.get_atr(),
           ^^^^^^^^^^^^
  File 
"/crypt/space/home/laforge/projects/git/pysim/contrib/pySim/transport/pcsc.py", 
line 95, in get_atr
    return self._con.getATR()
           ^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/smartcard/CardConnectionDecorator.py", 
line 66, in getATR
    return self.component.getATR()
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/smartcard/CardConnectionDecorator.py", 
line 66, in getATR
    return self.component.getATR()
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/smartcard/pcsc/PCSCCardConnection.py", 
line 215, in getATR
    hresult=hresult)
            ^^^^^^^
UnboundLocalError: cannot access local variable 'hresult' where it is not 
associated with a value
---
 smartcard/pcsc/PCSCCardConnection.py | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/smartcard/pcsc/PCSCCardConnection.py 
b/smartcard/pcsc/PCSCCardConnection.py
index 9975eea..929b0c5 100644
--- a/smartcard/pcsc/PCSCCardConnection.py
+++ b/smartcard/pcsc/PCSCCardConnection.py
@@ -150,8 +150,7 @@ class PCSCCardConnection(CardConnection):
         (C{smartcard.scard.SCARD_RESET_CARD})"""
         CardConnection.reconnect(self, protocol)
         if self.hcard is None:
-            raise CardConnectionException('Card not connected',
-                                          hresult=hresult)
+            raise CardConnectionException('Card not connected')
 
         pcscprotocol = translateprotocolmask(protocol)
         if 0 == pcscprotocol:
@@ -211,8 +210,7 @@ class PCSCCardConnection(CardConnection):
         """Return card ATR"""
         CardConnection.getATR(self)
         if self.hcard is None:
-            raise CardConnectionException('Card not connected',
-                                          hresult=hresult)
+            raise CardConnectionException('Card not connected')
         hresult, reader, state, protocol, atr = SCardStatus(self.hcard)
         if hresult != 0:
             raise CardConnectionException(
-- 
2.45.2

Reply via email to