Bug: JavaScript error: resource://gre/actors/AutoCompleteChild.jsm, line 196: Error: Invalid autocomplete selectedIndex
Status: RESOLVED
Priority: --
Resolution: FIXED
Created: 2020-05-05 13:12:39+00:00
Resolved: 2021-07-05 02:20:07+00:00

During the local mochitest run with FULL DEBUG version of TB, I see
the following error.
```
30:55.73 GECKO(1431743) [1431743, Main Thread] WARNING: NS_ENSURE_SUCCESS(rv, rv) failed with result 0x80004005 (NS_ERROR_FAILURE): file /NEW-SSD/NREF-COMM-CENTRAL/mozilla/dom/base/nsContentUtils.cpp, line 3770
30:55.99 GECKO(1431743) JavaScript error: resource://gre/actors/AutoCompleteChild.jsm, line 196: Error: Invalid autocomplete selectedIndex
30:55.99 GECKO(1431743) [1431743, Main Thread] WARNING: NS_ENSURE_TRUE(aIndex >= 0 && static_cast<uint32_t>(aIndex) < mMatchCount) failed: file /NEW-SSD/NREF-COMM-CENTRAL/mozilla/toolkit/components/autocomplete/nsAutoCompleteController.cpp, line 1698
```
The error in JavaScript is thrown from the code in AutoCompleteChild.jsm..:

```
  get selectedIndex() {
    // selectedIndex getter must be synchronous because we need the
    // correct value when the controller is in controller::HandleEnter.
    // We can't easily just let the parent inform us the new value every
    // time it changes because not every action that can change the
    // selectedIndex is trivial to catch (e.g. moving the mouse over the
    // list).
    let selectedIndexResult = Services.cpmm.sendSyncMessage(
      "FormAutoComplete:GetSelectedIndex",
      {
        browsingContext: this.browsingContext,
      }
    );

    if (
      selectedIndexResult.length != 1 ||
      !Number.isInteger(selectedIndexResult[0])
    ) {
*     throw new Error("Invalid autocomplete selectedIndex"); <===
    }
    return selectedIndexResult[0];
  }
```

The JavaScript errors are found in the following two tests.
```
30:55.33 TEST_START: comm/mail/base/test/browser/browser_menulist.js
30:55.34 INFO Entering test bound
30:55.99 GECKO(1431743) JavaScript error: resource://gre/actors/AutoCompleteChild.jsm, line 196: Error: Invalid autocomplete selectedIndex
30:56.00 GECKO(1431743) JavaScript error: resource://gre/actors/AutoCompleteChild.jsm, line 196: Error: Invalid autocomplete selectedIndex
30:56.08 INFO Console message: [JavaScript Error: "Error: Invalid autocomplete selectedIndex" {file: "resource://gre/actors/AutoCompleteChild.jsm" line: 196}]
30:56.08 INFO Console message: [JavaScript Error: "Error: Invalid autocomplete selectedIndex" {file: "resource://gre/actors/AutoCompleteChild.jsm" line: 196}]
30:56.45 INFO Leaving test bound
30:56.50 TEST_END: Test OK. Subtests passed 54/55. Unexpected 0


52:50.02 TEST_START: comm/mail/components/preferences/test/browser/browser_cloudfile.js
52:50.04 INFO Entering test bound
52:50.04 INFO Leaving test bound
52:50.04 INFO Entering test bound addRemoveAccounts
52:54.92 GECKO(1445883) JavaScript error: resource://gre/actors/AutoCompleteChild.jsm, line 196: Error: Invalid autocomplete selectedIndex
52:55.46 INFO Console message: [JavaScript Error: "Error: Invalid autocomplete selectedIndex" {file: "resource://gre/actors/AutoCompleteChild.jsm" line: 196}]
52:56.73 INFO Leaving test bound addRemoveAccounts
         ...
53:11.44 TEST_END: Test OK. Subtests passed 118/118. Unexpected 0
```

The error from the CPP part comes from NS_ENSURE_TRUE as follows.:
```
nsresult nsAutoCompleteController::GetResultValueLabelAt(int32_t aIndex,
                                                         bool aGetFinalValue,
                                                         bool aGetValue,
                                                         nsAString& _retval) {
  NS_ENSURE_TRUE(aIndex >= 0 && static_cast<uint32_t>(aIndex) < mMatchCount,
                 NS_ERROR_ILLEGAL_VALUE);

  int32_t matchIndex;
  nsIAutoCompleteResult* result;
```

I am attaching the relevant part of the local log. It is a bit verbose
with the locally added dumps.