refactor: add xmemcpyz() and use it in place of some xstrlcpy() (#28422)

Problem:  Using xstrlcpy() when the exact length of the string to be
          copied is known is not ideal because it requires adding 1 to
          the length and an unnecessary strlen().
Solution: Add xmemcpyz() and use it in place of such xstrlcpy() calls.
This commit is contained in:
zeertzjq
2024-04-20 19:31:00 +08:00
committed by GitHub
parent 4d52b0cf67
commit 0ea38c9a53
23 changed files with 61 additions and 46 deletions

View File

@@ -2859,7 +2859,7 @@ static void process_compflags(spellinfo_T *spin, afffile_T *aff, char *compflags
if (flag != 0) {
// Find the flag in the hashtable. If it was used before, use
// the existing ID. Otherwise add a new entry.
xstrlcpy(key, prevp, (size_t)(p - prevp) + 1);
xmemcpyz(key, prevp, (size_t)(p - prevp));
hashitem_T *hi = hash_find(&aff->af_comp, key);
if (!HASHITEM_EMPTY(hi)) {
id = HI2CI(hi)->ci_newID;
@@ -3263,7 +3263,7 @@ static int get_pfxlist(afffile_T *affile, char *afflist, char *store_afflist)
if (get_affitem(affile->af_flagtype, &p) != 0) {
// A flag is a postponed prefix flag if it appears in "af_pref"
// and its ID is not zero.
xstrlcpy(key, prevp, (size_t)(p - prevp) + 1);
xmemcpyz(key, prevp, (size_t)(p - prevp));
hashitem_T *hi = hash_find(&affile->af_pref, key);
if (!HASHITEM_EMPTY(hi)) {
int id = HI2AH(hi)->ah_newID;
@@ -3293,7 +3293,7 @@ static void get_compflags(afffile_T *affile, char *afflist, char *store_afflist)
char *prevp = p;
if (get_affitem(affile->af_flagtype, &p) != 0) {
// A flag is a compound flag if it appears in "af_comp".
xstrlcpy(key, prevp, (size_t)(p - prevp) + 1);
xmemcpyz(key, prevp, (size_t)(p - prevp));
hashitem_T *hi = hash_find(&affile->af_comp, key);
if (!HASHITEM_EMPTY(hi)) {
store_afflist[cnt++] = (char)(uint8_t)HI2CI(hi)->ci_newID;
@@ -3481,7 +3481,7 @@ static int store_aff_word(spellinfo_T *spin, char *word, char *afflist, afffile_
// Obey a "COMPOUNDFORBIDFLAG" of the affix: don't
// use the compound flags.
if (use_pfxlist != NULL && ae->ae_compforbid) {
xstrlcpy(pfx_pfxlist, use_pfxlist, (size_t)use_pfxlen + 1);
xmemcpyz(pfx_pfxlist, use_pfxlist, (size_t)use_pfxlen);
use_pfxlist = pfx_pfxlist;
}
@@ -5576,7 +5576,7 @@ static void init_spellfile(void)
if (aspath) {
// Use directory of an entry with path, e.g., for
// "/dir/lg.utf-8.spl" use "/dir".
xstrlcpy(buf, curbuf->b_s.b_p_spl, (size_t)(lstart - curbuf->b_s.b_p_spl));
xmemcpyz(buf, curbuf->b_s.b_p_spl, (size_t)(lstart - curbuf->b_s.b_p_spl - 1));
} else {
// Copy the path from 'runtimepath' to buf[].
copy_option_part(&rtp, buf, MAXPATHL, ",");
@@ -5585,7 +5585,7 @@ static void init_spellfile(void)
// Use the first language name from 'spelllang' and the
// encoding used in the first loaded .spl file.
if (aspath) {
xstrlcpy(buf, curbuf->b_s.b_p_spl, (size_t)(lend - curbuf->b_s.b_p_spl + 1));
xmemcpyz(buf, curbuf->b_s.b_p_spl, (size_t)(lend - curbuf->b_s.b_p_spl));
} else {
// Create the "spell" directory if it doesn't exist yet.
l = (int)strlen(buf);