Bug: Invalid vCards throw error messages and break the addressBook API
Status: RESOLVED
Priority: --
Resolution: FIXED
Created: 2022-10-03 10:44:53+00:00
Resolved: 2022-10-24 18:34:26+00:00

A user of my add-on [Mail Merge](https://addons.thunderbird.net/thunderbird/addon/mail-merge/) reported a problem after the upgrade from Thunderbird 91 / Mail Merge 8 to Thunderbird 102 / Mail Merge 9. After a few hours of analyzing the problem we figured out, that a bug in Thunderbird was ultimately causing the problem.

My add-on uses the [addressBooks API](https://webextension-api.thunderbird.net/en/latest/addressBooks.html) and [contacts API](https://webextension-api.thunderbird.net/en/latest/contacts.html) to get a list of all address books and all contacts.

However, a simple call to `messenger.contacts.get(addressbook.id)` failed for one specific address book, i.e. the Personal Address Book, while the same call succeeded for other address books, e.g. Collected Addresses. The culprit was a particular contact in the affected address book - see `contact.vcf`.

This (ghost) contact was probably created by Thunderbird during the upgrade from 91 to 102. The user says he did not create the contact manually. And I don't think the user could have created this contact within Thunderbird - even if he tried - because Thunderbird simply doesn't allow to create such a contact.

Importing the contact manually via "Address Book -> Import -> vCard" does work. Afterwards the contact is also shown in the contact list in the address book window. However, selecting the contact throws this error message in the error console:
```
Uncaught Error: Could not extract integer from "NaN-"
    strictParseInt resource:///modules/calendar/Ical.jsm:154
    part resource:///modules/calendar/Ical.jsm:6322
    fromDateAndOrTimeString resource:///modules/calendar/Ical.jsm:6337
    formatDate chrome://messenger/content/addressbook/aboutAddressBook.js:2895
    displayContact chrome://messenger/content/addressbook/aboutAddressBook.js:2943
    displayCards chrome://messenger/content/addressbook/aboutAddressBook.js:2659
    _onSelect chrome://messenger/content/addressbook/aboutAddressBook.js:2095
    handleEvent chrome://messenger/content/addressbook/aboutAddressBook.js:1508
    _selectSingle chrome://messenger/content/tree-listbox.js:1592
    connectedCallback chrome://messenger/content/tree-listbox.js:1060
Ical.jsm:154:13
```

Searching for the contact in the address book window throws this error:
```
Could not extract integer from "NaN-" Ical.jsm:154
    strictParseInt resource:///modules/calendar/Ical.jsm:154
    part resource:///modules/calendar/Ical.jsm:6322
    fromDateAndOrTimeString resource:///modules/calendar/Ical.jsm:6337
    toAbCard resource:///modules/VCardUtils.jsm:404
    InterpretGeneratorResume self-hosted:1422
    next self-hosted:1370
    toPropertyMap resource:///modules/VCardUtils.jsm:918
    results resource:///modules/AddrBookDirectory.jsm:379
    filter self-hosted:198
    search resource:///modules/AddrBookDirectory.jsm:369
    ABView chrome://messenger/content/addressbook/abView-new.js:28
    _onCommand chrome://messenger/content/addressbook/aboutAddressBook.js:2008
    handleEvent chrome://messenger/content/addressbook/aboutAddressBook.js:1502
    _fireCommand chrome://messenger/content/addressbook/aboutAddressBook.js:1183
    _fireCommand self-hosted:1115
```

Autocompletion in the To field in the compose window throws this error:
```
Could not extract integer from "NaN-" Ical.jsm:154
    strictParseInt resource:///modules/calendar/Ical.jsm:154
    part resource:///modules/calendar/Ical.jsm:6322
    fromDateAndOrTimeString resource:///modules/calendar/Ical.jsm:6337
    toAbCard resource:///modules/VCardUtils.jsm:404
    InterpretGeneratorResume self-hosted:1422
    next self-hosted:1370
    toPropertyMap resource:///modules/VCardUtils.jsm:918
    results resource:///modules/AddrBookDirectory.jsm:379
    filter self-hosted:198
    search resource:///modules/AddrBookDirectory.jsm:369
    _searchCards resource:///modules/AbAutoCompleteSearch.jsm:222
    _searchCards resource:///modules/AbAutoCompleteSearch.jsm:221
    startSearch resource:///modules/AbAutoCompleteSearch.jsm:478
```

I don't know, how the (ghost) contact has been created in the first place. I suspect this to be some kind of problem during the upgrade from Thunderbird 91 to 102. But as I can't provide any kind of useful information, e.g. steps to reproduce, I didn't file a dedicated bug, yet.

Anyway, an invalid contact like this can be easily imported either manually or especially via CardDAV. So I recommend to harden Thunderbird in a way, that invalid vCards or invalid vCard properties are handled gracefully.

There was a similar bug fixed just a few weeks ago in Thunderbird 102.3.0: https://bugzilla.mozilla.org/show_bug.cgi?id=1789793