mirror of
				https://github.com/neovim/neovim.git
				synced 2025-10-26 12:27:24 +00:00 
			
		
		
		
	fix(memline): don't check line count for closed memline #32403
Problem:  Error thrown when for invalid line number which may be accessed
          in an `on_detach` callback at which point line count is
          intentionally set to 0.
Solution: Move empty memline check to before line number check.
(cherry picked from commit 15bc930fca)
			
			
This commit is contained in:
		 luukvbaal
					luukvbaal
				
			
				
					committed by
					
						![github-actions[bot]](/assets/img/avatar_default.png) github-actions[bot]
						github-actions[bot]
					
				
			
			
				
	
			
			
			![github-actions[bot]](/assets/img/avatar_default.png) github-actions[bot]
						github-actions[bot]
					
				
			
						parent
						
							7e5b7ae4e0
						
					
				
				
					commit
					3fd08c2eb2
				
			| @@ -1880,6 +1880,11 @@ static char *ml_get_buf_impl(buf_T *buf, linenr_T lnum, bool will_change) | ||||
|   static int recursive = 0; | ||||
|   static char questions[4]; | ||||
|  | ||||
|   if (buf->b_ml.ml_mfp == NULL) {       // there are no lines | ||||
|     buf->b_ml.ml_line_len = 1; | ||||
|     return ""; | ||||
|   } | ||||
|  | ||||
|   if (lnum > buf->b_ml.ml_line_count) {  // invalid line number | ||||
|     if (recursive == 0) { | ||||
|       // Avoid giving this message for a recursive call, may happen when | ||||
| @@ -1899,11 +1904,6 @@ errorret: | ||||
|     lnum = 1; | ||||
|   } | ||||
|  | ||||
|   if (buf->b_ml.ml_mfp == NULL) {       // there are no lines | ||||
|     buf->b_ml.ml_line_len = 1; | ||||
|     return ""; | ||||
|   } | ||||
|  | ||||
|   // See if it is the same line as requested last time. | ||||
|   // Otherwise may need to flush last used line. | ||||
|   // Don't use the last used line when 'swapfile' is reset, need to load all | ||||
|   | ||||
| @@ -300,6 +300,24 @@ describe('lua buffer event callbacks: on_lines', function() | ||||
|     n.assert_alive() | ||||
|   end) | ||||
|  | ||||
|   it('no invalid lnum error for closed memline in on_detach #31251', function() | ||||
|     eq(vim.NIL, exec_lua('return _G.did_detach')) | ||||
|     exec_lua([[ | ||||
|       vim.api.nvim_buf_set_lines(0, 0, -1, false, { '' }) | ||||
|       local bufname = 'buf2' | ||||
|       local buf = vim.api.nvim_create_buf(false, true) | ||||
|       vim.api.nvim_buf_set_name(buf, bufname) | ||||
|       vim.bo[buf].bufhidden = 'wipe' | ||||
|       vim.cmd('vertical diffsplit '..bufname) | ||||
|       vim.api.nvim_buf_attach(0, false, { on_detach = function() | ||||
|         vim.cmd("redraw") | ||||
|         _G.did_detach = true | ||||
|       end}) | ||||
|       vim.cmd.bdelete() | ||||
|     ]]) | ||||
|     eq(true, exec_lua('return _G.did_detach')) | ||||
|   end) | ||||
|  | ||||
|   it('#12718 lnume', function() | ||||
|     api.nvim_buf_set_lines(0, 0, -1, true, { '1', '2', '3' }) | ||||
|     exec_lua([[ | ||||
|   | ||||
		Reference in New Issue
	
	Block a user