[PATCH v7 03/15] Add FormatUInt64{,Hex}() for formatting numbers in a signal safe manner

Peter Hutterer peter.hutterer at who-t.net
Wed Jun 20 22:56:34 PDT 2012


From: Chase Douglas <chase.douglas at canonical.com>

Signed-off-by: Chase Douglas <chase.douglas at canonical.com>
Reviewed-by: Peter Hutterer <peter.hutterer at who-t.net>
---
 include/misc.h        |    2 +
 os/utils.c            |   44 +++++++++++++++++++
 test/.gitignore       |    1 +
 test/Makefile.am      |    3 +-
 test/signal-logging.c |  115 +++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 164 insertions(+), 1 deletion(-)
 create mode 100644 test/signal-logging.c

diff --git a/include/misc.h b/include/misc.h
index fea74b8..6ae020a 100644
--- a/include/misc.h
+++ b/include/misc.h
@@ -229,6 +229,8 @@ pad_to_int32(const int bytes)
 }
 
 extern char **xstrtokenize(const char *str, const char *separators);
+extern void FormatUInt64(uint64_t num, char *string);
+extern void FormatUInt64Hex(uint64_t num, char *string);
 
 /**
  * Compare the two version numbers comprising of major.minor.
diff --git a/os/utils.c b/os/utils.c
index 2f07548..998c3ed 100644
--- a/os/utils.c
+++ b/os/utils.c
@@ -1793,3 +1793,47 @@ xstrtokenize(const char *str, const char *separators)
     free(list);
     return NULL;
 }
+
+/* Format a number into a string in a signal safe manner. The string should be
+ * at least 21 characters in order to handle all uint64_t values. */
+void
+FormatUInt64(uint64_t num, char *string)
+{
+    uint64_t divisor;
+    int len;
+    int i;
+
+    for (len = 1, divisor = 10;
+         len < 20 && num / divisor;
+         len++, divisor *= 10);
+
+    for (i = len, divisor = 1; i > 0; i--, divisor *= 10)
+        string[i - 1] = '0' + ((num / divisor) % 10);
+
+    string[len] = '\0';
+}
+
+/* Format a number into a hexadecimal string in a signal safe manner. The string
+ * should be at least 17 characters in order to handle all uint64_t values. */
+void
+FormatUInt64Hex(uint64_t num, char *string)
+{
+    uint64_t divisor;
+    int len;
+    int i;
+
+    for (len = 1, divisor = 0x10;
+         len < 16 && num / divisor;
+         len++, divisor *= 0x10);
+
+    for (i = len, divisor = 1; i > 0; i--, divisor *= 0x10) {
+        int val = (num / divisor) % 0x10;
+
+        if (val < 10)
+            string[i - 1] = '0' + val;
+        else
+            string[i - 1] = 'a' + val - 10;
+    }
+
+    string[len] = '\0';
+}
diff --git a/test/.gitignore b/test/.gitignore
index 5d4fdfa..27eb254 100644
--- a/test/.gitignore
+++ b/test/.gitignore
@@ -7,3 +7,4 @@ touch
 xfree86
 xkb
 xtest
+signal-logging
diff --git a/test/Makefile.am b/test/Makefile.am
index b2a53aa..c049b26 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -5,7 +5,7 @@ if XORG
 # Tests that require at least some DDX functions in order to fully link
 # For now, requires xf86 ddx, could be adjusted to use another
 SUBDIRS += xi2
-noinst_PROGRAMS += xkb input xtest misc fixes xfree86  hashtabletest
+noinst_PROGRAMS += xkb input xtest misc fixes xfree86 hashtabletest signal-logging
 endif
 check_LTLIBRARIES = libxservertest.la
 
@@ -36,6 +36,7 @@ misc_LDADD=$(TEST_LDADD)
 fixes_LDADD=$(TEST_LDADD)
 xfree86_LDADD=$(TEST_LDADD)
 touch_LDADD=$(TEST_LDADD)
+signal_logging_LDADD=$(TEST_LDADD)
 hashtabletest_LDADD=$(TEST_LDADD) $(top_srcdir)/Xext/hashtable.c
 
 libxservertest_la_LIBADD = $(XSERVER_LIBS)
diff --git a/test/signal-logging.c b/test/signal-logging.c
new file mode 100644
index 0000000..8aab0dd
--- /dev/null
+++ b/test/signal-logging.c
@@ -0,0 +1,115 @@
+/**
+ * Copyright © 2012 Canonical, Ltd.
+ *
+ *  Permission is hereby granted, free of charge, to any person obtaining a
+ *  copy of this software and associated documentation files (the "Software"),
+ *  to deal in the Software without restriction, including without limitation
+ *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ *  and/or sell copies of the Software, and to permit persons to whom the
+ *  Software is furnished to do so, subject to the following conditions:
+ *
+ *  The above copyright notice and this permission notice (including the next
+ *  paragraph) shall be included in all copies or substantial portions of the
+ *  Software.
+ *
+ *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ *  DEALINGS IN THE SOFTWARE.
+ */
+
+#ifdef HAVE_DIX_CONFIG_H
+#include <dix-config.h>
+#endif
+
+#include <stdint.h>
+#include "assert.h"
+#include "misc.h"
+
+struct number_format_test {
+    uint64_t number;
+    char string[21];
+    char hex_string[17];
+};
+
+static Bool
+check_number_format_test(const struct number_format_test *test)
+{
+    char string[21];
+
+    FormatUInt64(test->number, string);
+    if(strncmp(string, test->string, 21) != 0) {
+        fprintf(stderr, "Failed to convert %ju to decimal string (%s vs %s)\n",
+                test->number, test->string, string);
+        return FALSE;
+    }
+    FormatUInt64Hex(test->number, string);
+    if(strncmp(string, test->hex_string, 17) != 0) {
+        fprintf(stderr,
+                "Failed to convert %ju to hexadecimal string (%s vs %s)\n",
+                test->number, test->hex_string, string);
+        return FALSE;
+    }
+
+    return TRUE;
+}
+
+static Bool
+number_formatting(void)
+{
+    int i;
+    struct number_format_test tests[] = {
+        { /* Zero */
+            0,
+            "0",
+            "0",
+        },
+        { /* Single digit number */
+            5,
+            "5",
+            "5",
+        },
+        { /* Two digit decimal number */
+            12,
+            "12",
+            "c",
+        },
+        { /* Two digit hex number */
+            37,
+            "37",
+            "25",
+        },
+        { /* Large < 32 bit number */
+            0xC90B2,
+            "823474",
+            "c90b2",
+        },
+        { /* Large > 32 bit number */
+            0x15D027BF211B37A,
+            "98237498237498234",
+            "15d027bf211b37a",
+        },
+        { /* Maximum 64-bit number */
+            0xFFFFFFFFFFFFFFFF,
+            "18446744073709551615",
+            "ffffffffffffffff",
+        },
+    };
+
+    for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++)
+        if (!check_number_format_test(tests + i))
+            return FALSE;
+
+    return TRUE;
+}
+
+int
+main(int argc, char **argv)
+{
+    int ok = number_formatting();
+
+    return ok ? 0 : 1;
+}
-- 
1.7.10.2



More information about the xorg-devel mailing list