summaryrefslogtreecommitdiff
path: root/src/basic
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2018-01-16 11:48:25 +0100
committerSven Eden <yamakuzure@gmx.net>2018-05-30 07:50:09 +0200
commitb1920f356ceea46f35512ec6e36724a9d468dbe7 (patch)
tree09bca042f254bb0851af6c756f8ead8c285dfa52 /src/basic
parentf1eacb3a95b1c9ed8869cdc3345546fba266f170 (diff)
locale-util: add freelocale() cleanup helper
Diffstat (limited to 'src/basic')
-rw-r--r--src/basic/locale-util.h8
-rw-r--r--src/basic/parse-util.c12
2 files changed, 12 insertions, 8 deletions
diff --git a/src/basic/locale-util.h b/src/basic/locale-util.h
index 60ce017a1..4542770b1 100644
--- a/src/basic/locale-util.h
+++ b/src/basic/locale-util.h
@@ -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);
+}
diff --git a/src/basic/parse-util.c b/src/basic/parse-util.c
index ea902ed96..95caf2805 100644
--- a/src/basic/parse-util.c
+++ b/src/basic/parse-util.c
@@ -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;
}