Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/tests/lib/libc/gen Formally verify the numerous bugs in huma...



details:   https://anonhg.NetBSD.org/src/rev/9fe385b75419
branches:  trunk
changeset: 763899:9fe385b75419
user:      jruoho <jruoho%NetBSD.org@localhost>
date:      Wed Apr 06 10:04:53 2011 +0000

description:
Formally verify the numerous bugs in humanize_number(3).
>From PR lib/44097, by yamt@.

diffstat:

 tests/lib/libc/gen/t_humanize_number.c |  84 ++++++++++++++++++++++++++++-----
 1 files changed, 70 insertions(+), 14 deletions(-)

diffs (125 lines):

diff -r 0569753e6645 -r 9fe385b75419 tests/lib/libc/gen/t_humanize_number.c
--- a/tests/lib/libc/gen/t_humanize_number.c    Wed Apr 06 09:35:49 2011 +0000
+++ b/tests/lib/libc/gen/t_humanize_number.c    Wed Apr 06 10:04:53 2011 +0000
@@ -1,12 +1,9 @@
-/*     $NetBSD: t_humanize_number.c,v 1.1 2010/12/28 12:46:15 pgoyette Exp $   */
+/*     $NetBSD: t_humanize_number.c,v 1.2 2011/04/06 10:04:53 jruoho Exp $     */
 
 /*-
- * Copyright (c) 2010 The NetBSD Foundation, Inc.
+ * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc.
  * All rights reserved.
  *
- * This code is derived from software contributed to The NetBSD Foundation
- * by 
- *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
@@ -113,11 +110,10 @@
        { HN_DIVISOR_1000, "HN_DIVISOR_1000" },
 };
 
-const char *
-       formatflags(char *, size_t, const struct hnflags *, size_t, int);
-void   newline(void);
-void   w_printf(const char *, ...);
-int    main(int, char *[]);
+const char *formatflags(char *, size_t, const struct hnflags *, size_t, int);
+void       newline(void);
+void       w_printf(const char *, ...);
+int        main(int, char *[]);
 
 const char *
 formatflags(char *buf, size_t buflen, const struct hnflags *hfs,
@@ -198,15 +194,15 @@
        va_end(ap);
 }
 
-ATF_TC(humanize);
+ATF_TC(humanize_basic);
 
-ATF_TC_HEAD(humanize, tc)
+ATF_TC_HEAD(humanize_basic, tc)
 {
 
        atf_tc_set_md_var(tc, "descr", "Test humanize_number(3)");
 }
 
-ATF_TC_BODY(humanize, tc)
+ATF_TC_BODY(humanize_basic, tc)
 {
        char fbuf[128];
        const struct hnopts *ho;
@@ -248,10 +244,70 @@
        }
 }
 
+ATF_TC(humanize_big);
+
+ATF_TC_HEAD(humanize_big, tc)
+{
+
+       atf_tc_set_md_var(tc, "descr", "Test humanize big numbers");
+}
+
+ATF_TC_BODY(humanize_big, tc)
+{
+       char buf[1024];
+       int rv;
+
+       /*
+        * Seems to works.
+        */
+       (void)memset(buf, 0, sizeof(buf));
+
+       rv = humanize_number(buf, 10, 10000, "",
+           HN_AUTOSCALE, HN_NOSPACE);
+
+       ATF_REQUIRE(rv != -1);
+       ATF_REQUIRE(strcmp(buf, "10000") == 0);
+
+       /*
+        * A bogus value with large number.
+        */
+       (void)memset(buf, 0, sizeof(buf));
+
+       rv = humanize_number(buf, 10, INT64_MAX, "",
+           HN_AUTOSCALE, HN_NOSPACE);
+
+       ATF_REQUIRE(rv != -1);
+       ATF_REQUIRE(strcmp(buf, "0") != 0);
+
+       /*
+        * Large buffer with HN_AUTOSCALE. Entirely bogus.
+        */
+       (void)memset(buf, 0, sizeof(buf));
+
+       rv = humanize_number(buf, sizeof(buf), 10000, "",
+           HN_AUTOSCALE, HN_NOSPACE);
+
+       ATF_REQUIRE(rv != -1);
+       ATF_REQUIRE(strcmp(buf, "0%d%s%d%s%s%s") != 0);
+
+       /*
+        * Tight buffer.
+        *
+        * The man page says that len must be at least 4, but...
+        */
+       (void)memset(buf, 0, sizeof(buf));
+
+       rv = humanize_number(buf, 1, 1, "",
+           HN_AUTOSCALE, HN_NOSPACE);
+
+       ATF_REQUIRE(rv != -1);
+}
+
 ATF_TP_ADD_TCS(tp)
 {
 
-       ATF_TP_ADD_TC(tp, humanize);
+       ATF_TP_ADD_TC(tp, humanize_basic);
+       ATF_TP_ADD_TC(tp, humanize_big);
 
        return atf_no_error();
 }



Home | Main Index | Thread Index | Old Index