vim-patch:8.1.1843: might be freeing memory that was not allocated (#10756)

Problem:    Might be freeing memory that was not allocated.
Solution:   Have next_fenc() set the fenc_alloced flag. (closes vim/vim#4804)
f077db2423
This commit is contained in:
Jan Edmund Lazo 2019-08-13 10:46:26 -04:00 committed by Daniel Hahler
parent 90e44ecf11
commit 5e1acd412b

View File

@ -777,9 +777,8 @@ readfile(
fenc = curbuf->b_p_fenc; // use format from buffer fenc = curbuf->b_p_fenc; // use format from buffer
fenc_alloced = false; fenc_alloced = false;
} else { } else {
fenc_next = p_fencs; /* try items in 'fileencodings' */ fenc_next = p_fencs; // try items in 'fileencodings'
fenc = next_fenc(&fenc_next); fenc = next_fenc(&fenc_next, &fenc_alloced);
fenc_alloced = true;
} }
/* /*
@ -869,8 +868,7 @@ retry:
if (fenc_alloced) if (fenc_alloced)
xfree(fenc); xfree(fenc);
if (fenc_next != NULL) { if (fenc_next != NULL) {
fenc = next_fenc(&fenc_next); fenc = next_fenc(&fenc_next, &fenc_alloced);
fenc_alloced = (fenc_next != NULL);
} else { } else {
fenc = (char_u *)""; fenc = (char_u *)"";
fenc_alloced = false; fenc_alloced = false;
@ -2082,19 +2080,19 @@ void set_forced_fenc(exarg_T *eap)
} }
} }
/* // Find next fileencoding to use from 'fileencodings'.
* Find next fileencoding to use from 'fileencodings'. // "pp" points to fenc_next. It's advanced to the next item.
* "pp" points to fenc_next. It's advanced to the next item. // When there are no more items, an empty string is returned and *pp is set to
* When there are no more items, an empty string is returned and *pp is set to // NULL.
* NULL. // When *pp is not set to NULL, the result is in allocated memory and "alloced"
* When *pp is not set to NULL, the result is in allocated memory. // is set to true.
*/ static char_u *next_fenc(char_u **pp, bool *alloced)
static char_u *next_fenc(char_u **pp)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_RET FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_RET
{ {
char_u *p; char_u *p;
char_u *r; char_u *r;
*alloced = false;
if (**pp == NUL) { if (**pp == NUL) {
*pp = NULL; *pp = NULL;
return (char_u *)""; return (char_u *)"";
@ -2110,6 +2108,7 @@ static char_u *next_fenc(char_u **pp)
xfree(r); xfree(r);
r = p; r = p;
} }
*alloced = true;
return r; return r;
} }