# Buggy code
def check_device_dialog(self):
    match = re.search(
        r"v([0-9])+\.[0-9]+\.[0-9]+", self.dbb_hid.get_serial_number_string()
    )
    if match is None:
        raise Exception("error detecting firmware version")
    major_version = int(match.group(1))
    if major_version < MIN_MAJOR_VERSION:
        raise Exception(
            "Please upgrade to the newest firmware using the BitBox Desktop app: https://shiftcrypto.ch/start"
        )
    # Set password if fresh device
    if self.password is None and not self.dbb_has_password():
        if not self.setupRunning:
            return False  # A fresh device cannot connect to an existing wallet
        msg = (
            _("An uninitialized Digital Bitbox is detected.")
            + " "
            + _("Enter a new password below.")
            + "\n\n"
            + _("REMEMBER THE PASSWORD!")
            + "\n\n"
            + _("You cannot access your coins or a backup without the password.")
            + "\n"
            + _("A backup is saved automatically when generating a new wallet.")
        )
        if self.password_dialog(msg):
            reply = self.hid_send_plain(b'{"password":"' + self.password + b'"}')
        else:
            return False

    # Get password from user if not yet set
    msg = _("Enter your Digital Bitbox password:")
    while self.password is None:
        if not self.password_dialog(msg):
            raise UserCancelled()
        reply = self.hid_send_encrypt(b'{"led":"blink"}')
        if "error" in reply:
            self.password = None
            if reply["error"]["code"] == 109:
                msg = (
                    _("Incorrect password entered.")
                    + "\n\n"
                    + reply["error"]["message"]
                    + "\n\n"
                    + _("Enter your Digital Bitbox password:")
                )
            else:
                # Should never occur
                msg = (
                    _("Unexpected error occurred.")
                    + "\n\n"
                    + reply["error"]["message"]
                    + "\n\n"
                    + _("Enter your Digital Bitbox password:")
                )

    # Initialize device if not yet initialized
    if not self.setupRunning:
        self.isInitialized = True  # Wallet exists. Electrum code later checks if the device matches the wallet
    elif not self.isInitialized:
        reply = self.hid_send_encrypt(b'{"device":"info"}')
        if reply["device"]["id"] != "":
            self.recover_or_erase_dialog()  # Already seeded
        else:
            self.seed_device_dialog()  # Seed if not initialized
        self.mobile_pairing_dialog()
    return self.isInitialized


# Traceback (OSError)
$ ~/Downloads/apps/electrum-4.0.2-x86_64.AppImage --testnet
Warning: Ignoring XDG_SESSION_TYPE=wayland on Gnome. Use QT_QPA_PLATFORM=wayland to run on Wayland anyway.
E | plugin.DeviceMgr | failed to create client for bitbox02 at b'0001:0009:01': Exception('No BitBox02 detected')
E | plugins.coldcard.coldcard.Coldcard_KeyStore |
Traceback (most recent call last):
File "/tmp/.mount_electrcFBIsQ/usr/lib/python3.7/site-packages/electrum/plugins/coldcard/coldcard.py", line 383, in sign_transaction
resp = client.sign_transaction_poll()
File "/tmp/.mount_electrcFBIsQ/usr/lib/python3.7/site-packages/electrum/plugins/coldcard/coldcard.py", line 233, in sign_transaction_poll
return self.dev.send_recv(CCProtocolPacker.get_signed_txn(), timeout=None)
File "/tmp/.mount_electrcFBIsQ/usr/lib/python3.7/site-packages/ckcc/client.py", line 139, in send_recv
buf = self.dev.read(64, timeout_ms=(timeout or 0))
File "hid.pyx", line 123, in hid.device.read
OSError: read error
double free or corruption (fasttop)
Aborted

# Fixed code
def check_device_dialog(self):
    match = re.search(
        r"v([0-9])+\.[0-9]+\.[0-9]+",
        run_in_hwd_thread(self.dbb_hid.get_serial_number_string),
    )
    if match is None:
        raise Exception("error detecting firmware version")
    major_version = int(match.group(1))
    if major_version < MIN_MAJOR_VERSION:
        raise Exception(
            "Please upgrade to the newest firmware using the BitBox Desktop app: https://shiftcrypto.ch/start"
        )
    # Set password if fresh device
    if self.password is None and not self.dbb_has_password():
        if not self.setupRunning:
            return False  # A fresh device cannot connect to an existing wallet
        msg = (
            _("An uninitialized Digital Bitbox is detected.")
            + " "
            + _("Enter a new password below.")
            + "\n\n"
            + _("REMEMBER THE PASSWORD!")
            + "\n\n"
            + _("You cannot access your coins or a backup without the password.")
            + "\n"
            + _("A backup is saved automatically when generating a new wallet.")
        )
        if self.password_dialog(msg):
            reply = self.hid_send_plain(b'{"password":"' + self.password + b'"}')
        else:
            return False

    # Get password from user if not yet set
    msg = _("Enter your Digital Bitbox password:")
    while self.password is None:
        if not self.password_dialog(msg):
            raise UserCancelled()
        reply = self.hid_send_encrypt(b'{"led":"blink"}')
        if "error" in reply:
            self.password = None
            if reply["error"]["code"] == 109:
                msg = (
                    _("Incorrect password entered.")
                    + "\n\n"
                    + reply["error"]["message"]
                    + "\n\n"
                    + _("Enter your Digital Bitbox password:")
                )
            else:
                # Should never occur
                msg = (
                    _("Unexpected error occurred.")
                    + "\n\n"
                    + reply["error"]["message"]
                    + "\n\n"
                    + _("Enter your Digital Bitbox password:")
                )

    # Initialize device if not yet initialized
    if not self.setupRunning:
        self.isInitialized = True  # Wallet exists. Electrum code later checks if the device matches the wallet
    elif not self.isInitialized:
        reply = self.hid_send_encrypt(b'{"device":"info"}')
        if reply["device"]["id"] != "":
            self.recover_or_erase_dialog()  # Already seeded
        else:
            self.seed_device_dialog()  # Seed if not initialized
        self.mobile_pairing_dialog()
    return self.isInitialized