diff mbox

[igt] overlay: Call setlocale around strtod

Message ID 20180327142805.18259-1-chris@chris-wilson.co.uk (mailing list archive)
State New, archived
Headers show

Commit Message

Chris Wilson March 27, 2018, 2:28 p.m. UTC
strtod() is locale-dependent. The decimal conversion depends on the radix
character ('.' for some of us like myself) varies by locale. As the
kernel reports its values using the "C" locale, we need to switch to
that when parsing; and switch back before reporting to the user.

Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=105712
Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
Cc: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
---
 overlay/power.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

Comments

Tvrtko Ursulin March 27, 2018, 2:42 p.m. UTC | #1
On 27/03/2018 15:28, Chris Wilson wrote:
> strtod() is locale-dependent. The decimal conversion depends on the radix
> character ('.' for some of us like myself) varies by locale. As the
> kernel reports its values using the "C" locale, we need to switch to
> that when parsing; and switch back before reporting to the user.
> 
> Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=105712
> Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
> Cc: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
> ---
>   overlay/power.c | 11 +++++++++--
>   1 file changed, 9 insertions(+), 2 deletions(-)
> 
> diff --git a/overlay/power.c b/overlay/power.c
> index 9ac90fde..0f99e2a4 100644
> --- a/overlay/power.c
> +++ b/overlay/power.c
> @@ -31,6 +31,7 @@
>   #include <time.h>
>   #include <errno.h>
>   #include <ctype.h>
> +#include <locale.h>
>   #include <math.h>
>   
>   #include "igt_perf.h"
> @@ -97,12 +98,18 @@ static uint64_t rapl_gpu_power(void)
>   
>   static double filename_to_double(const char *filename)
>   {
> -	char buf[64];
> +	char *oldlocale;
> +	char buf[80];
> +	double v;
>   
>   	if (filename_to_buf(filename, buf, sizeof(buf)))
>   		return 0;
>   
> -	return strtod(buf, NULL);
> +	oldlocale = setlocale(LC_ALL, "C");
> +	v = strtod(buf, NULL);
> +	setlocale(LC_ALL, oldlocale);
> +
> +	return v;
>   }
>   
>   static double rapl_gpu_power_scale(void)
> 

Reviewed-by: Tvrtko Ursulin <tvrtko.ursulin@intel.com>

Regards,

Tvrtko
diff mbox

Patch

diff --git a/overlay/power.c b/overlay/power.c
index 9ac90fde..0f99e2a4 100644
--- a/overlay/power.c
+++ b/overlay/power.c
@@ -31,6 +31,7 @@ 
 #include <time.h>
 #include <errno.h>
 #include <ctype.h>
+#include <locale.h>
 #include <math.h>
 
 #include "igt_perf.h"
@@ -97,12 +98,18 @@  static uint64_t rapl_gpu_power(void)
 
 static double filename_to_double(const char *filename)
 {
-	char buf[64];
+	char *oldlocale;
+	char buf[80];
+	double v;
 
 	if (filename_to_buf(filename, buf, sizeof(buf)))
 		return 0;
 
-	return strtod(buf, NULL);
+	oldlocale = setlocale(LC_ALL, "C");
+	v = strtod(buf, NULL);
+	setlocale(LC_ALL, oldlocale);
+
+	return v;
 }
 
 static double rapl_gpu_power_scale(void)