[libvirt] [PATCH v2] util: Extract locale-related fixes into separate functions

Peter Krempa posted 1 patch 6 years, 10 months ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/3f227002b4f620109da45dbe4f0110918f41a51a.1498221103.git.pkrempa@redhat.com
src/util/virstring.c | 114 +++++++++++++++++++++++++++++++++------------------
1 file changed, 75 insertions(+), 39 deletions(-)
[libvirt] [PATCH v2] util: Extract locale-related fixes into separate functions
Posted by Peter Krempa 6 years, 10 months ago
From: Martin Kletzander <mkletzan@redhat.com>

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
---
 src/util/virstring.c | 114 +++++++++++++++++++++++++++++++++------------------
 1 file changed, 75 insertions(+), 39 deletions(-)

v2:
- fixed Martin's laziness ... oh wait.

diff --git a/src/util/virstring.c b/src/util/virstring.c
index feea5be05..9b54bd6fb 100644
--- a/src/util/virstring.c
+++ b/src/util/virstring.c
@@ -521,19 +521,81 @@ virStrToLong_ullp(char const *s, char **end_ptr, int base,
 /* In case thread-safe locales are available */
 #if HAVE_NEWLOCALE

-static locale_t virLocale;
+typedef locale_t virLocale;
+static virLocale virLocaleRaw;

 static int
 virLocaleOnceInit(void)
 {
-    virLocale = newlocale(LC_ALL_MASK, "C", (locale_t)0);
-    if (!virLocale)
+    virLocaleRaw = newlocale(LC_ALL_MASK, "C", (locale_t)0);
+    if (!virLocaleRaw)
         return -1;
     return 0;
 }

 VIR_ONCE_GLOBAL_INIT(virLocale);
-#endif
+
+/**
+ * virLocaleSetRaw:
+ *
+ * @oldlocale: set to old locale pointer
+ *
+ * Sets the locale to 'C' to allow operating on non-localized objects.
+ * Returns 0 on success -1 on error.
+ */
+static int
+virLocaleSetRaw(virLocale *oldlocale)
+{
+    if (virLocaleInitialize() < 0)
+        return -1;
+    *oldlocale = uselocale(virLocaleRaw);
+    return 0;
+}
+
+static void
+virLocaleRevert(virLocale *oldlocale)
+{
+    uselocale(*oldlocale);
+}
+
+static void
+virLocaleFixupRadix(char **strp ATTRIBUTE_UNUSED)
+{
+}
+
+#else /* !HAVE_NEWLOCALE */
+
+typedef int virLocale;
+
+static int
+virLocaleSetRaw(virLocale *oldlocale ATTRIBUTE_UNUSED)
+{
+    return 0;
+}
+
+static void
+virLocaleRevert(virLocale *oldlocale ATTRIBUTE_UNUSED)
+{
+}
+
+static void
+virLocaleFixupRadix(char **strp)
+{
+    char *radix, *tmp;
+    struct lconv *lc;
+
+    lc = localeconv();
+    radix = lc->decimal_point;
+    tmp = strstr(*strp, radix);
+    if (tmp) {
+        *tmp = '.';
+        if (strlen(radix) > 1)
+            memmove(tmp + 1, tmp + strlen(radix), strlen(*strp) - (tmp - *strp));
+    }
+}
+
+#endif /* !HAVE_NEWLOCALE */
+

 /**
  * virStrToDouble
@@ -547,22 +609,17 @@ virStrToDouble(char const *s,
                char **end_ptr,
                double *result)
 {
+    virLocale oldlocale;
     double val;
     char *p;
     int err;

     errno = 0;
-#if HAVE_NEWLOCALE
-    locale_t old_loc;
-    if (virLocaleInitialize() < 0)
+    if (virLocaleSetRaw(&oldlocale) < 0)
         return -1;
-
-    old_loc = uselocale(virLocale);
-#endif
     val = strtod(s, &p); /* exempt from syntax-check */
-#if HAVE_NEWLOCALE
-    uselocale(old_loc);
-#endif
+    virLocaleRevert(&oldlocale);
+
     err = (errno || (!end_ptr && *p) || p == s);
     if (end_ptr)
         *end_ptr = p;
@@ -582,38 +639,17 @@ virStrToDouble(char const *s,
 int
 virDoubleToStr(char **strp, double number)
 {
+    virLocale oldlocale;
     int ret = -1;

-#if HAVE_NEWLOCALE
-
-    locale_t old_loc;
-
-    if (virLocaleInitialize() < 0)
-        goto error;
+    if (virLocaleSetRaw(&oldlocale) < 0)
+        return -1;

-    old_loc = uselocale(virLocale);
     ret = virAsprintf(strp, "%lf", number);
-    uselocale(old_loc);
-
-#else

-    char *radix, *tmp;
-    struct lconv *lc;
+    virLocaleRevert(&oldlocale);
+    virLocaleFixupRadix(strp);

-    if ((ret = virAsprintf(strp, "%lf", number) < 0))
-        goto error;
-
-    lc = localeconv();
-    radix = lc->decimal_point;
-    tmp = strstr(*strp, radix);
-    if (tmp) {
-        *tmp = '.';
-        if (strlen(radix) > 1)
-            memmove(tmp + 1, tmp + strlen(radix), strlen(*strp) - (tmp - *strp));
-    }
-
-#endif /* HAVE_NEWLOCALE */
- error:
     return ret;
 }

-- 
2.12.2

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH v2] util: Extract locale-related fixes into separate functions
Posted by Ján Tomko 6 years, 10 months ago
On Fri, Jun 23, 2017 at 02:32:39PM +0200, Peter Krempa wrote:
>From: Martin Kletzander <mkletzan@redhat.com>
>
>Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
>---
> src/util/virstring.c | 114 +++++++++++++++++++++++++++++++++------------------
> 1 file changed, 75 insertions(+), 39 deletions(-)

ACK

>
>v2:
>- fixed Martin's laziness ... oh wait.
>

CLOSED CANTFIX

Jan
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list