Patch 9.0.1271
Problem:    Using sizeof() and subtract array size is tricky.
Solution:   Use offsetof() instead. (closes #11926)
Files:      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/vim9script.c


*** ../vim-9.0.1270/src/evalvars.c      2023-01-30 21:12:30.547422897 +0000
--- src/evalvars.c      2023-02-01 13:08:54.395217208 +0000
***************
*** 3960,3966 ****
                        || STRNCMP(name, "g:", 2) == 0 || var_in_autoload))
            goto failed;
  
!       di = alloc(sizeof(dictitem_T) + STRLEN(varname));
        if (di == NULL)
            goto failed;
        STRCPY(di->di_key, varname);
--- 3960,3966 ----
                        || STRNCMP(name, "g:", 2) == 0 || var_in_autoload))
            goto failed;
  
!       di = alloc(offsetof(dictitem_T, di_key) + STRLEN(varname) + 1);
        if (di == NULL)
            goto failed;
        STRCPY(di->di_key, varname);
*** ../vim-9.0.1270/src/findfile.c      2023-01-22 18:38:45.498261340 +0000
--- src/findfile.c      2023-02-01 13:08:54.395217208 +0000
***************
*** 1344,1350 ****
      /*
       * New file/dir.  Add it to the list of visited files/dirs.
       */
!     vp = alloc(sizeof(ff_visited_T) + STRLEN(ff_expand_buffer));
      if (vp == NULL)
        return OK;
  
--- 1344,1351 ----
      /*
       * New file/dir.  Add it to the list of visited files/dirs.
       */
!     vp = alloc(
!            offsetof(ff_visited_T, ffv_fname) + STRLEN(ff_expand_buffer) + 1);
      if (vp == NULL)
        return OK;
  
*** ../vim-9.0.1270/src/memline.c       2023-01-14 12:32:24.219984103 +0000
--- src/memline.c       2023-02-01 13:08:54.399217211 +0000
***************
*** 130,136 ****
  #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 B0_FNAME_SIZE_ORG     900     // what it was in older versions
  #define B0_FNAME_SIZE_NOCRYPT 898     // 2 bytes used for other things
--- 130,136 ----
  #define DB_INDEX_MASK (~DB_MARKED)
  
  #define INDEX_SIZE  (sizeof(unsigned))            // size of one db_index 
entry
! #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,4169 ****
      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);
  
      return hp;
  }
--- 4162,4170 ----
      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 - offsetof(PTR_BL, pb_pointer))
!                                                            / sizeof(PTR_EN));
  
      return hp;
  }
*** ../vim-9.0.1270/src/message.c       2023-01-14 12:32:24.219984103 +0000
--- src/message.c       2023-02-01 13:08:54.399217211 +0000
***************
*** 2739,2745 ****
  
      if (s > *sb_str)
      {
!       mp = alloc(sizeof(msgchunk_T) + (s - *sb_str));
        if (mp != NULL)
        {
            mp->sb_eol = finish;
--- 2739,2745 ----
  
      if (s > *sb_str)
      {
!       mp = alloc(offsetof(msgchunk_T, sb_text) + (s - *sb_str) + 1);
        if (mp != NULL)
        {
            mp->sb_eol = finish;
*** ../vim-9.0.1270/src/regexp_nfa.c    2023-01-22 21:14:32.621863614 +0000
--- src/regexp_nfa.c    2023-02-01 13:08:54.399217211 +0000
***************
*** 7505,7511 ****
      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 = alloc(prog_size);
      if (prog == NULL)
        goto fail;
--- 7505,7511 ----
      post2nfa(postfix, post_ptr, TRUE);
  
      // allocate the regprog with space for the compiled regexp
!     prog_size = offsetof(nfa_regprog_T, state) + sizeof(nfa_state_T) * nstate;
      prog = alloc(prog_size);
      if (prog == NULL)
        goto fail;
*** ../vim-9.0.1270/src/spell.c 2023-01-30 13:04:38.261749833 +0000
--- src/spell.c 2023-02-01 13:08:54.399217211 +0000
***************
*** 1848,1854 ****
      hi = hash_lookup(&lp->sl_wordcount, p, hash);
      if (HASHITEM_EMPTY(hi))
      {
!       wc = alloc(sizeof(wordcount_T) + STRLEN(p));
        if (wc == NULL)
            return;
        STRCPY(wc->wc_word, p);
--- 1848,1854 ----
      hi = hash_lookup(&lp->sl_wordcount, p, hash);
      if (HASHITEM_EMPTY(hi))
      {
!       wc = alloc(offsetof(wordcount_T, wc_word) + STRLEN(p) + 1);
        if (wc == NULL)
            return;
        STRCPY(wc->wc_word, p);
*** ../vim-9.0.1270/src/spellfile.c     2023-01-27 21:03:08.899101847 +0000
--- src/spellfile.c     2023-02-01 13:08:54.399217211 +0000
***************
*** 4305,4311 ****
            bl = NULL;
        else
            // Allocate a block of memory. It is not freed until much later.
!           bl = alloc_clear(sizeof(sblock_T) + SBLOCKSIZE);
        if (bl == NULL)
        {
            if (!spin->si_did_emsg)
--- 4305,4311 ----
            bl = NULL;
        else
            // Allocate a block of memory. It is not freed until much later.
!           bl = alloc_clear(offsetof(sblock_T, sb_data) + SBLOCKSIZE + 1);
        if (bl == NULL)
        {
            if (!spin->si_did_emsg)
*** ../vim-9.0.1270/src/spellsuggest.c  2023-01-23 20:46:16.166493150 +0000
--- src/spellsuggest.c  2023-02-01 13:08:54.399217211 +0000
***************
*** 3228,3234 ****
      hi = hash_lookup(&slang->sl_sounddone, goodword, hash);
      if (HASHITEM_EMPTY(hi))
      {
!       sft = alloc(sizeof(sftword_T) + STRLEN(goodword));
        if (sft != NULL)
        {
            sft->sft_score = score;
--- 3228,3234 ----
      hi = hash_lookup(&slang->sl_sounddone, goodword, hash);
      if (HASHITEM_EMPTY(hi))
      {
!       sft = alloc(offsetof(sftword_T, sft_word) + STRLEN(goodword) + 1);
        if (sft != NULL)
        {
            sft->sft_score = score;
*** ../vim-9.0.1270/src/vim9script.c    2023-01-26 11:58:39.610071592 +0000
--- src/vim9script.c    2023-02-01 13:08:54.399217211 +0000
***************
*** 922,928 ****
            // 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));
            if (newsav == NULL)
                return;
  
--- 922,928 ----
            // 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(
!                             offsetof(sallvar_T, sav_key) + STRLEN(name) + 1);
            if (newsav == NULL)
                return;
  
*** ../vim-9.0.1270/src/version.c       2023-01-31 21:13:35.070100023 +0000
--- src/version.c       2023-02-01 13:10:27.411112704 +0000
***************
*** 697,698 ****
--- 697,700 ----
  {   /* Add new patch number below this line */
+ /**/
+     1271,
  /**/

-- 
Ed's Radiator Shop: The Best Place in Town to Take a Leak.

 /// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net   \\\
///                                                                      \\\
\\\        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/20230201131148.9A4301C0903%40moolenaar.net.

Raspunde prin e-mail lui