Skip to content

Commit 1b438a8

Browse files
zeertzjqbrammool
authored andcommitted
patch 9.0.1271: using sizeof() and subtract array size is tricky
Problem: Using sizeof() and subtract array size is tricky. Solution: Use offsetof() instead. (closes #11926)
1 parent a7d36b7 commit 1b438a8

10 files changed

Lines changed: 15 additions & 11 deletions

File tree

src/evalvars.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3960,7 +3960,7 @@ set_var_const(
39603960
|| STRNCMP(name, "g:", 2) == 0 || var_in_autoload))
39613961
goto failed;
39623962

3963-
di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3963+
di = alloc(offsetof(dictitem_T, di_key) + STRLEN(varname) + 1);
39643964
if (di == NULL)
39653965
goto failed;
39663966
STRCPY(di->di_key, varname);

src/findfile.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1344,7 +1344,8 @@ ff_check_visited(
13441344
/*
13451345
* New file/dir. Add it to the list of visited files/dirs.
13461346
*/
1347-
vp = alloc(sizeof(ff_visited_T) + STRLEN(ff_expand_buffer));
1347+
vp = alloc(
1348+
offsetof(ff_visited_T, ffv_fname) + STRLEN(ff_expand_buffer) + 1);
13481349
if (vp == NULL)
13491350
return OK;
13501351

src/memline.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ struct data_block
130130
#define DB_INDEX_MASK (~DB_MARKED)
131131

132132
#define INDEX_SIZE (sizeof(unsigned)) // size of one db_index entry
133-
#define HEADER_SIZE (sizeof(DATA_BL) - INDEX_SIZE) // size of data block header
133+
#define HEADER_SIZE (offsetof(DATA_BL, db_index)) // size of data block header
134134

135135
#define B0_FNAME_SIZE_ORG 900 // what it was in older versions
136136
#define B0_FNAME_SIZE_NOCRYPT 898 // 2 bytes used for other things
@@ -4162,8 +4162,9 @@ ml_new_ptr(memfile_T *mfp)
41624162
pp = (PTR_BL *)(hp->bh_data);
41634163
pp->pb_id = PTR_ID;
41644164
pp->pb_count = 0;
4165-
pp->pb_count_max = (short_u)((mfp->mf_page_size - sizeof(PTR_BL))
4166-
/ sizeof(PTR_EN) + 1);
4165+
pp->pb_count_max =
4166+
(short_u)((mfp->mf_page_size - offsetof(PTR_BL, pb_pointer))
4167+
/ sizeof(PTR_EN));
41674168

41684169
return hp;
41694170
}

src/message.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2739,7 +2739,7 @@ store_sb_text(
27392739

27402740
if (s > *sb_str)
27412741
{
2742-
mp = alloc(sizeof(msgchunk_T) + (s - *sb_str));
2742+
mp = alloc(offsetof(msgchunk_T, sb_text) + (s - *sb_str) + 1);
27432743
if (mp != NULL)
27442744
{
27452745
mp->sb_eol = finish;

src/regexp_nfa.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7505,7 +7505,7 @@ nfa_regcomp(char_u *expr, int re_flags)
75057505
post2nfa(postfix, post_ptr, TRUE);
75067506

75077507
// allocate the regprog with space for the compiled regexp
7508-
prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1);
7508+
prog_size = offsetof(nfa_regprog_T, state) + sizeof(nfa_state_T) * nstate;
75097509
prog = alloc(prog_size);
75107510
if (prog == NULL)
75117511
goto fail;

src/spell.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1848,7 +1848,7 @@ count_common_word(
18481848
hi = hash_lookup(&lp->sl_wordcount, p, hash);
18491849
if (HASHITEM_EMPTY(hi))
18501850
{
1851-
wc = alloc(sizeof(wordcount_T) + STRLEN(p));
1851+
wc = alloc(offsetof(wordcount_T, wc_word) + STRLEN(p) + 1);
18521852
if (wc == NULL)
18531853
return;
18541854
STRCPY(wc->wc_word, p);

src/spellfile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4305,7 +4305,7 @@ getroom(
43054305
bl = NULL;
43064306
else
43074307
// Allocate a block of memory. It is not freed until much later.
4308-
bl = alloc_clear(sizeof(sblock_T) + SBLOCKSIZE);
4308+
bl = alloc_clear(offsetof(sblock_T, sb_data) + SBLOCKSIZE + 1);
43094309
if (bl == NULL)
43104310
{
43114311
if (!spin->si_did_emsg)

src/spellsuggest.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3228,7 +3228,7 @@ add_sound_suggest(
32283228
hi = hash_lookup(&slang->sl_sounddone, goodword, hash);
32293229
if (HASHITEM_EMPTY(hi))
32303230
{
3231-
sft = alloc(sizeof(sftword_T) + STRLEN(goodword));
3231+
sft = alloc(offsetof(sftword_T, sft_word) + STRLEN(goodword) + 1);
32323232
if (sft != NULL)
32333233
{
32343234
sft->sft_score = score;

src/version.c

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

696696
static int included_patches[] =
697697
{ /* Add new patch number below this line */
698+
/**/
699+
1271,
698700
/**/
699701
1270,
700702
/**/

src/vim9script.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,7 @@ update_vim9_script_var(
922922
// svar_T and create a new sallvar_T.
923923
sv = ((svar_T *)si->sn_var_vals.ga_data) + si->sn_var_vals.ga_len;
924924
newsav = (sallvar_T *)alloc_clear(
925-
sizeof(sallvar_T) + STRLEN(name));
925+
offsetof(sallvar_T, sav_key) + STRLEN(name) + 1);
926926
if (newsav == NULL)
927927
return;
928928

0 commit comments

Comments
 (0)