Repository: neovim/neovim
File: src/nvim/mouse.c (C)

Code before:
    int click_row = mouse_row;
    int click_col = mouse_col;
    win_T *wp = mouse_find_win(&click_grid, &click_row, &click_col);
    if (wp == NULL) {
      return false;
    }

    StlClickDefinition *click_defs = in_status_line ? wp->w_status_click_defs
                                                    : in_winbar ? wp->w_winbar_click_defs
                                                                : wp->w_statuscol_click_defs;

    if (in_status_line && global_stl_height() > 0) {
      // global statusline is displayed for the current window,
      // and spans the whole screen.
      click_defs = curwin->w_status_click_defs;
      click_col = mouse_col;
    }

    if (in_statuscol && wp->w_p_rl) {
      click_col = wp->w_width_inner - click_col - 1;
    }

    if ((in_statuscol && click_col >= (int)wp->w_statuscol_click_defs_size)
        || (in_status_line
            && click_col >=
            (int)(global_stl_height() > 0 ? curwin : wp)->w_status_click_defs_size)) {
      return false;
    }

    if (click_defs != NULL) {
      switch (click_defs[click_col].type) {
      case kStlClickDisabled:
        // If there is no click definition, still open the popupmenu for a
        // statuscolumn click like a click in the sign/number column does.
        if (in_statuscol && mouse_model_popup()
            && which_button == MOUSE_RIGHT && !(mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))) {
          do_popup(which_button, m_pos_flag, m_pos);
        }
        break;
      case kStlClickFuncRun:
        call_click_def_func(click_defs, click_col, which_button);
        break;
      default:
        assert(false && "winbar, statusline and statuscolumn only support %@ for clicks");
        break;
      }
    }

    if (!(in_statuscol && (jump_flags & (MOUSE_FOLD_CLOSE|MOUSE_FOLD_OPEN)))) {
      return false;
    }

Review comment (bug):
This is actually fix to a separate bug. Setting  'statusline' option in triggers it.
I guess because old default statusline didn't allocate click_defs so it didn't appear in test-suit. This was causing out of bound memory access leading to flaky test crashes.

It seems click_col can be == w_statuscol_click_def_size. which exceeds the allocated buffer size. Which is pretty weird. I was thinking it might be due to some mismatch of 0/1 based indexing. Spent quite a while looking at the related code but couldn't find the. Also clickable regions work pixel perfect. Couldn't really reproduce it other then in the test suit there. Adding a check here seemed like good fix afterwards since I couldn't really reproduce the crash any other way and the behavior was acting correctly.

Should I move it to a separate pr? Or leave it here?

Code after:
  if ((in_winbar || in_status_line || in_statuscol) && is_click) {
    // Handle click event on window bar, status line or status column
    int click_grid = mouse_grid;
    int click_row = mouse_row;
    int click_col = mouse_col;
    win_T *wp = mouse_find_win(&click_grid, &click_row, &click_col);
    if (wp == NULL) {
      return false;
    }

    StlClickDefinition *click_defs = in_status_line ? wp->w_status_click_defs
                                                    : in_winbar ? wp->w_winbar_click_defs
                                                                : wp->w_statuscol_click_defs;

    if (in_global_statusline) {
      // global statusline is displayed for the current window,
      // and spans the whole screen.
      click_defs = curwin->w_status_click_defs;
      click_col = mouse_col;
    }

    if (in_statuscol && wp->w_p_rl) {
      click_col = wp->w_width_inner - click_col - 1;
    }

    if ((in_statuscol && click_col >= (int)wp->w_statuscol_click_defs_size)
        || (in_status_line
            && click_col >=
            (int)(in_global_statusline ? curwin : wp)->w_status_click_defs_size)) {
      return false;
    }

    if (click_defs != NULL) {
      switch (click_defs[click_col].type) {
      case kStlClickDisabled:
        // If there is no click definition, still open the popupmenu for a
        // statuscolumn click like a click in the sign/number column does.
        if (in_statuscol && mouse_model_popup()
            && which_button == MOUSE_RIGHT && !(mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))) {
          do_popup(which_button, m_pos_flag, m_pos);
        }
        break;
      case kStlClickFuncRun:
        call_click_def_func(click_defs, click_col, which_button);
        break;
      default:
        assert(false && "winbar, statusline and statuscolumn only support %@ for clicks");
        break;
      }
    }