strings.c: Fix problems found during code review.

This commit is contained in:
Jurica Bradaric 2017-03-06 21:33:55 +01:00
parent 2f80360e9a
commit 04b91d6b89

View File

@ -588,17 +588,14 @@ static varnumber_T tv_nr(typval_T *tvs, int *idxp)
/// @param[in] tvs List of VimL values. List is terminated by VAR_UNKNOWN /// @param[in] tvs List of VimL values. List is terminated by VAR_UNKNOWN
/// value. /// value.
/// @param[in,out] idxp Index in a list. Will be incremented. /// @param[in,out] idxp Index in a list. Will be incremented.
/// /// @param[out] tofree If the idxp entry in tvs is not a String or a Number,
/// @param[out] tofree If "tofree" is NULL get_tv_string_chk() is used. Some /// it will be converted to String in the same format
/// types (e.g. List) are not converted to a string. If /// as ":echo" and stored in "*tofree". The caller must
/// "tofree" is not NULL encode_tv2echo() is used. All /// free "*tofree".
/// types are converted to a string with the same format as
/// ":echo". The caller must free "*tofree".
/// ///
/// @return String value or NULL in case of error. /// @return String value or NULL in case of error.
static char *tv_str(typval_T *tvs, int *idxp, char_u **tofree) static char *tv_str(typval_T *tvs, int *idxp, char ** const tofree)
FUNC_ATTR_NONNULL_ARG(1) FUNC_ATTR_NONNULL_ARG(2) FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
FUNC_ATTR_WARN_UNUSED_RESULT
{ {
int idx = *idxp - 1; int idx = *idxp - 1;
char *s = NULL; char *s = NULL;
@ -607,11 +604,12 @@ static char *tv_str(typval_T *tvs, int *idxp, char_u **tofree)
EMSG(_(e_printf)); EMSG(_(e_printf));
} else { } else {
(*idxp)++; (*idxp)++;
if (tofree != NULL) { if (tvs[idx].v_type == VAR_STRING || tvs[idx].v_type == VAR_NUMBER) {
s = encode_tv2echo(&tvs[idx], NULL);
*tofree = (char_u *)s;
} else {
s = (char *)get_tv_string_chk(&tvs[idx]); s = (char *)get_tv_string_chk(&tvs[idx]);
*tofree = NULL;
} else {
s = encode_tv2echo(&tvs[idx], NULL);
*tofree = s;
} }
} }
return s; return s;
@ -827,7 +825,7 @@ int vim_vsnprintf(char *str, size_t str_m, const char *fmt, va_list ap,
char fmt_spec = '\0'; char fmt_spec = '\0';
// buffer for 's' and 'S' specs // buffer for 's' and 'S' specs
char_u *tofree = NULL; char *tofree = NULL;
p++; // skip '%' p++; // skip '%'