]> granicus.if.org Git - vim/commitdiff
patch 9.0.1271: using sizeof() and subtract array size is tricky v9.0.1271
authorzeertzjq <zeertzjq@outlook.com>
Wed, 1 Feb 2023 13:11:15 +0000 (13:11 +0000)
committerBram Moolenaar <Bram@vim.org>
Wed, 1 Feb 2023 13:11:15 +0000 (13:11 +0000)
Problem:    Using sizeof() and subtract array size is tricky.
Solution:   Use offsetof() instead. (closes #11926)

src/evalvars.c
src/findfile.c
src/memline.c
src/message.c
src/regexp_nfa.c
src/spell.c
src/spellfile.c
src/spellsuggest.c
src/version.c
src/vim9script.c

index 066543669b1b8d3d5aa65521fc84223b87b17da8..b2343c0a2b3c80c1cd8b7488c4cfd193bbf8c22f 100644 (file)
@@ -3960,7 +3960,7 @@ set_var_const(
                        || STRNCMP(name, "g:", 2) == 0 || var_in_autoload))
            goto failed;
 
-       di = alloc(sizeof(dictitem_T) + STRLEN(varname));
+       di = alloc(offsetof(dictitem_T, di_key) + STRLEN(varname) + 1);
        if (di == NULL)
            goto failed;
        STRCPY(di->di_key, varname);
index 4e2ad774c6a1ffe1d25bcaea7fed108c1f6a3e6d..e0d9d6638252076db67292c83e978401122d5d4b 100644 (file)
@@ -1344,7 +1344,8 @@ ff_check_visited(
     /*
      * New file/dir.  Add it to the list of visited files/dirs.
      */
-    vp = alloc(sizeof(ff_visited_T) + STRLEN(ff_expand_buffer));
+    vp = alloc(
+            offsetof(ff_visited_T, ffv_fname) + STRLEN(ff_expand_buffer) + 1);
     if (vp == NULL)
        return OK;
 
index 7cbe559e65bb62112d2737045e83a4c43325355c..7acea1346c67f8036e36ae9329444345d3b0f1e4 100644 (file)
@@ -130,7 +130,7 @@ struct data_block
 #define DB_INDEX_MASK  (~DB_MARKED)
 
 #define INDEX_SIZE  (sizeof(unsigned))     // size of one db_index entry
-#define HEADER_SIZE (sizeof(DATA_BL) - INDEX_SIZE)  // size of data block header
+#define HEADER_SIZE (offsetof(DATA_BL, db_index))  // size of data block header
 
 #define B0_FNAME_SIZE_ORG      900     // what it was in older versions
 #define B0_FNAME_SIZE_NOCRYPT  898     // 2 bytes used for other things
@@ -4162,8 +4162,9 @@ ml_new_ptr(memfile_T *mfp)
     pp = (PTR_BL *)(hp->bh_data);
     pp->pb_id = PTR_ID;
     pp->pb_count = 0;
-    pp->pb_count_max = (short_u)((mfp->mf_page_size - sizeof(PTR_BL))
-                                                       / sizeof(PTR_EN) + 1);
+    pp->pb_count_max =
+       (short_u)((mfp->mf_page_size - offsetof(PTR_BL, pb_pointer))
+                                                            / sizeof(PTR_EN));
 
     return hp;
 }
index d7b92635716c44db3ffafbf27fb306d959a12de5..b8e5eb95f23ae83738edee42efeeb19b0fa78419 100644 (file)
@@ -2739,7 +2739,7 @@ store_sb_text(
 
     if (s > *sb_str)
     {
-       mp = alloc(sizeof(msgchunk_T) + (s - *sb_str));
+       mp = alloc(offsetof(msgchunk_T, sb_text) + (s - *sb_str) + 1);
        if (mp != NULL)
        {
            mp->sb_eol = finish;
index 92475ad94a06991c5a0169340c965b1615e5e4fc..cc29a4c3e2fed92e556217ef55127c512a32873f 100644 (file)
@@ -7505,7 +7505,7 @@ nfa_regcomp(char_u *expr, int re_flags)
     post2nfa(postfix, post_ptr, TRUE);
 
     // allocate the regprog with space for the compiled regexp
-    prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1);
+    prog_size = offsetof(nfa_regprog_T, state) + sizeof(nfa_state_T) * nstate;
     prog = alloc(prog_size);
     if (prog == NULL)
        goto fail;
index 5f8332208a727c5b9c2db953687dacc63f92bbf8..072740cbaaef3ba0bd4e1763b28031b226b7a7b1 100644 (file)
@@ -1848,7 +1848,7 @@ count_common_word(
     hi = hash_lookup(&lp->sl_wordcount, p, hash);
     if (HASHITEM_EMPTY(hi))
     {
-       wc = alloc(sizeof(wordcount_T) + STRLEN(p));
+       wc = alloc(offsetof(wordcount_T, wc_word) + STRLEN(p) + 1);
        if (wc == NULL)
            return;
        STRCPY(wc->wc_word, p);
index 710e4388f65ce6678900d93b19947d3e03eb459b..bf51f5fd6936cb0b12431415d41d9d3941d9fa0f 100644 (file)
@@ -4305,7 +4305,7 @@ getroom(
            bl = NULL;
        else
            // Allocate a block of memory. It is not freed until much later.
-           bl = alloc_clear(sizeof(sblock_T) + SBLOCKSIZE);
+           bl = alloc_clear(offsetof(sblock_T, sb_data) + SBLOCKSIZE + 1);
        if (bl == NULL)
        {
            if (!spin->si_did_emsg)
index 5cb576f2b1960d57bb6d2e882efe60895e57ed5c..6b34ec8165d8b3b8fc20cb99afead91e0569b816 100644 (file)
@@ -3228,7 +3228,7 @@ add_sound_suggest(
     hi = hash_lookup(&slang->sl_sounddone, goodword, hash);
     if (HASHITEM_EMPTY(hi))
     {
-       sft = alloc(sizeof(sftword_T) + STRLEN(goodword));
+       sft = alloc(offsetof(sftword_T, sft_word) + STRLEN(goodword) + 1);
        if (sft != NULL)
        {
            sft->sft_score = score;
index a79a9d0b58d726160669c45842020cce7994a6d8..68c47a32d752121d6757b65c8e487736fffec645 100644 (file)
@@ -695,6 +695,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1271,
 /**/
     1270,
 /**/
index fd4658c6f03cb90f285f69c3f4ad84902856c688..b946fb918ac2a9ae878b0545f138f73edee4a4e1 100644 (file)
@@ -922,7 +922,7 @@ update_vim9_script_var(
            // svar_T and create a new sallvar_T.
            sv = ((svar_T *)si->sn_var_vals.ga_data) + si->sn_var_vals.ga_len;
            newsav = (sallvar_T *)alloc_clear(
-                                      sizeof(sallvar_T) + STRLEN(name));
+                             offsetof(sallvar_T, sav_key) + STRLEN(name) + 1);
            if (newsav == NULL)
                return;