From 4450a454662a2bc1c2cb3cdfea76a832eabf8aad Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Mon, 16 Feb 2026 21:03:21 +0800 Subject: [PATCH] vim-patch:9.1.1954: Setting a byte in a blob, accepts values outside 0-255 Problem: Setting a byte in a blob, accepts values outside 0-255 Solution: When setting a byte in a blob, check for valid values (Yegappan Lakshmanan) closes: vim/vim#18870 https://github.com/vim/vim/commit/f4a299700e211a728f0f7398eb8fceaf44165711 Co-authored-by: Yegappan Lakshmanan --- src/nvim/errors.h | 1 + src/nvim/eval.c | 8 ++++++-- src/nvim/eval/typval.c | 2 -- test/old/testdir/test_blob.vim | 9 +++++++++ 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/nvim/errors.h b/src/nvim/errors.h index cecf2d36c5..be2b8ca800 100644 --- a/src/nvim/errors.h +++ b/src/nvim/errors.h @@ -197,6 +197,7 @@ EXTERN const char e_invalid_line_number_nr[] INIT(= N_("E966: Invalid line numbe EXTERN const char e_reduce_of_an_empty_str_with_no_initial_value[] INIT(= N_("E998: Reduce of an empty %s with no initial value")); +EXTERN const char e_invalid_value_for_blob_nr[] INIT(= N_("E1239: Invalid value for blob: 0x" PRIX64)); EXTERN const char e_stray_closing_curly_str[] INIT(= N_("E1278: Stray '}' without a matching '{': %s")); EXTERN const char e_missing_close_curly_str[] diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 73557517da..550ac3483f 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -1315,9 +1315,13 @@ void set_var_lval(lval_T *lp, char *endp, typval_T *rettv, bool copy, const bool } } else { bool error = false; - const char val = (char)tv_get_number_chk(rettv, &error); + const varnumber_T val = tv_get_number_chk(rettv, &error); if (!error) { - tv_blob_set_append(lp->ll_blob, lp->ll_n1, (uint8_t)val); + if (val < 0 || val > 255) { + semsg(_(e_invalid_value_for_blob_nr), val); + } else { + tv_blob_set_append(lp->ll_blob, lp->ll_n1, (uint8_t)val); + } } } } else if (op != NULL && *op != '=') { diff --git a/src/nvim/eval/typval.c b/src/nvim/eval/typval.c index 3c88f5c967..84d94f2643 100644 --- a/src/nvim/eval/typval.c +++ b/src/nvim/eval/typval.c @@ -99,8 +99,6 @@ static const char e_list_or_blob_required_for_argument_nr[] = N_("E1226: List or Blob required for argument %d"); static const char e_blob_required_for_argument_nr[] = N_("E1238: Blob required for argument %d"); -static const char e_invalid_value_for_blob_nr[] - = N_("E1239: Invalid value for blob: %d"); static const char e_string_list_or_blob_required_for_argument_nr[] = N_("E1252: String, List or Blob required for argument %d"); static const char e_string_or_function_required_for_argument_nr[] diff --git a/test/old/testdir/test_blob.vim b/test/old/testdir/test_blob.vim index f4e79d6ea5..1b8165f5a0 100644 --- a/test/old/testdir/test_blob.vim +++ b/test/old/testdir/test_blob.vim @@ -895,4 +895,13 @@ func Test_blob_items() call CheckSourceLegacyAndVim9Success(lines) endfunc +" Test for setting a byte in a blob with invalid value +func Test_blob_byte_set_invalid_value() + let lines =<< trim END + VAR b = 0zD0C3E4E18E1B + LET b[0] = 229539777187355 + END + call CheckSourceLegacyAndVim9Failure(lines, 'E1239: Invalid value for blob:') +endfunc + " vim: shiftwidth=2 sts=2 expandtab