Skip to content

Commit

Permalink
patch 9.1.0813: no error handling with setglobal and number types
Browse files Browse the repository at this point in the history
Problem:  no error handling with setglobal and number types
Solution: validate values when using :setglobal with number option types
          (Milly)

closes: #15928

Signed-off-by: Milly <milly.ca@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
  • Loading branch information
Milly authored and chrisbra committed Oct 23, 2024
1 parent d080986 commit 1180728
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 23 deletions.
88 changes: 73 additions & 15 deletions src/option.c
Original file line number Diff line number Diff line change
Expand Up @@ -3465,6 +3465,16 @@ did_set_conceallevel(optset_T *args UNUSED)
errmsg = e_invalid_argument;
curwin->w_p_cole = 3;
}
if (curwin->w_allbuf_opt.wo_cole < 0)
{
errmsg = e_argument_must_be_positive;
curwin->w_allbuf_opt.wo_cole = 0;
}
else if (curwin->w_allbuf_opt.wo_cole > 3)
{
errmsg = e_invalid_argument;
curwin->w_allbuf_opt.wo_cole = 3;
}

return errmsg;
}
Expand Down Expand Up @@ -3530,6 +3540,16 @@ did_set_foldcolumn(optset_T *args UNUSED)
errmsg = e_invalid_argument;
curwin->w_p_fdc = 12;
}
if (curwin->w_allbuf_opt.wo_fdc < 0)
{
errmsg = e_argument_must_be_positive;
curwin->w_allbuf_opt.wo_fdc = 0;
}
else if (curwin->w_allbuf_opt.wo_fdc > 12)
{
errmsg = e_invalid_argument;
curwin->w_allbuf_opt.wo_fdc = 12;
}

return errmsg;
}
Expand Down Expand Up @@ -3856,11 +3876,21 @@ did_set_numberwidth(optset_T *args UNUSED)
errmsg = e_argument_must_be_positive;
curwin->w_p_nuw = 1;
}
if (curwin->w_p_nuw > 20)
else if (curwin->w_p_nuw > 20)
{
errmsg = e_invalid_argument;
curwin->w_p_nuw = 20;
}
if (curwin->w_allbuf_opt.wo_nuw < 1)
{
errmsg = e_argument_must_be_positive;
curwin->w_allbuf_opt.wo_nuw = 1;
}
else if (curwin->w_allbuf_opt.wo_nuw > 20)
{
errmsg = e_invalid_argument;
curwin->w_allbuf_opt.wo_nuw = 20;
}
curwin->w_nrwidth_line_count = 0; // trigger a redraw

return errmsg;
Expand Down Expand Up @@ -4141,6 +4171,27 @@ did_set_shiftwidth_tabstop(optset_T *args)
long *pp = (long *)args->os_varp;
char *errmsg = NULL;

if (curbuf->b_p_ts <= 0)
{
errmsg = e_argument_must_be_positive;
curbuf->b_p_ts = 8;
}
else if (curbuf->b_p_ts > TABSTOP_MAX)
{
errmsg = e_invalid_argument;
curbuf->b_p_ts = 8;
}
if (p_ts <= 0)
{
errmsg = e_argument_must_be_positive;
p_ts = 8;
}
else if (p_ts > TABSTOP_MAX)
{
errmsg = e_invalid_argument;
p_ts = 8;
}

if (curbuf->b_p_sw < 0)
{
errmsg = e_argument_must_be_positive;
Expand All @@ -4151,6 +4202,18 @@ did_set_shiftwidth_tabstop(optset_T *args)
: curbuf->b_p_ts;
#else
curbuf->b_p_sw = curbuf->b_p_ts;
#endif
}
if (p_sw < 0)
{
errmsg = e_argument_must_be_positive;
#ifdef FEAT_VARTABS
// Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
? tabstop_first(curbuf->b_p_vts_array)
: curbuf->b_p_ts;
#else
p_sw = curbuf->b_p_ts;
#endif
}

Expand Down Expand Up @@ -4342,6 +4405,11 @@ did_set_textwidth(optset_T *args UNUSED)
errmsg = e_argument_must_be_positive;
curbuf->b_p_tw = 0;
}
if (p_tw < 0)
{
errmsg = e_argument_must_be_positive;
p_tw = 0;
}
#ifdef FEAT_SYN_HL
{
win_T *wp;
Expand Down Expand Up @@ -4810,16 +4878,6 @@ check_num_option_bounds(
p_window = Rows - 1;
}

if (curbuf->b_p_ts <= 0)
{
errmsg = e_argument_must_be_positive;
curbuf->b_p_ts = 8;
}
else if (curbuf->b_p_ts > TABSTOP_MAX)
{
errmsg = e_invalid_argument;
curbuf->b_p_ts = 8;
}
if (p_tm < 0)
{
errmsg = e_argument_must_be_positive;
Expand Down Expand Up @@ -4952,6 +5010,10 @@ set_num_option(
need_mouse_correct = TRUE;
#endif

// May set global value for local option.
if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
*(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;

// Invoke the option specific callback function to validate and apply the
// new value.
if (options[opt_idx].opt_did_set_cb != NULL)
Expand All @@ -4971,10 +5033,6 @@ set_num_option(
errmsg = check_num_option_bounds(pp, old_value, old_Rows, old_Columns,
errbuf, errbuflen, errmsg);

// May set global value for local option.
if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
*(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;

options[opt_idx].flags |= P_WAS_SET;

#if defined(FEAT_EVAL)
Expand Down
8 changes: 0 additions & 8 deletions src/testdir/gen_opt_test.vim
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,6 @@ endwhile
let skip_setglobal_reasons = #{
\ iminsert: 'The global value is always overwritten by the local value',
\ imsearch: 'The global value is always overwritten by the local value',
\ conceallevel: 'TODO: fix missing error handling for setglobal',
\ foldcolumn: 'TODO: fix missing error handling for setglobal',
\ numberwidth: 'TODO: fix missing error handling for setglobal',
\ scrolloff: 'TODO: fix missing error handling for setglobal',
\ shiftwidth: 'TODO: fix missing error handling for setglobal',
\ sidescrolloff: 'TODO: fix missing error handling for setglobal',
\ tabstop: 'TODO: fix missing error handling for setglobal',
\ textwidth: 'TODO: fix missing error handling for setglobal',
\}

" Script header.
Expand Down
2 changes: 2 additions & 0 deletions src/version.c
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,8 @@ static char *(features[]) =

static int included_patches[] =
{ /* Add new patch number below this line */
/**/
813,
/**/
812,
/**/
Expand Down

0 comments on commit 1180728

Please sign in to comment.