chiark / gitweb /
locale-util: add freelocale() cleanup helper
authorLennart Poettering <lennart@poettering.net>
Tue, 16 Jan 2018 10:48:25 +0000 (11:48 +0100)
committerSven Eden <yamakuzure@gmx.net>
Wed, 30 May 2018 05:50:09 +0000 (07:50 +0200)
src/basic/locale-util.h
src/basic/parse-util.c

index 60ce017a1519cf8e8eaa8e35394a9ea46f23add0..4542770b15758629ef26673cfb5a1999aaf11f23 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 ea902ed96e929939fa58424ac6b5f8c0fbb4e0c6..95caf2805d924a7b82e6b2076abf983ddf75fefd 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"
@@ -536,9 +537,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);
@@ -549,16 +550,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;
 }