int idx)
 {
        int i;
+       int n_channels = 0;
 
        if (fw_has_api(&mvm->fw->ucode_capa,
                       IWL_UCODE_TLV_API_SCAN_OFFLOAD_CHANS)) {
 
                for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN * 8; i++)
                        if (matches[idx].matching_channels[i / 8] & (BIT(i % 8)))
-                               match->channels[match->n_channels++] =
+                               match->channels[n_channels++] =
                                        mvm->nd_channels[i]->center_freq;
        } else {
                struct iwl_scan_offload_profile_match_v1 *matches =
 
                for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN_V1 * 8; i++)
                        if (matches[idx].matching_channels[i / 8] & (BIT(i % 8)))
-                               match->channels[match->n_channels++] =
+                               match->channels[n_channels++] =
                                        mvm->nd_channels[i]->center_freq;
        }
+       /* We may have ended up with fewer channels than we allocated. */
+       match->n_channels = n_channels;
 }
 
 /**
                             GFP_KERNEL);
        if (!net_detect || !n_matches)
                goto out_report_nd;
+       net_detect->n_matches = n_matches;
+       n_matches = 0;
 
        for_each_set_bit(i, &matched_profiles, mvm->n_nd_match_sets) {
                struct cfg80211_wowlan_nd_match *match;
                                GFP_KERNEL);
                if (!match)
                        goto out_report_nd;
+               match->n_channels = n_channels;
 
-               net_detect->matches[net_detect->n_matches++] = match;
+               net_detect->matches[n_matches++] = match;
 
                /* We inverted the order of the SSIDs in the scan
                 * request, so invert the index here.
 
                iwl_mvm_query_set_freqs(mvm, d3_data->nd_results, match, i);
        }
+       /* We may have fewer matches than we allocated. */
+       net_detect->n_matches = n_matches;
 
 out_report_nd:
        wakeup.net_detect = net_detect;