aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/winfloat.c
diff options
context:
space:
mode:
authorglepnir <glephunter@gmail.com>2024-07-17 20:22:59 +0800
committerGitHub <noreply@github.com>2024-07-17 12:22:59 +0000
commit8ddcf9d9390000c05bb817f70a1cbe36f2145d81 (patch)
tree8f733e2bf8ec849270ae65cc9e4c06c8713539bc /src/nvim/winfloat.c
parent5d7fd74397a9fb96bc9d1f72c402884b51255bf2 (diff)
downloadrneovim-8ddcf9d9390000c05bb817f70a1cbe36f2145d81.tar.gz
rneovim-8ddcf9d9390000c05bb817f70a1cbe36f2145d81.tar.bz2
rneovim-8ddcf9d9390000c05bb817f70a1cbe36f2145d81.zip
fix(float): handle error in win_float_create() (#29742)
Problem: Missing error handling in win_float_create() function. Solution: Add an inline function for error handling.
Diffstat (limited to 'src/nvim/winfloat.c')
-rw-r--r--src/nvim/winfloat.c30
1 files changed, 24 insertions, 6 deletions
diff --git a/src/nvim/winfloat.c b/src/nvim/winfloat.c
index 77bbaed105..e70f84ee7b 100644
--- a/src/nvim/winfloat.c
+++ b/src/nvim/winfloat.c
@@ -17,6 +17,7 @@
#include "nvim/grid_defs.h"
#include "nvim/macros_defs.h"
#include "nvim/memory.h"
+#include "nvim/message.h"
#include "nvim/mouse.h"
#include "nvim/move.h"
#include "nvim/option.h"
@@ -359,6 +360,21 @@ win_T *win_float_find_altwin(const win_T *win, const tabpage_T *tp)
: tp->tp_firstwin;
}
+/// Inline helper function for handling errors and cleanup in win_float_create.
+static inline win_T *handle_error_and_cleanup(win_T *wp, Error *err)
+{
+ if (ERROR_SET(err)) {
+ emsg(err->msg);
+ api_clear_error(err);
+ }
+ if (wp) {
+ win_remove(wp, NULL);
+ win_free(wp, NULL);
+ }
+ unblock_autocmds();
+ return NULL;
+}
+
/// create a floating preview window.
///
/// @param[in] bool enter floating window.
@@ -381,23 +397,25 @@ win_T *win_float_create(bool enter, bool new_buf)
block_autocmds();
win_T *wp = win_new_float(NULL, false, config, &err);
if (!wp) {
- unblock_autocmds();
- return NULL;
+ return handle_error_and_cleanup(wp, &err);
}
if (new_buf) {
Buffer b = nvim_create_buf(false, true, &err);
if (!b) {
- win_remove(wp, NULL);
- win_free(wp, NULL);
- unblock_autocmds();
- return NULL;
+ return handle_error_and_cleanup(wp, &err);
}
buf_T *buf = find_buffer_by_handle(b, &err);
+ if (!buf) {
+ return handle_error_and_cleanup(wp, &err);
+ }
buf->b_p_bl = false; // unlist
set_option_direct_for(kOptBufhidden, STATIC_CSTR_AS_OPTVAL("wipe"), OPT_LOCAL, 0, kOptReqBuf,
buf);
win_set_buf(wp, buf, &err);
+ if (ERROR_SET(&err)) {
+ return handle_error_and_cleanup(wp, &err);
+ }
}
unblock_autocmds();
wp->w_p_diff = false;