From 39600d93860d202ef86e19cc6a7e0c5d5b589ae1 Mon Sep 17 00:00:00 2001
From: Simon McVittie <smcv@collabora.com>
Date: Tue, 16 Mar 2021 16:15:54 +0000
Subject: [PATCH] pv-wrap: Communicate timeouts to pv-adverb as
 locale-independent ASCII

These arguments take a G_OPTION_ARG_DOUBLE, which is documented to be
parsed using either the user's locale or the C locale (in fact they
use g_strtod(), which parses it both ways and takes the longer match).
The locale matters in locales like de_DE that use a decimal comma
instead of a decimal point: we can always parse 2.000000 in the C
locale, but we can only parse 2,000000 if we are in a correctly-set-up
locale that uses the decimal comma.

If we needed to generate new locale files inside the container, then
pv-adverb needs to be able to parse its command-line before we have
generated those locale files. This means that
--terminate-idle-timeout=2,000000 won't work reliably, so we need to
use the C locale format, --terminate-idle-timeout=2.000000.

Using g_ascii_dtostr() has the side benefit that it uses the shortest
possible representation that does not lose precision; in the common case
that the timeout is an integer number of seconds, we'll just print it
as an integer.

Helps: https://github.com/ValveSoftware/steam-runtime/issues/381
Signed-off-by: Simon McVittie <smcv@collabora.com>
---
 pressure-vessel/wrap.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/pressure-vessel/wrap.c b/pressure-vessel/wrap.c
index 0edada1ff..09c0a745e 100644
--- a/pressure-vessel/wrap.c
+++ b/pressure-vessel/wrap.c
@@ -2659,14 +2659,22 @@ main (int argc,
 
       if (opt_terminate_timeout >= 0.0)
         {
+          char idle_buf[G_ASCII_DTOSTR_BUF_SIZE] = {};
+          char timeout_buf[G_ASCII_DTOSTR_BUF_SIZE] = {};
+
+          g_ascii_dtostr (idle_buf, sizeof (idle_buf),
+                          opt_terminate_idle_timeout);
+          g_ascii_dtostr (timeout_buf, sizeof (timeout_buf),
+                          opt_terminate_timeout);
+
           if (opt_terminate_idle_timeout > 0.0)
             flatpak_bwrap_add_arg_printf (adverb_argv,
-                                          "--terminate-idle-timeout=%f",
-                                          opt_terminate_idle_timeout);
+                                          "--terminate-idle-timeout=%s",
+                                          idle_buf);
 
           flatpak_bwrap_add_arg_printf (adverb_argv,
-                                        "--terminate-timeout=%f",
-                                        opt_terminate_timeout);
+                                        "--terminate-timeout=%s",
+                                        timeout_buf);
         }
 
       flatpak_bwrap_add_args (adverb_argv,
-- 
GitLab