From: VMware, Inc <> Date: Sat, 28 May 2011 19:51:51 +0000 (-0700) Subject: lib/string: clean up before a major change X-Git-Tag: 2011.05.27-420096~44 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=ed553dd79c286d85f1bec284d2c54fd5c8ab0880;p=thirdparty%2Fopen-vm-tools.git lib/string: clean up before a major change Pretty up things at a bit... Signed-off-by: Marcelo Vanzin --- diff --git a/open-vm-tools/lib/string/bsd_output_shared.c b/open-vm-tools/lib/string/bsd_output_shared.c index 56bb804ac..1c49262f1 100644 --- a/open-vm-tools/lib/string/bsd_output_shared.c +++ b/open-vm-tools/lib/string/bsd_output_shared.c @@ -104,7 +104,46 @@ dtoa(double d, // IN char *str = NULL; int dec; -#ifndef _WIN32 +#if defined(_WIN32) + if (2 == mode) { + str = malloc(_CVTBUFSIZE); + if (str) { + if (_ecvt_s(str, _CVTBUFSIZE, d, prec, &dec, sign)) { + free(str); + str = NULL; + } + } + } else { + ASSERT(3 == mode); + str = malloc(_CVTBUFSIZE); + if (str) { + if (_fcvt_s(str, _CVTBUFSIZE, d, prec, &dec, sign)) { + free(str); + str = NULL; + } + } + + /* + * When the value is not zero but rounds to zero at prec digits, + * the Windows fcvt() sometimes returns the empty string and + * a negative dec that goes too far (as in -dec > prec). + * For example, converting 0.001 with prec 1 results in + * the empty string and dec -2. (See bug 253674.) + * + * We just clamp dec to -prec when this happens. + * + * While this may appear to be a safe and good thing to + * do in general. It really only works when the result is + * all zeros or empty. Since checking for all zeros is + * expensive, we only check for empty string, which works + * for this bug. + */ + + if (str && *str == '\0' && dec < 0 && dec < -prec) { + dec = -prec; + } + } +#else // _WIN32 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; if (2 == mode) { @@ -148,44 +187,6 @@ dtoa(double d, // IN pthread_mutex_unlock(&mutex); } } -#else // _WIN32 - if (2 == mode) { - str = malloc(_CVTBUFSIZE); - if (str) { - if (_ecvt_s(str, _CVTBUFSIZE, d, prec, &dec, sign)) { - free(str); - str = NULL; - } - } - } else { - ASSERT(3 == mode); - str = malloc(_CVTBUFSIZE); - if (str) { - if (_fcvt_s(str, _CVTBUFSIZE, d, prec, &dec, sign)) { - free(str); - str = NULL; - } - } - /* - * When the value is not zero but rounds to zero at prec digits, - * the Windows fcvt() sometimes returns the empty string and - * a negative dec that goes too far (as in -dec > prec). - * For example, converting 0.001 with prec 1 results in - * the empty string and dec -2. (See bug 253674.) - * - * We just clamp dec to -prec when this happens. - * - * While this may appear to be a safe and good thing to - * do in general. It really only works when the result is - * all zeros or empty. Since checking for all zeros is - * expensive, we only check for empty string, which works - * for this bug. - */ - - if (str && *str == '\0' && dec < 0 && dec < -prec) { - dec = -prec; - } - } #endif // _WIN32 if (str) {