refactor: change some xstrndup() and xstrnsave() to xmemdupz() (#25959)

When the given length is exactly the number of bytes to copy, xmemdupz()
makes the intention clearer.
This commit is contained in:
zeertzjq
2023-11-10 08:39:21 +08:00
committed by GitHub
parent ae8ca79920
commit cd63a9addd
32 changed files with 64 additions and 67 deletions

View File

@@ -5669,7 +5669,7 @@ static void read_file_or_blob(typval_T *argvars, typval_T *rettv, bool always_bl
}
if (prevlen == 0) {
assert(len < INT_MAX);
s = xstrnsave(start, len);
s = xmemdupz(start, len);
} else {
// Change "prev" buffer to be the right size. This way
// the bytes are only copied once, and very long lines are
@@ -6305,7 +6305,7 @@ static void reduce_string(typval_T *argvars, typval_T *expr, typval_T *rettv)
*rettv = (typval_T){
.v_type = VAR_STRING,
.v_lock = VAR_UNLOCKED,
.vval.v_string = xstrnsave(p, (size_t)len),
.vval.v_string = xmemdupz(p, (size_t)len),
};
p += len;
} else if (tv_check_for_string_arg(argvars, 2) == FAIL) {
@@ -6321,7 +6321,7 @@ static void reduce_string(typval_T *argvars, typval_T *expr, typval_T *rettv)
argv[1] = (typval_T){
.v_type = VAR_STRING,
.v_lock = VAR_UNLOCKED,
.vval.v_string = xstrnsave(p, (size_t)len),
.vval.v_string = xmemdupz(p, (size_t)len),
};
const int r = eval_expr_typval(expr, true, argv, 2, rettv);