0
0
mirror of https://github.com/vim/vim.git synced 2025-09-27 04:14:06 -04:00

patch 7.4.2280

Problem:    printf() doesn't handle infinity float values correctly.
Solution:   Add a table with possible infinity values. (Dominique Pelle)
This commit is contained in:
Bram Moolenaar
2016-08-28 16:03:38 +02:00
parent 46328f9a1c
commit e999782e36
3 changed files with 128 additions and 39 deletions

View File

@@ -3990,6 +3990,28 @@ tv_float(typval_T *tvs, int *idxp)
# endif
#endif
/*
* Return the representation of infinity for printf() function:
* "-inf", "inf", "+inf", " inf", "-INF", "INF", "+INF" or " INF".
*/
static const char *
infinity_str(int positive,
char fmt_spec,
int force_sign,
int space_for_positive)
{
static const char *table[] =
{
"-inf", "inf", "+inf", " inf",
"-INF", "INF", "+INF", " INF"
};
int idx = positive * (1 + force_sign + force_sign * space_for_positive);
if (ASCII_ISUPPER(fmt_spec))
idx += 4;
return table[idx];
}
/*
* This code was included to provide a portable vsnprintf() and snprintf().
* Some systems may provide their own, but we always use this one for
@@ -4008,8 +4030,8 @@ tv_float(typval_T *tvs, int *idxp)
*
* Limited support for floating point was added: 'f', 'e', 'E', 'g', 'G'.
*
* Length modifiers 'h' (short int) and 'l' (long int) are supported.
* 'll' (long long int) is not supported.
* Length modifiers 'h' (short int) and 'l' (long int) and 'll' (long long int)
* are supported.
*
* The locale is not used, the string is used as a byte string. This is only
* relevant for double-byte encodings where the second byte may be '%'.
@@ -4397,7 +4419,7 @@ vim_vsnprintf(
uvarnumber_T ullong_arg = 0;
# endif
/* only defined for b convertion */
/* only defined for b conversion */
uvarnumber_T bin_arg = 0;
/* pointer argument value -only defined for p
@@ -4702,7 +4724,6 @@ vim_vsnprintf(
char format[40];
int l;
int remove_trailing_zeroes = FALSE;
char *s;
f =
# if defined(FEAT_EVAL)
@@ -4732,16 +4753,10 @@ vim_vsnprintf(
)
{
/* Avoid a buffer overflow */
if (f < 0)
{
strcpy(tmp, "-inf");
str_arg_l = 4;
}
else
{
strcpy(tmp, "inf");
str_arg_l = 3;
}
STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
force_sign, space_for_positive));
str_arg_l = STRLEN(tmp);
zero_padding = 0;
}
else
{
@@ -4761,23 +4776,25 @@ vim_vsnprintf(
}
format[l] = fmt_spec;
format[l + 1] = NUL;
str_arg_l = sprintf(tmp, format, f);
/* Be consistent: Change "1.#IND" to "nan" and
* "1.#INF" to "inf". */
s = *tmp == '-' ? tmp + 1 : tmp;
if (STRNCMP(s, "1.#INF", 6) == 0)
STRCPY(s, "inf");
else if (STRNCMP(s, "1.#IND", 6) == 0)
STRCPY(s, "nan");
/* Remove sign before "nan". */
if (STRNCMP(tmp, "-nan", 4) == 0)
STRCPY(tmp, "nan");
/* Add sign before "inf" if needed. */
if (isinf(f) == -1 && STRNCMP(tmp, "inf", 3) == 0)
STRCPY(tmp, "-inf");
if (isnan(f))
{
/* Not a number: nan or NAN */
STRCPY(tmp, ASCII_ISUPPER(fmt_spec) ? "NAN"
: "nan");
str_arg_l = 3;
zero_padding = 0;
}
else if (isinf(f))
{
STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
force_sign, space_for_positive));
str_arg_l = STRLEN(tmp);
zero_padding = 0;
}
else
/* Regular float number */
str_arg_l = sprintf(tmp, format, f);
if (remove_trailing_zeroes)
{