>From beeaaf02c5e675fee031ed5eb856d6c391636e85 Mon Sep 17 00:00:00 2001 From: Peter Bex Date: Mon, 18 Nov 2013 22:06:13 +0100 Subject: [PATCH] Remove HAVE_GCVT check and definition. Use snprintf on all platforms. gcvt() is deprecated in POSIX, and it produces problematic output in at least cygwin. For example, -0.0 is printed as "0.0" and more fun things. This fix adds a regression test for the negative zero case. Replace sprintf usage with snprintf just in case someone decides to use insane values for flonum_print_precision. --- Makefile.aix | 1 - Makefile.cross-linux-mingw | 1 - Makefile.cygwin | 1 - Makefile.haiku | 1 - Makefile.hurd | 1 - Makefile.linux | 1 - Makefile.mingw | 1 - Makefile.mingw-msys | 1 - Makefile.solaris | 1 - chicken.h | 1 - runtime.c | 17 +++-------------- tests/numbers-string-conversion-tests.scm | 4 +++- 12 files changed, 6 insertions(+), 25 deletions(-) diff --git a/Makefile.aix b/Makefile.aix index d453499..8bde946 100644 --- a/Makefile.aix +++ b/Makefile.aix @@ -97,7 +97,6 @@ chicken-config.h: chicken-defaults.h echo "#define HAVE_ALLOCA_H 1" >>$@ echo "#define HAVE_GRP_H 1" >>$@ echo "#define HAVE_ERRNO_H 1" >>$@ - echo "#define HAVE_GCVT 1" >>$@ echo "#define HAVE_SYSEXITS_H 1" >>$@ echo "#define C_STACK_GROWS_DOWNWARD 1" >>$@ ifdef GCHOOKS diff --git a/Makefile.cross-linux-mingw b/Makefile.cross-linux-mingw index c18dd18..e1778ca 100644 --- a/Makefile.cross-linux-mingw +++ b/Makefile.cross-linux-mingw @@ -116,7 +116,6 @@ chicken-config.h: chicken-defaults.h echo "#define HAVE_ALLOCA_H 1" >>$@ echo "#define HAVE_DIRECT_H 1" >>$@ echo "#define HAVE_ERRNO_H 1" >>$@ - echo "#define HAVE_GCVT 1" >>$@ echo "#define HAVE_LOADLIBRARY 1" >>$@ echo "#define HAVE_GETPROCADDRESS 1" >>$@ echo "#define HAVE_WINSOCK2_H 1" >>$@ diff --git a/Makefile.cygwin b/Makefile.cygwin index b4122ef..1518a16 100644 --- a/Makefile.cygwin +++ b/Makefile.cygwin @@ -113,7 +113,6 @@ chicken-config.h: chicken-defaults.h echo "#define HAVE_ALLOCA_H 1" >>$@ echo "#define HAVE_GRP_H 1" >>$@ echo "#define HAVE_ERRNO_H 1" >>$@ - echo "#define HAVE_GCVT 1" >>$@ echo "#define HAVE_SYSEXITS_H 1" >>$@ echo "#define HAVE_DLFCN_H 1" >>$@ echo "#define C_STACK_GROWS_DOWNWARD 1" >>$@ diff --git a/Makefile.haiku b/Makefile.haiku index c6e33ab..63da7c0 100644 --- a/Makefile.haiku +++ b/Makefile.haiku @@ -91,7 +91,6 @@ chicken-config.h: chicken-defaults.h echo "#define HAVE_ALLOCA_H 1" >>$@ echo "#define HAVE_GRP_H 1" >>$@ echo "#define HAVE_ERRNO_H 1" >>$@ - echo "#define HAVE_GCVT 1" >>$@ echo "#define HAVE_MEMMOVE 1" >>$@ echo "#define C_STACK_GROWS_DOWNWARD 1" >>$@ echo "#define SIGIO 0" >>$@ diff --git a/Makefile.hurd b/Makefile.hurd index 74c8c00..1a64158 100644 --- a/Makefile.hurd +++ b/Makefile.hurd @@ -92,7 +92,6 @@ chicken-config.h: chicken-defaults.h echo "#define HAVE_ALLOCA_H 1" >>$@ echo "#define HAVE_GRP_H 1" >>$@ echo "#define HAVE_ERRNO_H 1" >>$@ - echo "#define HAVE_GCVT 1" >>$@ echo "#define HAVE_SYSEXITS_H 1" >>$@ echo "#define HAVE_MEMMOVE 1" >>$@ echo "#define C_STACK_GROWS_DOWNWARD 1" >>$@ diff --git a/Makefile.linux b/Makefile.linux index 11d3b9f..7f5898d 100644 --- a/Makefile.linux +++ b/Makefile.linux @@ -92,7 +92,6 @@ chicken-config.h: chicken-defaults.h echo "#define HAVE_ALLOCA_H 1" >>$@ echo "#define HAVE_GRP_H 1" >>$@ echo "#define HAVE_ERRNO_H 1" >>$@ - echo "#define HAVE_GCVT 1" >>$@ echo "#define HAVE_SYSEXITS_H 1" >>$@ echo "#define HAVE_MEMMOVE 1" >>$@ echo "#define C_STACK_GROWS_DOWNWARD 1" >>$@ diff --git a/Makefile.mingw b/Makefile.mingw index e34f604..48b801d 100644 --- a/Makefile.mingw +++ b/Makefile.mingw @@ -110,7 +110,6 @@ chicken-config.h: chicken-defaults.h echo #define STDC_HEADERS 1 >>$@ echo #define HAVE_DIRECT_H 1 >>$@ echo #define HAVE_ERRNO_H 1 >>$@ - echo #define HAVE_GCVT 1 >>$@ echo #define HAVE_LOADLIBRARY 1 >>$@ echo #define HAVE_GETPROCADDRESS 1 >>$@ echo #define HAVE_WINSOCK2_H 1 >>$@ diff --git a/Makefile.mingw-msys b/Makefile.mingw-msys index d2ac34b..74607aa 100644 --- a/Makefile.mingw-msys +++ b/Makefile.mingw-msys @@ -109,7 +109,6 @@ chicken-config.h: chicken-defaults.h echo "#define STDC_HEADERS 1" >>$@ echo "#define HAVE_DIRECT_H 1" >>$@ echo "#define HAVE_ERRNO_H 1" >>$@ - echo "#define HAVE_GCVT 1" >>$@ echo "#define HAVE_LOADLIBRARY 1" >>$@ echo "#define HAVE_GETPROCADDRESS 1" >>$@ echo "#define HAVE_WINSOCK2_H 1" >>$@ diff --git a/Makefile.solaris b/Makefile.solaris index 99f9826..a7c8894 100644 --- a/Makefile.solaris +++ b/Makefile.solaris @@ -119,7 +119,6 @@ chicken-config.h: chicken-defaults.h echo "#define HAVE_ALLOCA 1" >>$@ echo "#define HAVE_GRP_H 1" >>$@ echo "#define HAVE_ERRNO_H 1" >>$@ - echo "#define HAVE_GCVT 1" >>$@ echo "#define HAVE_SYSEXITS_H 1" >>$@ echo "#define C_STACK_GROWS_DOWNWARD 1" >>$@ ifdef GCHOOKS diff --git a/chicken.h b/chicken.h index 6265940..0174c3e 100644 --- a/chicken.h +++ b/chicken.h @@ -956,7 +956,6 @@ DECL_C_PROC_p0 (128, 1,0,0,0,0,0,0,0) # define C_fopen fopen # define C_fclose fclose # define C_strpbrk strpbrk -# define C_gcvt gcvt # define C_sprintf sprintf # define C_snprintf snprintf # define C_printf printf diff --git a/runtime.c b/runtime.c index bd676d1..c65df50 100644 --- a/runtime.c +++ b/runtime.c @@ -4137,11 +4137,7 @@ C_regparm C_word C_fcall C_display_flonum(C_word port, C_word n) { C_FILEPTR fp = C_port_file(port); -#ifdef HAVE_GCVT - C_fprintf(fp, C_text("%s"), C_gcvt(C_flonum_magnitude(n), flonum_print_precision, buffer)); -#else C_fprintf(fp, C_text("%.*g"), flonum_print_precision, C_flonum_magnitude(n)); -#endif return C_SCHEME_UNDEFINED; } @@ -7807,11 +7803,9 @@ void C_ccall C_number_to_string(C_word c, C_word closure, C_word k, C_word num, goto fini; } -#ifdef HAVE_GCVT - p = C_gcvt(f, flonum_print_precision, buffer); /* p unused, but we want to avoid stupid warnings */ -#else - C_sprintf(buffer, C_text("%.*g"), flonum_print_precision, f); -#endif + C_snprintf(buffer, STRING_BUFFER_SIZE, C_text("%.*g"), + flonum_print_precision, f); + buffer[STRING_BUFFER_SIZE-1] = '\0'; if((p = C_strpbrk(buffer, C_text(".eE"))) == NULL) { if(*buffer == 'i' || *buffer == 'n') { /* inf or nan */ @@ -7820,11 +7814,6 @@ void C_ccall C_number_to_string(C_word c, C_word closure, C_word k, C_word num, } else if(buffer[ 1 ] != 'i') C_strcat(buffer, C_text(".0")); /* negative infinity? */ } -#ifdef __MINGW32__ - /* On mingw32, gcvt(3) does not add a trailing zero */ - else if(buffer[ C_strlen(buffer) - 1 ] == '.') - C_strcat(buffer, C_text("0")); -#endif p = buffer; } diff --git a/tests/numbers-string-conversion-tests.scm b/tests/numbers-string-conversion-tests.scm index 3373117..3c19c24 100644 --- a/tests/numbers-string-conversion-tests.scm +++ b/tests/numbers-string-conversion-tests.scm @@ -17,6 +17,7 @@ (define the-nan (fp/ 0.0 0.0)) (define pos-inf (fp/ 1.0 0.0)) (define neg-inf (fp/ -1.0 0.0)) +(define neg-zero (/ -1.0 +inf.0)) (define (nan? x) (and (number? x) (not (= x x)))) @@ -171,7 +172,7 @@ ("1#e2" 1000.0 1500.0 "1000.0" "1500.0" "1000." "1500." "1.0e3" "15.0e2") ("1e2#" #f) - "NaN, Inf" + "NaN, Inf, negative zero" ("+nan.0" the-nan "+NaN.0") ("+NAN.0" the-nan "+nan.0" "+NaN.0") ("+nan.1" #f) @@ -193,6 +194,7 @@ ("#i+nan.0" the-nan "+nan.0" "+NaN.0") ("#i+inf.0" pos-inf "+inf.0" "+Inf.0") ("#i-inf.0" neg-inf "-inf.0" "-Inf.0") + ("-0.0" neg-zero "-.0" "-0.") ;; These used to be accepted but are invalid ("+nan" #f) ("+inf" #f) -- 1.7.10.4