AW: [PATCH 1/1] libbb: fix parse_duration_str with LOCALE_SUPPORT

Maxim Kochetkov fido_max at inbox.ru
Tue Mar 23 04:49:50 UTC 2021



22.03.2021 13:21, Walter Harms wrote:
> hi,
> 
> does setlocale("") reset to the last used locale  ?

Usually the locale is set by setting the LANG variable. In this case the 
LC_NUMERIC part of locale remains empty so it inherits from LANG. So it 
is ok to set it to C and then reset back to empty.

> 
> I would expect something like:
> old=getlocale()
> setlocale("C")
> strtod()
> setlocale(old);

In busybox getlocale() is not used at all. Using it may affect 
stability/performance/size - need to double check.

> 
> note:
> i had the same problems years ago and used
> strtod_l()
> with a static stored locale_t.

It seems nice but the same as getlocale() - strtod_l() is not used by 
busybox. Also musl/glibc/uclibc must be checked before such refactoring.


At this moment the pattern
setlocale(LC_NUMERIC, "C");
...
setlocale(LC_NUMERIC, "");
is already used in another parts of busybox. So I think it is better 
approach for now to fix this issue.

In future we can do refactoring by using strtod_l() instead of strtod() 
or getlocale()/setlocale().

> 
> re,
>   wh
> 
> ________________________________________
> Von: busybox <busybox-bounces at busybox.net> im Auftrag von Maxim Kochetkov <fido_max at inbox.ru>
> Gesendet: Montag, 22. März 2021 06:23:06
> An: busybox at busybox.net
> Betreff: [PATCH 1/1] libbb: fix parse_duration_str with LOCALE_SUPPORT
> 
> WARNUNG: Diese E-Mail kam von außerhalb der Organisation. Klicken Sie nicht auf Links oder öffnen Sie keine Anhänge, es sei denn, Sie kennen den/die Absender*in und wissen, dass der Inhalt sicher ist.
> 
> 
> Decimal dot may differs from ".", so we need to set LC_NUMERIC to "C" before
> processing duration string by strtod()
> 
> Signed-off-by: Maxim Kochetkov <fido_max at inbox.ru>
> ---
>   coreutils/sleep.c | 4 ----
>   libbb/duration.c  | 7 +++++++
>   2 files changed, 7 insertions(+), 4 deletions(-)
> 
> diff --git a/coreutils/sleep.c b/coreutils/sleep.c
> index 7bfaab920..2658e84df 100644
> --- a/coreutils/sleep.c
> +++ b/coreutils/sleep.c
> @@ -74,10 +74,6 @@ int sleep_main(int argc UNUSED_PARAM, char **argv)
>                          sleep(INT_MAX);
> 
>   #if ENABLE_FEATURE_FANCY_SLEEP
> -# if ENABLE_FLOAT_DURATION
> -       /* undo busybox.c setlocale */
> -       setlocale(LC_NUMERIC, "C");
> -# endif
>          duration = 0;
>          do {
>                  duration += parse_duration_str(*argv);
> diff --git a/libbb/duration.c b/libbb/duration.c
> index 086da15fb..cbbb7336d 100644
> --- a/libbb/duration.c
> +++ b/libbb/duration.c
> @@ -33,6 +33,10 @@ static const struct suffix_mult duration_suffixes[] ALIGN_SUFFIX = {
>   duration_t FAST_FUNC parse_duration_str(char *str)
>   {
>          duration_t duration;
> +# if ENABLE_LOCALE_SUPPORT
> +       /* undo busybox.c setlocale */
> +       setlocale(LC_NUMERIC, "C");
> +# endif
> 
>          if (strchr(str, '.')) {
>                  double d;
> @@ -54,6 +58,9 @@ duration_t FAST_FUNC parse_duration_str(char *str)
>                  duration = xatoul_sfx(str, duration_suffixes);
>          }
> 
> +#if ENABLE_LOCALE_SUPPORT
> +       setlocale(LC_NUMERIC, "");
> +#endif
>          return duration;
>   }
>   void FAST_FUNC sleep_for_duration(duration_t duration)
> --
> 2.30.2
> 
> _______________________________________________
> busybox mailing list
> busybox at busybox.net
> http://lists.busybox.net/mailman/listinfo/busybox
> 


More information about the busybox mailing list