To: vim_dev@googlegroups.com Subject: Patch 8.1.1827 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 8.1.1827 Problem: Allocating more memory than needed for extended structs. Solution: Use offsetof() instead of sizeof(). (Dominique Pelle, closes #4786) Files: src/getchar.c, src/regexp.c, src/sign.c, src/structs.h, src/syntax.c, src/textprop.c, src/userfunc.c *** ../vim-8.1.1826/src/getchar.c 2019-08-03 16:18:03.429654593 +0200 --- src/getchar.c 2019-08-08 20:25:42.524843750 +0200 *************** *** 232,238 **** len = MINIMAL_SIZE; else len = slen; ! p = alloc(sizeof(buffblock_T) + len); if (p == NULL) return; /* no space, just forget it */ buf->bh_space = (int)(len - slen); --- 232,238 ---- len = MINIMAL_SIZE; else len = slen; ! p = alloc(offsetof(buffblock_T, b_str) + len + 1); if (p == NULL) return; /* no space, just forget it */ buf->bh_space = (int)(len - slen); *** ../vim-8.1.1826/src/regexp.c 2019-08-03 18:17:07.680638632 +0200 --- src/regexp.c 2019-08-08 20:25:42.524843750 +0200 *************** *** 1319,1325 **** return NULL; /* Allocate space. */ ! r = alloc(sizeof(bt_regprog_T) + regsize); if (r == NULL) return NULL; r->re_in_use = FALSE; --- 1319,1325 ---- return NULL; /* Allocate space. */ ! r = alloc(offsetof(bt_regprog_T, program) + regsize); if (r == NULL) return NULL; r->re_in_use = FALSE; *** ../vim-8.1.1826/src/sign.c 2019-07-20 16:51:14.343605469 +0200 --- src/sign.c 2019-08-08 20:25:42.524843750 +0200 *************** *** 85,91 **** if (HASHITEM_EMPTY(hi)) { // new group ! group = alloc(sizeof(signgroup_T) + STRLEN(groupname)); if (group == NULL) return NULL; STRCPY(group->sg_name, groupname); --- 85,91 ---- if (HASHITEM_EMPTY(hi)) { // new group ! group = alloc(offsetof(signgroup_T, sg_name) + STRLEN(groupname) + 1); if (group == NULL) return NULL; STRCPY(group->sg_name, groupname); *** ../vim-8.1.1826/src/structs.h 2019-08-03 21:58:17.753476626 +0200 --- src/structs.h 2019-08-08 20:25:42.524843750 +0200 *************** *** 742,750 **** // Sign group typedef struct signgroup_S { - short_u refcount; // number of signs in this group int next_sign_id; // next sign id for this group ! char_u sg_name[1]; // sign group name } signgroup_T; typedef struct signlist signlist_T; --- 742,750 ---- // Sign group typedef struct signgroup_S { int next_sign_id; // next sign id for this group ! short_u refcount; // number of signs in this group ! char_u sg_name[1]; // sign group name, actually longer } signgroup_T; typedef struct signlist signlist_T; *** ../vim-8.1.1826/src/syntax.c 2019-07-15 22:40:19.061741740 +0200 --- src/syntax.c 2019-08-08 20:25:42.524843750 +0200 *************** *** 4394,4400 **** name_folded, MAXKEYWLEN + 1); else name_ic = name; ! kp = alloc(sizeof(keyentry_T) + STRLEN(name_ic)); if (kp == NULL) return; STRCPY(kp->keyword, name_ic); --- 4394,4400 ---- name_folded, MAXKEYWLEN + 1); else name_ic = name; ! kp = alloc(offsetof(keyentry_T, keyword) + STRLEN(name_ic) + 1); if (kp == NULL) return; STRCPY(kp->keyword, name_ic); *** ../vim-8.1.1826/src/textprop.c 2019-06-04 19:16:25.518124894 +0200 --- src/textprop.c 2019-08-08 20:25:42.524843750 +0200 *************** *** 695,701 **** semsg(_("E969: Property type %s already defined"), name); return; } ! prop = alloc_clear(sizeof(proptype_T) + STRLEN(name)); if (prop == NULL) return; STRCPY(prop->pt_name, name); --- 695,701 ---- semsg(_("E969: Property type %s already defined"), name); return; } ! prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1); if (prop == NULL) return; STRCPY(prop->pt_name, name); *** ../vim-8.1.1826/src/userfunc.c 2019-08-05 23:10:06.661422313 +0200 --- src/userfunc.c 2019-08-08 20:25:42.528843730 +0200 *************** *** 288,294 **** sprintf((char*)name, "%d", ++lambda_no); ! fp = alloc_clear(sizeof(ufunc_T) + STRLEN(name)); if (fp == NULL) goto errret; pt = ALLOC_CLEAR_ONE(partial_T); --- 288,294 ---- sprintf((char*)name, "%d", ++lambda_no); ! fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1); if (fp == NULL) goto errret; pt = ALLOC_CLEAR_ONE(partial_T); *************** *** 2631,2637 **** } } ! fp = alloc_clear(sizeof(ufunc_T) + STRLEN(name)); if (fp == NULL) goto erret; --- 2631,2637 ---- } } ! fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1); if (fp == NULL) goto erret; *** ../vim-8.1.1826/src/version.c 2019-08-07 23:07:03.964858688 +0200 --- src/version.c 2019-08-08 20:48:34.783611555 +0200 *************** *** 771,772 **** --- 771,774 ---- { /* Add new patch number below this line */ + /**/ + 1827, /**/ -- hundred-and-one symptoms of being an internet addict: 30. Even though you died last week, you've managed to retain OPS on your favorite IRC channel. /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\ \\\ an exciting new programming language -- http://www.Zimbu.org /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///