diff options
-rw-r--r-- | src/nvim/eval/typval.c | 28 | ||||
-rw-r--r-- | src/nvim/memline.c | 23 |
2 files changed, 33 insertions, 18 deletions
diff --git a/src/nvim/eval/typval.c b/src/nvim/eval/typval.c index 912aecafec..ffb46abfea 100644 --- a/src/nvim/eval/typval.c +++ b/src/nvim/eval/typval.c @@ -1656,12 +1656,7 @@ int tv_dict_add_special(dict_T *const d, const char *const key, /// Add a string entry to dictionary /// -/// @param[out] d Dictionary to add entry to. -/// @param[in] key Key to add. -/// @param[in] key_len Key length. -/// @param[in] val String to add. -/// -/// @return OK in case of success, FAIL when key already exists. +/// @see tv_dict_add_allocated_str int tv_dict_add_str(dict_T *const d, const char *const key, const size_t key_len, const char *const val) @@ -1672,6 +1667,27 @@ int tv_dict_add_str(dict_T *const d, /// Add a string entry to dictionary /// +/// @param[out] d Dictionary to add entry to. +/// @param[in] key Key to add. +/// @param[in] key_len Key length. +/// @param[in] val String to add. NULL adds empty string. +/// @param[in] len Use this many bytes from `val`, or -1 for whole string. +/// +/// @return OK in case of success, FAIL when key already exists. +int tv_dict_add_str_len(dict_T *const d, + const char *const key, const size_t key_len, + char *const val, int len) + FUNC_ATTR_NONNULL_ARG(1, 2) +{ + char *s = val ? val : ""; + if (val != NULL) { + s = (len < 0) ? xstrdup(val) : xstrndup(val, (size_t)len); + } + return tv_dict_add_allocated_str(d, key, key_len, s); +} + +/// Add a string entry to dictionary +/// /// Unlike tv_dict_add_str() saves val to the new dictionary item in place of /// creating a new copy. /// diff --git a/src/nvim/memline.c b/src/nvim/memline.c index e341ba0427..25266f4261 100644 --- a/src/nvim/memline.c +++ b/src/nvim/memline.c @@ -1466,19 +1466,18 @@ void get_b0_dict(const char *fname, dict_T *d) if ((fd = os_open(fname, O_RDONLY, 0)) >= 0) { if (read_eintr(fd, &b0, sizeof(b0)) == sizeof(b0)) { if (ml_check_b0_id(&b0) == FAIL) { - tv_dict_add_str(d, S_LEN("error"), xstrdup("Not a swap file")); + tv_dict_add_str(d, S_LEN("error"), "Not a swap file"); } else if (b0_magic_wrong(&b0)) { - tv_dict_add_str(d, S_LEN("error"), xstrdup("Magic number mismatch")); + tv_dict_add_str(d, S_LEN("error"), "Magic number mismatch"); } else { // We have swap information. - tv_dict_add_str(d, S_LEN("version"), - xstrndup((char *)b0.b0_version, 10)); - tv_dict_add_str(d, S_LEN("user"), - xstrndup((char *)b0.b0_uname, B0_UNAME_SIZE)); - tv_dict_add_str(d, S_LEN("host"), - xstrndup((char *)b0.b0_hname, B0_HNAME_SIZE)); - tv_dict_add_str(d, S_LEN("fname"), - xstrndup((char *)b0.b0_fname, B0_FNAME_SIZE_ORG)); + tv_dict_add_str_len(d, S_LEN("version"), (char *)b0.b0_version, 10); + tv_dict_add_str_len(d, S_LEN("user"), (char *)b0.b0_uname, + B0_UNAME_SIZE); + tv_dict_add_str_len(d, S_LEN("host"), (char *)b0.b0_hname, + B0_HNAME_SIZE); + tv_dict_add_str_len(d, S_LEN("fname"), (char *)b0.b0_fname, + B0_FNAME_SIZE_ORG); tv_dict_add_nr(d, S_LEN("pid"), char_to_long(b0.b0_pid)); tv_dict_add_nr(d, S_LEN("mtime"), char_to_long(b0.b0_mtime)); @@ -1486,11 +1485,11 @@ void get_b0_dict(const char *fname, dict_T *d) tv_dict_add_nr(d, S_LEN("inode"), char_to_long(b0.b0_ino)); } } else { - tv_dict_add_str(d, S_LEN("error"), xstrdup("Cannot read file")); + tv_dict_add_str(d, S_LEN("error"), "Cannot read file"); } close(fd); } else { - tv_dict_add_str(d, S_LEN("error"), xstrdup("Cannot open file")); + tv_dict_add_str(d, S_LEN("error"), "Cannot open file"); } } |