Skip to content

Commit c15de97

Browse files
John Marriottchrisbra
authored andcommitted
patch 9.1.1028: too many strlen() calls in screen.c
Problem: too many strlen() calls in screen.c Solution: refactor screen.c and remove calls to strlen(), verify that leadmultispace != NULL (John Marriott) closes: #16460 Signed-off-by: John Marriott <basilisk@internode.on.net> Signed-off-by: Christian Brabandt <cb@256bit.org>
1 parent ca307ef commit c15de97

2 files changed

Lines changed: 71 additions & 64 deletions

File tree

src/screen.c

Lines changed: 69 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -240,13 +240,10 @@ win_draw_end(
240240
int
241241
compute_foldcolumn(win_T *wp, int col)
242242
{
243-
int fdc = wp->w_p_fdc;
244243
int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
245-
int wwidth = wp->w_width;
244+
int n = wp->w_width - (col + wmw);
246245

247-
if (fdc > wwidth - (col + wmw))
248-
fdc = wwidth - (col + wmw);
249-
return fdc;
246+
return MIN(wp->w_p_fdc, n);
250247
}
251248

252249
/*
@@ -271,6 +268,7 @@ fill_foldcolumn(
271268
size_t byte_counter = 0;
272269
int symbol = 0;
273270
int len = 0;
271+
int n;
274272

275273
// Init to all spaces.
276274
vim_memset(p, ' ', MAX_MCO * fdc + 1);
@@ -284,7 +282,8 @@ fill_foldcolumn(
284282
if (first_level < 1)
285283
first_level = 1;
286284

287-
for (i = 0; i < MIN(fdc, level); i++)
285+
n = MIN(fdc, level); // evaluate this once
286+
for (i = 0; i < n; i++)
288287
{
289288
if (win_foldinfo.fi_lnum == lnum
290289
&& first_level + i >= win_foldinfo.fi_low_level)
@@ -1120,7 +1119,7 @@ win_redr_custom(
11201119
// might change the option value and free the memory.
11211120
stl = vim_strsave(stl);
11221121
width = build_stl_str_hl(ewp, buf, sizeof(buf),
1123-
stl, opt_name, opt_scope,
1122+
(stl == NULL) ? (char_u *)"" : stl, opt_name, opt_scope,
11241123
fillchar, maxwidth, &hltab, &tabtab);
11251124
vim_free(stl);
11261125
ewp->w_p_crb = p_crb_save;
@@ -1129,12 +1128,13 @@ win_redr_custom(
11291128
p = transstr(buf);
11301129
if (p != NULL)
11311130
{
1132-
vim_strncpy(buf, p, sizeof(buf) - 1);
1131+
len = vim_snprintf((char *)buf, sizeof(buf), "%s", p);
11331132
vim_free(p);
11341133
}
1134+
else
1135+
len = (int)STRLEN(buf);
11351136

11361137
// fill up with "fillchar"
1137-
len = (int)STRLEN(buf);
11381138
while (width < maxwidth && len < (int)sizeof(buf) - 1)
11391139
{
11401140
len += (*mb_char2bytes)(fillchar, buf + len);
@@ -4368,8 +4368,7 @@ draw_tabline(void)
43684368
{
43694369
if (wincount > 1)
43704370
{
4371-
vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
4372-
len = (int)STRLEN(NameBuff);
4371+
len = vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
43734372
if (col + len >= Columns - 3)
43744373
break;
43754374
screen_puts_len(NameBuff, len, 0, col,
@@ -4389,6 +4388,8 @@ draw_tabline(void)
43894388
room = scol - col + tabwidth - 1;
43904389
if (room > 0)
43914390
{
4391+
int n;
4392+
43924393
// Get buffer name in NameBuff[]
43934394
get_trans_bufname(cwp->w_buffer);
43944395
shorten_dir(NameBuff);
@@ -4405,8 +4406,9 @@ draw_tabline(void)
44054406
p += len - room;
44064407
len = room;
44074408
}
4408-
if (len > Columns - col - 1)
4409-
len = Columns - col - 1;
4409+
n = Columns - col - 1;
4410+
if (len > n)
4411+
len = n;
44104412

44114413
screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
44124414
col += len;
@@ -4429,7 +4431,8 @@ draw_tabline(void)
44294431
// Draw the 'showcmd' information if 'showcmdloc' == "tabline".
44304432
if (p_sc && *p_sloc == 't')
44314433
{
4432-
int width = MIN(10, (int)Columns - col - (tabcount > 1) * 3);
4434+
int n = (int)Columns - col - (tabcount > 1) * 3;
4435+
int width = MIN(10, n);
44334436

44344437
if (width > 0)
44354438
screen_puts_len(showcmd_buf, width, 0, (int)Columns
@@ -4687,44 +4690,48 @@ get_encoded_char_adv(char_u **p)
46874690
struct charstab
46884691
{
46894692
int *cp;
4690-
char *name;
4693+
string_T name;
46914694
};
4695+
4696+
#define CHARSTAB_ENTRY(cp, name) \
4697+
{(cp), {(char_u *)(name), STRLEN_LITERAL(name)}}
4698+
46924699
static fill_chars_T fill_chars;
46934700
static struct charstab filltab[] =
46944701
{
4695-
{&fill_chars.stl, "stl"},
4696-
{&fill_chars.stlnc, "stlnc"},
4697-
{&fill_chars.vert, "vert"},
4698-
{&fill_chars.fold, "fold"},
4699-
{&fill_chars.foldopen, "foldopen"},
4700-
{&fill_chars.foldclosed, "foldclose"},
4701-
{&fill_chars.foldsep, "foldsep"},
4702-
{&fill_chars.diff, "diff"},
4703-
{&fill_chars.eob, "eob"},
4704-
{&fill_chars.lastline, "lastline"},
4702+
CHARSTAB_ENTRY(&fill_chars.stl, "stl"),
4703+
CHARSTAB_ENTRY(&fill_chars.stlnc, "stlnc"),
4704+
CHARSTAB_ENTRY(&fill_chars.vert, "vert"),
4705+
CHARSTAB_ENTRY(&fill_chars.fold, "fold"),
4706+
CHARSTAB_ENTRY(&fill_chars.foldopen, "foldopen"),
4707+
CHARSTAB_ENTRY(&fill_chars.foldclosed, "foldclose"),
4708+
CHARSTAB_ENTRY(&fill_chars.foldsep, "foldsep"),
4709+
CHARSTAB_ENTRY(&fill_chars.diff, "diff"),
4710+
CHARSTAB_ENTRY(&fill_chars.eob, "eob"),
4711+
CHARSTAB_ENTRY(&fill_chars.lastline, "lastline")
47054712
};
47064713
static lcs_chars_T lcs_chars;
47074714
static struct charstab lcstab[] =
47084715
{
4709-
{&lcs_chars.eol, "eol"},
4710-
{&lcs_chars.ext, "extends"},
4711-
{&lcs_chars.nbsp, "nbsp"},
4712-
{&lcs_chars.prec, "precedes"},
4713-
{&lcs_chars.space, "space"},
4714-
{&lcs_chars.tab2, "tab"},
4715-
{&lcs_chars.trail, "trail"},
4716-
{&lcs_chars.lead, "lead"},
4716+
CHARSTAB_ENTRY(&lcs_chars.eol, "eol"),
4717+
CHARSTAB_ENTRY(&lcs_chars.ext, "extends"),
4718+
CHARSTAB_ENTRY(&lcs_chars.nbsp, "nbsp"),
4719+
CHARSTAB_ENTRY(&lcs_chars.prec, "precedes"),
4720+
CHARSTAB_ENTRY(&lcs_chars.space, "space"),
4721+
CHARSTAB_ENTRY(&lcs_chars.tab2, "tab"),
4722+
CHARSTAB_ENTRY(&lcs_chars.trail, "trail"),
4723+
CHARSTAB_ENTRY(&lcs_chars.lead, "lead"),
47174724
#ifdef FEAT_CONCEAL
4718-
{&lcs_chars.conceal, "conceal"},
4725+
CHARSTAB_ENTRY(&lcs_chars.conceal, "conceal"),
47194726
#else
4720-
{NULL, "conceal"},
4727+
CHARSTAB_ENTRY(NULL, "conceal"),
47214728
#endif
4722-
{NULL, "multispace"},
4723-
{NULL, "leadmultispace"},
4729+
CHARSTAB_ENTRY(NULL, "multispace"),
4730+
CHARSTAB_ENTRY(NULL, "leadmultispace")
47244731
};
47254732

47264733
static char *
4727-
field_value_err(char *errbuf, size_t errbuflen, char *fmt, char *field)
4734+
field_value_err(char *errbuf, size_t errbuflen, char *fmt, char_u *field)
47284735
{
47294736
if (errbuf == NULL)
47304737
return "";
@@ -4744,7 +4751,7 @@ field_value_err(char *errbuf, size_t errbuflen, char *fmt, char *field)
47444751
set_chars_option(win_T *wp, char_u *value, int is_listchars, int apply,
47454752
char *errbuf, size_t errbuflen)
47464753
{
4747-
int round, i, len, entries;
4754+
int round, i, entries;
47484755
char_u *p, *s;
47494756
int c1 = 0, c2 = 0, c3 = 0;
47504757
char_u *last_multispace = NULL; // Last occurrence of "multispace:"
@@ -4767,7 +4774,7 @@ set_chars_option(win_T *wp, char_u *value, int is_listchars, int apply,
47674774
tab = filltab;
47684775
entries = ARRAY_LENGTH(filltab);
47694776
if (wp->w_p_fcs[0] == NUL)
4770-
value = p_fcs; // local value is empty, us the global value
4777+
value = p_fcs; // local value is empty, use the global value
47714778
}
47724779

47734780
// first round: check for valid value, second round: assign values
@@ -4797,7 +4804,8 @@ set_chars_option(win_T *wp, char_u *value, int is_listchars, int apply,
47974804
{
47984805
lcs_chars.leadmultispace =
47994806
ALLOC_MULT(int, lead_multispace_len + 1);
4800-
lcs_chars.leadmultispace[lead_multispace_len] = NUL;
4807+
if (lcs_chars.leadmultispace != NULL)
4808+
lcs_chars.leadmultispace[lead_multispace_len] = NUL;
48014809
}
48024810
else
48034811
lcs_chars.leadmultispace = NULL;
@@ -4821,13 +4829,12 @@ set_chars_option(win_T *wp, char_u *value, int is_listchars, int apply,
48214829
{
48224830
for (i = 0; i < entries; ++i)
48234831
{
4824-
len = (int)STRLEN(tab[i].name);
4825-
if (!(STRNCMP(p, tab[i].name, len) == 0 && p[len] == ':'))
4832+
if (!(STRNCMP(p, tab[i].name.string, tab[i].name.length) == 0 && p[tab[i].name.length] == ':'))
48264833
continue;
48274834

4828-
if (is_listchars && strcmp(tab[i].name, "multispace") == 0)
4835+
s = p + tab[i].name.length + 1;
4836+
if (is_listchars && STRCMP(tab[i].name.string, "multispace") == 0)
48294837
{
4830-
s = p + len + 1;
48314838
if (round == 0)
48324839
{
48334840
// Get length of lcs-multispace string in first round
@@ -4839,14 +4846,14 @@ set_chars_option(win_T *wp, char_u *value, int is_listchars, int apply,
48394846
if (char2cells(c1) > 1)
48404847
return field_value_err(errbuf, errbuflen,
48414848
e_wrong_character_width_for_field_str,
4842-
tab[i].name);
4849+
tab[i].name.string);
48434850
++multispace_len;
48444851
}
48454852
if (multispace_len == 0)
48464853
// lcs-multispace cannot be an empty string
48474854
return field_value_err(errbuf, errbuflen,
48484855
e_wrong_number_of_characters_for_field_str,
4849-
tab[i].name);
4856+
tab[i].name.string);
48504857
p = s;
48514858
}
48524859
else
@@ -4856,17 +4863,16 @@ set_chars_option(win_T *wp, char_u *value, int is_listchars, int apply,
48564863
while (*s != NUL && *s != ',')
48574864
{
48584865
c1 = get_encoded_char_adv(&s);
4859-
if (p == last_multispace)
4866+
if (p == last_multispace && lcs_chars.multispace != NULL)
48604867
lcs_chars.multispace[multispace_pos++] = c1;
48614868
}
48624869
p = s;
48634870
}
48644871
break;
48654872
}
48664873

4867-
if (is_listchars && strcmp(tab[i].name, "leadmultispace") == 0)
4874+
if (is_listchars && STRCMP(tab[i].name.string, "leadmultispace") == 0)
48684875
{
4869-
s = p + len + 1;
48704876
if (round == 0)
48714877
{
48724878
// get length of lcs-leadmultispace string in first
@@ -4879,14 +4885,14 @@ set_chars_option(win_T *wp, char_u *value, int is_listchars, int apply,
48794885
if (char2cells(c1) > 1)
48804886
return field_value_err(errbuf, errbuflen,
48814887
e_wrong_character_width_for_field_str,
4882-
tab[i].name);
4888+
tab[i].name.string);
48834889
++lead_multispace_len;
48844890
}
48854891
if (lead_multispace_len == 0)
48864892
// lcs-leadmultispace cannot be an empty string
48874893
return field_value_err(errbuf, errbuflen,
48884894
e_wrong_number_of_characters_for_field_str,
4889-
tab[i].name);
4895+
tab[i].name.string);
48904896
p = s;
48914897
}
48924898
else
@@ -4896,7 +4902,7 @@ set_chars_option(win_T *wp, char_u *value, int is_listchars, int apply,
48964902
while (*s != NUL && *s != ',')
48974903
{
48984904
c1 = get_encoded_char_adv(&s);
4899-
if (p == last_lmultispace)
4905+
if (p == last_lmultispace && lcs_chars.leadmultispace != NULL)
49004906
lcs_chars.leadmultispace[multispace_pos++] = c1;
49014907
}
49024908
p = s;
@@ -4905,34 +4911,33 @@ set_chars_option(win_T *wp, char_u *value, int is_listchars, int apply,
49054911
}
49064912

49074913
c2 = c3 = 0;
4908-
s = p + len + 1;
49094914
if (*s == NUL)
49104915
return field_value_err(errbuf, errbuflen,
49114916
e_wrong_number_of_characters_for_field_str,
4912-
tab[i].name);
4917+
tab[i].name.string);
49134918
c1 = get_encoded_char_adv(&s);
49144919
if (char2cells(c1) > 1)
49154920
return field_value_err(errbuf, errbuflen,
49164921
e_wrong_character_width_for_field_str,
4917-
tab[i].name);
4922+
tab[i].name.string);
49184923
if (tab[i].cp == &lcs_chars.tab2)
49194924
{
49204925
if (*s == NUL)
49214926
return field_value_err(errbuf, errbuflen,
49224927
e_wrong_number_of_characters_for_field_str,
4923-
tab[i].name);
4928+
tab[i].name.string);
49244929
c2 = get_encoded_char_adv(&s);
49254930
if (char2cells(c2) > 1)
49264931
return field_value_err(errbuf, errbuflen,
49274932
e_wrong_character_width_for_field_str,
4928-
tab[i].name);
4933+
tab[i].name.string);
49294934
if (!(*s == ',' || *s == NUL))
49304935
{
49314936
c3 = get_encoded_char_adv(&s);
49324937
if (char2cells(c3) > 1)
49334938
return field_value_err(errbuf, errbuflen,
49344939
e_wrong_character_width_for_field_str,
4935-
tab[i].name);
4940+
tab[i].name.string);
49364941
}
49374942
}
49384943

@@ -4956,7 +4961,7 @@ set_chars_option(win_T *wp, char_u *value, int is_listchars, int apply,
49564961
else
49574962
return field_value_err(errbuf, errbuflen,
49584963
e_wrong_number_of_characters_for_field_str,
4959-
tab[i].name);
4964+
tab[i].name.string);
49604965
}
49614966

49624967
if (i == entries)
@@ -5011,10 +5016,10 @@ set_listchars_option(win_T *wp, char_u *val, int apply, char *errbuf,
50115016
char_u *
50125017
get_fillchars_name(expand_T *xp UNUSED, int idx)
50135018
{
5014-
if (idx >= (int)(sizeof(filltab) / sizeof(filltab[0])))
5019+
if (idx < 0 || idx >= (int)ARRAY_LENGTH(filltab))
50155020
return NULL;
50165021

5017-
return (char_u*)filltab[idx].name;
5022+
return filltab[idx].name.string;
50185023
}
50195024

50205025
/*
@@ -5024,10 +5029,10 @@ get_fillchars_name(expand_T *xp UNUSED, int idx)
50245029
char_u *
50255030
get_listchars_name(expand_T *xp UNUSED, int idx)
50265031
{
5027-
if (idx >= (int)(sizeof(lcstab) / sizeof(lcstab[0])))
5032+
if (idx < 0 || idx >= (int)ARRAY_LENGTH(lcstab))
50285033
return NULL;
50295034

5030-
return (char_u*)lcstab[idx].name;
5035+
return lcstab[idx].name.string;
50315036
}
50325037

50335038
/*

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,8 @@ static char *(features[]) =
704704

705705
static int included_patches[] =
706706
{ /* Add new patch number below this line */
707+
/**/
708+
1028,
707709
/**/
708710
1027,
709711
/**/

0 commit comments

Comments
 (0)