From: Lennart Poettering Date: Tue, 16 Jan 2018 10:48:25 +0000 (+0100) Subject: locale-util: add freelocale() cleanup helper X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=b1920f356ceea46f35512ec6e36724a9d468dbe7;p=elogind.git locale-util: add freelocale() cleanup helper --- 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 #include +//#include #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; }