Skip to contentSkip to conversation

test-inspect: reset locale after gtk_init()

PATCH In review Davidson Francis 3 participants 4 messages Jul 5, 2020 Sparse checker

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 test-inspect: reset locale after gtk_init()

The test-inspect tool uses GTK to visualize symbol nodes. It turns out that gtk_init() implicitly sets the locale to the system locale, and since Sparse uses strtod()/strtold() for parsing floating-point numbers in expressions, parsing becomes locale-dependent.

Since the system's locale may be different from "C", test-inspect may be unable to parse float numbers.

Steps to reproduce:

    $ echo "int main(void){3.14;}" > test.c
    $ LC_ALL="fr_FR.UTF-8" test-inspect test.c
Output:
    test.c:1:16: error: constant 3.14 is not a valid number

Fix this by resetting the locale right after gtk_init().

---
 test-inspect.c | 2 ++
 1 file changed, 2 insertions(+)
test-inspect.c +2 -0
index 63754cb3..a59cd902 100644
@@ -6,6 +6,7 @@
 #include <ctype.h>
 #include <unistd.h>
 #include <fcntl.h>
+#include <locale.h>
 
 #include "lib.h"
 #include "allocate.h"
@@ -31,6 +32,7 @@ int main(int argc, char **argv)
 	struct symbol_list *view_syms = NULL;
 
 	gtk_init(&argc,&argv);
+	setlocale(LC_ALL, "C");
 	expand_symbols(sparse_initialize(argc, argv, &filelist));
 	FOR_EACH_PTR(filelist, file) {
 		struct symbol_list *syms = sparse(file);
Signature · 1 line
2.11.0
#1 lore raw
Quoted 5 lines · Davidson Francis

The test-inspect tool uses GTK to visualize symbol nodes. It turns out that gtk_init() implicitly sets the locale to the system locale, and since Sparse uses strtod()/strtold() for parsing floating-point numbers in expressions, parsing becomes locale-dependent.

We probably shouldn't be using strtod/strtold in the first place because of issues like this, but I think your patch is likely the simplest fix.

It _might_ be a good idea to limit it to LC_NUMERIC instead of LC_ALL, but who knows.. I'm not sure what else might be affected (and I'm not sure how good LC_NUMERIC support is on all platforms)

Linus

#2 lore raw
Quoted 6 lines · Linus Torvalds
Quoted 5 lines · Davidson Francis

The test-inspect tool uses GTK to visualize symbol nodes. It turns out that gtk_init() implicitly sets the locale to the system locale, and since Sparse uses strtod()/strtold() for parsing floating-point numbers in expressions, parsing becomes locale-dependent.

Oh yes, indeed :(
Thanks for the patch.

Quoted 3 lines

We probably shouldn't be using strtod/strtold in the first place because of issues like this, but I think your patch is likely the simplest fix.

Yes.

Quoted 3 lines

It _might_ be a good idea to limit it to LC_NUMERIC instead of LC_ALL, but who knows.. I'm not sure what else might be affected (and I'm not sure how good LC_NUMERIC support is on all platforms)

Well, checking the standard, I see that strtold() first strips whitespaces as defined by isspace() and isspace() itself depends on LC_CTYPE. So, for the moment, I prefer to take Davidson's patch as is this (OTOH, we already depend on isspace() anyway).

-- Luc

#3 lore raw
Quoted 7 lines · Luc Van Oostenryck
Quoted 5 lines · Linus Torvalds

It _might_ be a good idea to limit it to LC_NUMERIC instead of LC_ALL,
but who knows.. I'm not sure what else might be affected (and I'm not
sure how good LC_NUMERIC support is on all platforms)

Quoted 6 lines

Well, checking the standard, I see that strtold() first strips whitespaces as defined by isspace() and isspace() itself depends on LC_CTYPE. So, for the moment, I prefer to take Davidson's patch as is this (OTOH, we already depend on isspace() anyway).

-- Luc

I was also in doubt as to what would be the most appropriate option. Anyway, thanks for accepting the patch.

Regards,
Davidson Francis.