locale-util: add freelocale() cleanup helper
authorLennart Poettering <lennart@poettering.net>
Tue, 16 Jan 2018 10:48:25 +0000 (11:48 +0100)
committerLennart Poettering <lennart@poettering.net>
Tue, 16 Jan 2018 10:53:43 +0000 (11:53 +0100)
src/basic/locale-util.h
src/basic/parse-util.c
src/import/curl-util.c

index 60ce017..f75dcbc 100644 (file)
@@ -22,6 +22,7 @@
 
 #include <libintl.h>
 #include <stdbool.h>
+#include <locale.h>
 
 #include "macro.h"
 
@@ -75,3 +76,10 @@ LocaleVariable locale_variable_from_string(const char *s) _pure_;
 
 int get_keymaps(char ***l);
 bool keymap_is_valid(const char *name);
+
+static inline void freelocalep(locale_t *p) {
+        if (*p == (locale_t) 0)
+                return;
+
+        freelocale(*p);
+}
index 33f94f3..ab80c2b 100644 (file)
@@ -28,6 +28,7 @@
 #include "alloc-util.h"
 #include "errno-list.h"
 #include "extract-word.h"
+#include "locale-util.h"
 #include "macro.h"
 #include "parse-util.h"
 #include "process-util.h"
@@ -531,9 +532,9 @@ int safe_atoi16(const char *s, int16_t *ret) {
 }
 
 int safe_atod(const char *s, double *ret_d) {
+        _cleanup_(freelocalep) locale_t loc = (locale_t) 0;
         char *x = NULL;
         double d = 0;
-        locale_t loc;
 
         assert(s);
         assert(ret_d);
@@ -544,16 +545,11 @@ int safe_atod(const char *s, double *ret_d) {
 
         errno = 0;
         d = strtod_l(s, &x, loc);
-        if (errno > 0) {
-                freelocale(loc);
+        if (errno > 0)
                 return -errno;
-        }
-        if (!x || x == s || *x) {
-                freelocale(loc);
+        if (!x || x == s || *x != 0)
                 return -EINVAL;
-        }
 
-        freelocale(loc);
         *ret_d = (double) d;
         return 0;
 }
index 7069c95..62bbaa5 100644 (file)
@@ -21,6 +21,7 @@
 #include "alloc-util.h"
 #include "curl-util.h"
 #include "fd-util.h"
+#include "locale-util.h"
 #include "string-util.h"
 
 static void curl_glue_check_finished(CurlGlue *g) {
@@ -414,8 +415,8 @@ int curl_header_strdup(const void *contents, size_t sz, const char *field, char
 }
 
 int curl_parse_http_time(const char *t, usec_t *ret) {
+        _cleanup_(freelocalep) locale_t loc = (locale_t) 0;
         const char *e;
-        locale_t loc;
         struct tm tm;
         time_t v;
 
@@ -434,7 +435,6 @@ int curl_parse_http_time(const char *t, usec_t *ret) {
         if (!e || *e != 0)
                 /* ANSI C */
                 e = strptime_l(t, "%a %b %d %H:%M:%S %Y", &tm, loc);
-        freelocale(loc);
         if (!e || *e != 0)
                 return -EINVAL;