Skip to content

Commit bbd75ce

Browse files
committed
Merge #804 'Coverity fix resource leaks 1b'
2 parents f39fd5b + 70f28d9 commit bbd75ce

File tree

5 files changed

+109
-84
lines changed

5 files changed

+109
-84
lines changed

src/nvim/memline.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,7 @@ int ml_open(buf_T *buf)
304304
b0p->b0_magic_int = (int)B0_MAGIC_INT;
305305
b0p->b0_magic_short = (short)B0_MAGIC_SHORT;
306306
b0p->b0_magic_char = B0_MAGIC_CHAR;
307-
STRNCPY(b0p->b0_version, "VIM ", 4);
308-
STRNCPY(b0p->b0_version + 4, Version, 6);
307+
xstrlcpy(xstpcpy((char *) b0p->b0_version, "VIM "), Version, 6);
309308
long_to_char((long)mfp->mf_page_size, b0p->b0_page_size);
310309

311310
if (!buf->b_spell) {

src/nvim/ops.c

+67-38
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ struct block_def {
9090
colnr_T start_char_vcols; /* number of vcols of pre-block char */
9191
};
9292

93-
9493
#ifdef INCLUDE_GENERATED_DECLARATIONS
9594
# include "ops.c.generated.h"
9695
#endif
96+
9797
/*
9898
* The names of operators.
9999
* IMPORTANT: Index must correspond with defines in vim.h!!!
@@ -4704,29 +4704,48 @@ get_reg_contents (
47044704
return retval;
47054705
}
47064706

4707-
/*
4708-
* Store string "str" in register "name".
4709-
* "maxlen" is the maximum number of bytes to use, -1 for all bytes.
4710-
* If "must_append" is TRUE, always append to the register. Otherwise append
4711-
* if "name" is an uppercase letter.
4712-
* Note: "maxlen" and "must_append" don't work for the "/" register.
4713-
* Careful: 'str' is modified, you may have to use a copy!
4714-
* If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
4715-
*/
4716-
void write_reg_contents(int name, char_u *str, int maxlen, int must_append)
4707+
/// write_reg_contents - store `str` in register `name`
4708+
///
4709+
/// @see write_reg_contents_ex
4710+
void write_reg_contents(int name,
4711+
const char_u *str,
4712+
ssize_t len,
4713+
int must_append)
47174714
{
4718-
write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
4715+
write_reg_contents_ex(name, str, len, must_append, MAUTO, 0L);
47194716
}
47204717

4721-
void write_reg_contents_ex(int name, char_u *str, int maxlen, int must_append, int yank_type, long block_len)
4718+
/// write_reg_contents_ex - store `str` in register `name`
4719+
///
4720+
/// If `str` ends in '\n' or '\r', use linewise, otherwise use
4721+
/// characterwise.
4722+
///
4723+
/// @warning when `name` is '/', `len` and `must_append` are ignored. This
4724+
/// means that `str` MUST be NUL-terminated.
4725+
///
4726+
/// @param name The name of the register
4727+
/// @param str The contents to write
4728+
/// @param len If >= 0, write `len` bytes of `str`. Otherwise, write
4729+
/// `strlen(str)` bytes. If `len` is larger than the
4730+
/// allocated size of `src`, the behaviour is undefined.
4731+
/// @param must_append If true, append the contents of `str` to the current
4732+
/// contents of the register. Note that regardless of
4733+
/// `must_append`, this function will append when `name`
4734+
/// is an uppercase letter.
4735+
/// @param yank_type MCHAR, MLINE, MBLOCK or MAUTO
4736+
/// @param block_len width of visual block
4737+
void write_reg_contents_ex(int name,
4738+
const char_u *str,
4739+
ssize_t len,
4740+
int must_append,
4741+
int yank_type,
4742+
long block_len)
47224743
{
47234744
struct yankreg *old_y_previous, *old_y_current;
4724-
long len;
47254745

4726-
if (maxlen >= 0)
4727-
len = maxlen;
4728-
else
4729-
len = (long)STRLEN(str);
4746+
if (len < 0) {
4747+
len = (ssize_t) STRLEN(str);
4748+
}
47304749

47314750
/* Special case: '/' search pattern */
47324751
if (name == '/') {
@@ -4735,17 +4754,25 @@ void write_reg_contents_ex(int name, char_u *str, int maxlen, int must_append, i
47354754
}
47364755

47374756
if (name == '=') {
4738-
char_u *p, *s;
4739-
4740-
p = vim_strnsave(str, (int)len);
4757+
size_t offset = 0;
4758+
size_t totlen = (size_t) len;
47414759

4742-
if (must_append) {
4743-
s = concat_str(get_expr_line_src(), p);
4744-
free(p);
4745-
p = s;
4760+
if (must_append && expr_line) {
4761+
// append has been specified and expr_line already exists, so we'll
4762+
// append the new string to expr_line.
4763+
size_t exprlen = STRLEN(expr_line);
47464764

4765+
totlen += exprlen;
4766+
offset = exprlen;
47474767
}
4748-
set_expr_line(p);
4768+
4769+
// modify the global expr_line, extend/shrink it if necessary (realloc).
4770+
// Copy the input string into the adjusted memory at the specified
4771+
// offset.
4772+
expr_line = xrealloc(expr_line, totlen + 1);
4773+
memcpy(expr_line + offset, str, (size_t) len);
4774+
expr_line[totlen] = NUL;
4775+
47494776
return;
47504777
}
47514778

@@ -4773,18 +4800,20 @@ void write_reg_contents_ex(int name, char_u *str, int maxlen, int must_append, i
47734800
y_current = old_y_current;
47744801
}
47754802

4776-
/*
4777-
* Put a string into a register. When the register is not empty, the string
4778-
* is appended.
4779-
*/
4780-
static void
4781-
str_to_reg (
4782-
struct yankreg *y_ptr, /* pointer to yank register */
4783-
int yank_type, /* MCHAR, MLINE, MBLOCK, MAUTO */
4784-
char_u *str, /* string to put in register */
4785-
long len, /* length of string */
4786-
long blocklen /* width of Visual block */
4787-
)
4803+
/// str_to_reg - Put a string into a register.
4804+
///
4805+
/// When the register is not empty, the string is appended.
4806+
///
4807+
/// @param y_ptr pointer to yank register
4808+
/// @param yank_type MCHAR, MLINE, MBLOCK or MAUTO
4809+
/// @param str string to put in register
4810+
/// @param len length of the string
4811+
/// @param blocklen width of visual block
4812+
static void str_to_reg(struct yankreg *y_ptr,
4813+
int yank_type,
4814+
const char_u *str,
4815+
long len,
4816+
long blocklen)
47884817
{
47894818
int type; /* MCHAR, MLINE or MBLOCK */
47904819
int lnum;

src/nvim/option.c

+11-12
Original file line numberDiff line numberDiff line change
@@ -2216,21 +2216,20 @@ set_options_default (
22162216
win_comp_scroll(wp);
22172217
}
22182218

2219-
/*
2220-
* Set the Vi-default value of a string option.
2221-
* Used for 'sh', 'backupskip' and 'term'.
2222-
*/
2223-
void set_string_default(char *name, char_u *val)
2219+
/// Set the Vi-default value of a string option.
2220+
/// Used for 'sh', 'backupskip' and 'term'.
2221+
///
2222+
/// @param name The name of the option
2223+
/// @param val The value of the option
2224+
void set_string_default(const char *name, const char_u *val)
22242225
{
2225-
char_u *p;
2226-
int opt_idx;
2227-
2228-
p = vim_strsave(val);
2229-
opt_idx = findoption((char_u *)name);
2226+
int opt_idx = findoption((char_u *)name);
22302227
if (opt_idx >= 0) {
2231-
if (options[opt_idx].flags & P_DEF_ALLOCED)
2228+
if (options[opt_idx].flags & P_DEF_ALLOCED) {
22322229
free(options[opt_idx].def_val[VI_DEFAULT]);
2233-
options[opt_idx].def_val[VI_DEFAULT] = p;
2230+
}
2231+
2232+
options[opt_idx].def_val[VI_DEFAULT] = (char_u *) xstrdup((char *) val);
22342233
options[opt_idx].flags |= P_DEF_ALLOCED;
22352234
}
22362235
}

src/nvim/search.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -375,14 +375,14 @@ void reset_search_dir(void)
375375
* Set the last search pattern. For ":let @/ =" and viminfo.
376376
* Also set the saved search pattern, so that this works in an autocommand.
377377
*/
378-
void set_last_search_pat(char_u *s, int idx, int magic, int setlast)
378+
void set_last_search_pat(const char_u *s, int idx, int magic, int setlast)
379379
{
380380
free(spats[idx].pat);
381381
/* An empty string means that nothing should be matched. */
382382
if (*s == NUL)
383383
spats[idx].pat = NULL;
384384
else
385-
spats[idx].pat = vim_strsave(s);
385+
spats[idx].pat = (char_u *) xstrdup((char *) s);
386386
spats[idx].magic = magic;
387387
spats[idx].no_scs = FALSE;
388388
spats[idx].off.dir = '/';

src/nvim/syntax.c

+28-30
Original file line numberDiff line numberDiff line change
@@ -3806,54 +3806,52 @@ static void clear_keywtab(hashtab_T *ht)
38063806
hash_init(ht);
38073807
}
38083808

3809-
/*
3810-
* Add a keyword to the list of keywords.
3811-
*/
3812-
static void
3813-
add_keyword (
3814-
char_u *name, /* name of keyword */
3815-
int id, /* group ID for this keyword */
3816-
int flags, /* flags for this keyword */
3817-
short *cont_in_list, /* containedin for this keyword */
3818-
short *next_list, /* nextgroup for this keyword */
3819-
int conceal_char
3820-
)
3809+
/// Add a keyword to the list of keywords.
3810+
///
3811+
/// @param name name of keyword
3812+
/// @param id group ID for this keyword
3813+
/// @param flags flags for this keyword
3814+
/// @param cont_in_list containedin for this keyword
3815+
/// @param next_list nextgroup for this keyword
3816+
static void add_keyword(char_u *name,
3817+
int id,
3818+
int flags,
3819+
short *cont_in_list,
3820+
short *next_list,
3821+
int conceal_char)
38213822
{
3822-
hashtab_T *ht;
3823-
hashitem_T *hi;
3824-
char_u *name_ic;
3825-
long_u hash;
38263823
char_u name_folded[MAXKEYWLEN + 1];
3824+
char_u *name_ic = (curwin->w_s->b_syn_ic)
3825+
? str_foldcase(name, (int)STRLEN(name), name_folded, sizeof(name_folded))
3826+
: name;
38273827

3828-
if (curwin->w_s->b_syn_ic)
3829-
name_ic = str_foldcase(name, (int)STRLEN(name),
3830-
name_folded, MAXKEYWLEN + 1);
3831-
else
3832-
name_ic = name;
38333828
keyentry_T *kp = xmalloc(sizeof(keyentry_T) + STRLEN(name_ic));
38343829
STRCPY(kp->keyword, name_ic);
38353830
kp->k_syn.id = id;
38363831
kp->k_syn.inc_tag = current_syn_inc_tag;
38373832
kp->flags = flags;
38383833
kp->k_char = conceal_char;
38393834
kp->k_syn.cont_in_list = copy_id_list(cont_in_list);
3840-
if (cont_in_list != NULL)
3835+
if (cont_in_list != NULL) {
38413836
curwin->w_s->b_syn_containedin = TRUE;
3837+
}
38423838
kp->next_list = copy_id_list(next_list);
38433839

3844-
if (curwin->w_s->b_syn_ic)
3845-
ht = &curwin->w_s->b_keywtab_ic;
3846-
else
3847-
ht = &curwin->w_s->b_keywtab;
3840+
long_u hash = hash_hash(kp->keyword);
3841+
hashtab_T *ht = (curwin->w_s->b_syn_ic) ? &curwin->w_s->b_keywtab_ic
3842+
: &curwin->w_s->b_keywtab;
3843+
hashitem_T *hi = hash_lookup(ht, kp->keyword, hash);
38483844

3849-
hash = hash_hash(kp->keyword);
3850-
hi = hash_lookup(ht, kp->keyword, hash);
3845+
// even though it looks like only the kp->keyword member is
3846+
// being used here, vim uses some pointer trickery to get the orignal
3847+
// struct again later by using knowledge of the offset of the keyword
3848+
// field in the struct. See the definition of the HI2KE macro.
38513849
if (HASHITEM_EMPTY(hi)) {
3852-
/* new keyword, add to hashtable */
3850+
// new keyword, add to hashtable
38533851
kp->ke_next = NULL;
38543852
hash_add_item(ht, hi, kp->keyword, hash);
38553853
} else {
3856-
/* keyword already exists, prepend to list */
3854+
// keyword already exists, prepend to list
38573855
kp->ke_next = HI2KE(hi);
38583856
hi->hi_key = KE2HIKEY(kp);
38593857
}

0 commit comments

Comments
 (0)