0
0
mirror of https://github.com/vim/vim.git synced 2025-07-26 11:04:33 -04:00

patch 8.1.1827: allocating more memory than needed for extended structs

Problem:    Allocating more memory than needed for extended structs.
Solution:   Use offsetof() instead of sizeof(). (Dominique Pelle,
            closes #4786)
This commit is contained in:
Bram Moolenaar 2019-08-08 20:49:14 +02:00
parent 8c5a278fc5
commit 47ed553fd5
8 changed files with 11 additions and 9 deletions

View File

@ -232,7 +232,7 @@ add_buff(
len = MINIMAL_SIZE; len = MINIMAL_SIZE;
else else
len = slen; len = slen;
p = alloc(sizeof(buffblock_T) + len); p = alloc(offsetof(buffblock_T, b_str) + len + 1);
if (p == NULL) if (p == NULL)
return; /* no space, just forget it */ return; /* no space, just forget it */
buf->bh_space = (int)(len - slen); buf->bh_space = (int)(len - slen);

View File

@ -1319,7 +1319,7 @@ bt_regcomp(char_u *expr, int re_flags)
return NULL; return NULL;
/* Allocate space. */ /* Allocate space. */
r = alloc(sizeof(bt_regprog_T) + regsize); r = alloc(offsetof(bt_regprog_T, program) + regsize);
if (r == NULL) if (r == NULL)
return NULL; return NULL;
r->re_in_use = FALSE; r->re_in_use = FALSE;

View File

@ -85,7 +85,7 @@ sign_group_ref(char_u *groupname)
if (HASHITEM_EMPTY(hi)) if (HASHITEM_EMPTY(hi))
{ {
// new group // new group
group = alloc(sizeof(signgroup_T) + STRLEN(groupname)); group = alloc(offsetof(signgroup_T, sg_name) + STRLEN(groupname) + 1);
if (group == NULL) if (group == NULL)
return NULL; return NULL;
STRCPY(group->sg_name, groupname); STRCPY(group->sg_name, groupname);

View File

@ -742,9 +742,9 @@ typedef struct proptype_S
// Sign group // Sign group
typedef struct signgroup_S typedef struct signgroup_S
{ {
short_u refcount; // number of signs in this group
int next_sign_id; // next sign id for this group int next_sign_id; // next sign id for this group
char_u sg_name[1]; // sign group name short_u refcount; // number of signs in this group
char_u sg_name[1]; // sign group name, actually longer
} signgroup_T; } signgroup_T;
typedef struct signlist signlist_T; typedef struct signlist signlist_T;

View File

@ -4394,7 +4394,7 @@ add_keyword(
name_folded, MAXKEYWLEN + 1); name_folded, MAXKEYWLEN + 1);
else else
name_ic = name; name_ic = name;
kp = alloc(sizeof(keyentry_T) + STRLEN(name_ic)); kp = alloc(offsetof(keyentry_T, keyword) + STRLEN(name_ic) + 1);
if (kp == NULL) if (kp == NULL)
return; return;
STRCPY(kp->keyword, name_ic); STRCPY(kp->keyword, name_ic);

View File

@ -695,7 +695,7 @@ prop_type_set(typval_T *argvars, int add)
semsg(_("E969: Property type %s already defined"), name); semsg(_("E969: Property type %s already defined"), name);
return; return;
} }
prop = alloc_clear(sizeof(proptype_T) + STRLEN(name)); prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
if (prop == NULL) if (prop == NULL)
return; return;
STRCPY(prop->pt_name, name); STRCPY(prop->pt_name, name);

View File

@ -288,7 +288,7 @@ get_lambda_tv(char_u **arg, typval_T *rettv, int evaluate)
sprintf((char*)name, "<lambda>%d", ++lambda_no); sprintf((char*)name, "<lambda>%d", ++lambda_no);
fp = alloc_clear(sizeof(ufunc_T) + STRLEN(name)); fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
if (fp == NULL) if (fp == NULL)
goto errret; goto errret;
pt = ALLOC_CLEAR_ONE(partial_T); pt = ALLOC_CLEAR_ONE(partial_T);
@ -2631,7 +2631,7 @@ ex_function(exarg_T *eap)
} }
} }
fp = alloc_clear(sizeof(ufunc_T) + STRLEN(name)); fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
if (fp == NULL) if (fp == NULL)
goto erret; goto erret;

View File

@ -769,6 +769,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
1827,
/**/ /**/
1826, 1826,
/**/ /**/