aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorElliott Hughes2014-12-16 16:45:32 -0600
committerElliott Hughes2014-12-20 20:53:08 -0600
commit1b18aff9ba7c06e0e41ccafd22e1a4a585f86d78 (patch)
tree53f14fee7cd6a9ecc1f9132ff1cc5c57e8708fde
parent92b9cb2c899c386954b8f9ad8111aa6c8c63e306 (diff)
downloadplatform-bionic-1b18aff9ba7c06e0e41ccafd22e1a4a585f86d78.tar.gz
platform-bionic-1b18aff9ba7c06e0e41ccafd22e1a4a585f86d78.tar.xz
platform-bionic-1b18aff9ba7c06e0e41ccafd22e1a4a585f86d78.zip
Fix nan output in the printf family.
Bug: https://code.google.com/p/android/issues/detail?id=82452 Change-Id: I51f226c8b033de6e81baeea5e6db3de6ed196f73
-rw-r--r--libc/upstream-openbsd/lib/libc/stdio/vfprintf.c7
-rw-r--r--libc/upstream-openbsd/lib/libc/stdio/vfwprintf.c7
-rw-r--r--tests/stdio_test.cpp76
3 files changed, 66 insertions, 24 deletions
diff --git a/libc/upstream-openbsd/lib/libc/stdio/vfprintf.c b/libc/upstream-openbsd/lib/libc/stdio/vfprintf.c
index 7f8ff317..5f4fb7fa 100644
--- a/libc/upstream-openbsd/lib/libc/stdio/vfprintf.c
+++ b/libc/upstream-openbsd/lib/libc/stdio/vfprintf.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: vfprintf.c,v 1.66 2014/05/03 12:36:45 deraadt Exp $ */ 1/* $OpenBSD: vfprintf.c,v 1.67 2014/12/21 00:23:30 daniel Exp $ */
2/*- 2/*-
3 * Copyright (c) 1990 The Regents of the University of California. 3 * Copyright (c) 1990 The Regents of the University of California.
4 * All rights reserved. 4 * All rights reserved.
@@ -753,10 +753,9 @@ fp_common:
753 if (signflag) 753 if (signflag)
754 sign = '-'; 754 sign = '-';
755 if (expt == INT_MAX) { /* inf or nan */ 755 if (expt == INT_MAX) { /* inf or nan */
756 if (*cp == 'N') { 756 if (*cp == 'N')
757 cp = (ch >= 'a') ? "nan" : "NAN"; 757 cp = (ch >= 'a') ? "nan" : "NAN";
758 sign = '\0'; 758 else
759 } else
760 cp = (ch >= 'a') ? "inf" : "INF"; 759 cp = (ch >= 'a') ? "inf" : "INF";
761 size = 3; 760 size = 3;
762 flags &= ~ZEROPAD; 761 flags &= ~ZEROPAD;
diff --git a/libc/upstream-openbsd/lib/libc/stdio/vfwprintf.c b/libc/upstream-openbsd/lib/libc/stdio/vfwprintf.c
index ef0ca43b..a6f41232 100644
--- a/libc/upstream-openbsd/lib/libc/stdio/vfwprintf.c
+++ b/libc/upstream-openbsd/lib/libc/stdio/vfwprintf.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: vfwprintf.c,v 1.11 2014/06/04 07:45:25 stsp Exp $ */ 1/* $OpenBSD: vfwprintf.c,v 1.12 2014/12/21 00:23:30 daniel Exp $ */
2/*- 2/*-
3 * Copyright (c) 1990 The Regents of the University of California. 3 * Copyright (c) 1990 The Regents of the University of California.
4 * All rights reserved. 4 * All rights reserved.
@@ -731,10 +731,9 @@ fp_common:
731 if (signflag) 731 if (signflag)
732 sign = '-'; 732 sign = '-';
733 if (expt == INT_MAX) { /* inf or nan */ 733 if (expt == INT_MAX) { /* inf or nan */
734 if (*cp == 'N') { 734 if (*cp == 'N')
735 cp = (ch >= 'a') ? L"nan" : L"NAN"; 735 cp = (ch >= 'a') ? L"nan" : L"NAN";
736 sign = '\0'; 736 else
737 } else
738 cp = (ch >= 'a') ? L"inf" : L"INF"; 737 cp = (ch >= 'a') ? L"inf" : L"INF";
739 size = 3; 738 size = 3;
740 flags &= ~ZEROPAD; 739 flags &= ~ZEROPAD;
diff --git a/tests/stdio_test.cpp b/tests/stdio_test.cpp
index 854fc7b7..de5eea34 100644
--- a/tests/stdio_test.cpp
+++ b/tests/stdio_test.cpp
@@ -364,22 +364,52 @@ TEST(stdio, snprintf_smoke) {
364 EXPECT_STREQ("print_me_twice print_me_twice", buf); 364 EXPECT_STREQ("print_me_twice print_me_twice", buf);
365} 365}
366 366
367TEST(stdio, snprintf_f_special) { 367template <typename T>
368 char buf[BUFSIZ]; 368void CheckInfNan(int snprintf_fn(T*, size_t, const T*, ...),
369 snprintf(buf, sizeof(buf), "%f", nanf("")); 369 const T* fmt, const T* fmt_plus,
370 EXPECT_STRCASEEQ("NaN", buf); 370 const T* minus_inf, const T* inf_, const T* plus_inf,
371 371 const T* minus_nan, const T* nan_, const T* plus_nan) {
372 snprintf(buf, sizeof(buf), "%f", HUGE_VALF); 372 T buf[BUFSIZ];
373 EXPECT_STRCASEEQ("Inf", buf); 373
374} 374 snprintf_fn(buf, sizeof(buf), fmt, nan(""));
375 375 EXPECT_STREQ(nan_, buf) << fmt;
376TEST(stdio, snprintf_g_special) { 376 snprintf_fn(buf, sizeof(buf), fmt, -nan(""));
377 char buf[BUFSIZ]; 377 EXPECT_STREQ(minus_nan, buf) << fmt;
378 snprintf(buf, sizeof(buf), "%g", nan("")); 378 snprintf_fn(buf, sizeof(buf), fmt_plus, nan(""));
379 EXPECT_STRCASEEQ("NaN", buf); 379 EXPECT_STREQ(plus_nan, buf) << fmt_plus;
380 380 snprintf_fn(buf, sizeof(buf), fmt_plus, -nan(""));
381 snprintf(buf, sizeof(buf), "%g", HUGE_VAL); 381 EXPECT_STREQ(minus_nan, buf) << fmt_plus;
382 EXPECT_STRCASEEQ("Inf", buf); 382
383 snprintf_fn(buf, sizeof(buf), fmt, HUGE_VAL);
384 EXPECT_STREQ(inf_, buf) << fmt;
385 snprintf_fn(buf, sizeof(buf), fmt, -HUGE_VAL);
386 EXPECT_STREQ(minus_inf, buf) << fmt;
387 snprintf_fn(buf, sizeof(buf), fmt_plus, HUGE_VAL);
388 EXPECT_STREQ(plus_inf, buf) << fmt_plus;
389 snprintf_fn(buf, sizeof(buf), fmt_plus, -HUGE_VAL);
390 EXPECT_STREQ(minus_inf, buf) << fmt_plus;
391}
392
393TEST(stdio, snprintf_inf_nan) {
394 CheckInfNan(snprintf, "%a", "%+a", "-inf", "inf", "+inf", "-nan", "nan", "+nan");
395 CheckInfNan(snprintf, "%A", "%+A", "-INF", "INF", "+INF", "-NAN", "NAN", "+NAN");
396 CheckInfNan(snprintf, "%e", "%+e", "-inf", "inf", "+inf", "-nan", "nan", "+nan");
397 CheckInfNan(snprintf, "%E", "%+E", "-INF", "INF", "+INF", "-NAN", "NAN", "+NAN");
398 CheckInfNan(snprintf, "%f", "%+f", "-inf", "inf", "+inf", "-nan", "nan", "+nan");
399 CheckInfNan(snprintf, "%F", "%+F", "-INF", "INF", "+INF", "-NAN", "NAN", "+NAN");
400 CheckInfNan(snprintf, "%g", "%+g", "-inf", "inf", "+inf", "-nan", "nan", "+nan");
401 CheckInfNan(snprintf, "%G", "%+G", "-INF", "INF", "+INF", "-NAN", "NAN", "+NAN");
402}
403
404TEST(stdio, wsprintf_inf_nan) {
405 CheckInfNan(swprintf, L"%a", L"%+a", L"-inf", L"inf", L"+inf", L"-nan", L"nan", L"+nan");
406 CheckInfNan(swprintf, L"%A", L"%+A", L"-INF", L"INF", L"+INF", L"-NAN", L"NAN", L"+NAN");
407 CheckInfNan(swprintf, L"%e", L"%+e", L"-inf", L"inf", L"+inf", L"-nan", L"nan", L"+nan");
408 CheckInfNan(swprintf, L"%E", L"%+E", L"-INF", L"INF", L"+INF", L"-NAN", L"NAN", L"+NAN");
409 CheckInfNan(swprintf, L"%f", L"%+f", L"-inf", L"inf", L"+inf", L"-nan", L"nan", L"+nan");
410 CheckInfNan(swprintf, L"%F", L"%+F", L"-INF", L"INF", L"+INF", L"-NAN", L"NAN", L"+NAN");
411 CheckInfNan(swprintf, L"%g", L"%+g", L"-inf", L"inf", L"+inf", L"-nan", L"nan", L"+nan");
412 CheckInfNan(swprintf, L"%G", L"%+G", L"-INF", L"INF", L"+INF", L"-NAN", L"NAN", L"+NAN");
383} 413}
384 414
385TEST(stdio, snprintf_d_INT_MAX) { 415TEST(stdio, snprintf_d_INT_MAX) {
@@ -439,8 +469,22 @@ TEST(stdio, snprintf_e) {
439TEST(stdio, snprintf_negative_zero_5084292) { 469TEST(stdio, snprintf_negative_zero_5084292) {
440 char buf[BUFSIZ]; 470 char buf[BUFSIZ];
441 471
472 snprintf(buf, sizeof(buf), "%e", -0.0);
473 EXPECT_STREQ("-0.000000e+00", buf);
474 snprintf(buf, sizeof(buf), "%E", -0.0);
475 EXPECT_STREQ("-0.000000E+00", buf);
442 snprintf(buf, sizeof(buf), "%f", -0.0); 476 snprintf(buf, sizeof(buf), "%f", -0.0);
443 EXPECT_STREQ("-0.000000", buf); 477 EXPECT_STREQ("-0.000000", buf);
478 snprintf(buf, sizeof(buf), "%F", -0.0);
479 EXPECT_STREQ("-0.000000", buf);
480 snprintf(buf, sizeof(buf), "%g", -0.0);
481 EXPECT_STREQ("-0", buf);
482 snprintf(buf, sizeof(buf), "%G", -0.0);
483 EXPECT_STREQ("-0", buf);
484 snprintf(buf, sizeof(buf), "%a", -0.0);
485 EXPECT_STREQ("-0x0p+0", buf);
486 snprintf(buf, sizeof(buf), "%A", -0.0);
487 EXPECT_STREQ("-0X0P+0", buf);
444} 488}
445 489
446TEST(stdio, snprintf_utf8_15439554) { 490TEST(stdio, snprintf_utf8_15439554) {