After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 109446 - Gnome Print shouldn't use setlocale
Gnome Print shouldn't use setlocale
Status: RESOLVED FIXED
Product: gnome-print
Classification: Deprecated
Component: general
CVS
Other All
: Normal normal
: ---
Assigned To: Andreas J. Guelzow
Andreas J. Guelzow
Depends on:
Blocks:
 
 
Reported: 2003-03-28 19:41 UTC by Dominic Lachowicz
Modified: 2005-08-15 01:53 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Patch that addresses some of the problem (3.97 KB, patch)
2003-03-28 19:41 UTC, Dominic Lachowicz
none Details | Review
obsoletes older patch (3.96 KB, patch)
2003-03-28 20:13 UTC, Dominic Lachowicz
none Details | Review

Description Dominic Lachowicz 2003-03-28 19:41:00 UTC
Call isn't threadsafe, can wreak havoc with calling application, etc...
It's generally bad form to use these in libraries. In most cases you can
call g_ascii_dtostr and g_ascii_strtod instead
Comment 1 Dominic Lachowicz 2003-03-28 19:41:34 UTC
Created attachment 15278 [details] [review]
Patch that addresses some of the problem
Comment 2 Dominic Lachowicz 2003-03-28 20:13:33 UTC
Created attachment 15281 [details] [review]
obsoletes older patch
Comment 3 Dominic Lachowicz 2003-03-28 20:18:18 UTC
fortunately, the # of %f and %g formatting commands are few in number.
should be possible to use g_ascii_dtostr or g_ascii_formatd in their
places, use %s in the format strings, and remove the setlocale calls
inside of the _printf functions.
Comment 4 Kjartan Maraas 2003-06-14 14:41:25 UTC
Is this too controversial for 2.2.x?
Comment 5 Owen Taylor 2003-06-25 20:24:14 UTC
I don't think it is controversial, though it seems that it
might not be complete from Dom's comment.
Comment 6 Dominic Lachowicz 2003-06-25 20:25:27 UTC
no, it's certainly not controversial. my work here isn't complete,
though it is a big step in the right direction. it should still
probably be committed - in this case a partial solution is preferable
to none at all.
Comment 7 Andreas J. Guelzow 2003-11-19 22:48:24 UTC
Looking at the proposed patch, I don't think there is a guarantee that
in gnome_print_config_set_length sprintf does not write beyond the
length of the buffer. (It is very likely that unit->abbr is shorter
than  G_ASCII_DTOSTR_BUF_SIZE but that is the way to get into trouble.)

We should be using snprintf instead of sprintf to avoid these silly
problems.
Comment 8 Andreas J. Guelzow 2003-11-22 21:03:13 UTC
fixed the easily removable occurrences of setlocale and added warnings
to the others.
Comment 9 Morten Welinder 2003-11-23 21:25:24 UTC
Getting rid of setlocale isn't going to play well with working around
bugs in cups.  (Although setlocale is thread-safe when used to query.)
Comment 10 Andreas J. Guelzow 2003-11-23 21:44:42 UTC
I haven't touched those workarounds since we depend on them. There
were various other places were using g_ascii_... could avoid them or
the locale didn't really got into play.
Comment 11 Morten Welinder 2003-11-23 21:54:58 UTC
I imagine that we could do...

   old = g_strdup (setlocale (..., NULL));
   some_cups_call ();
   if (strcmp (old, setlocale (..., NULL)) {
      static gboolean warned = FALSE;
      if (!warned) {
         warned = TRUE;
         g_warning ("Working around locale bug in cups.");
      }
      setlocale (..., old);
   }
   g_free (old);

...which is almost thread-safe assuming that cups stops messing with
the locale.  ("Almost" because setlocale is not required to return
identical strings even for identical locales.)

It would also improve the chances of getting cups fixed, I bet.
(Last I looked I couldn't even find a bug reporting address.)
Comment 12 Andreas J. Guelzow 2003-12-02 16:24:49 UTC
I have implemented Morten's suggestion where we need to call setlocale
to protect against cups bugs.

Setlocale is still used in:

gf_pso_sprintf
gnome_print_pdf_fprintf
gnome_print_pdf_page_fprintf
gnome_print_ps2_fprintf
gnome_print_transport_printf

These are more difficult to replace. I guess we may have to look at
where they are called and avoid using them with locale sensitive
format strings.
Comment 13 Morten Welinder 2003-12-02 18:40:16 UTC
There seems to be just two problematic ones:

gnome-font-face.c:              gf_pso_sprintf (pso,
"%%!PS-TrueTypeFont-%g-%g\n", TTVersion, MfrRevision);
gnome-font-face.c:              gf_pso_sprintf (pso, "/FontBBox [%g %g
%g %g] def\n", bbox->x0, bbox->y0, bbox->x1, bbox->y1);

The former is trivial.  The latter does not look hard either.
Comment 14 alexander.winston 2004-01-03 18:27:46 UTC
Upgrading the priority to High because of the patches attached.
Comment 15 Dominic Lachowicz 2004-01-03 18:33:05 UTC
this doesn't need to be "high" any more. andreas and morten have done
a lot of work inside of gnome-print's CVS, and these patches are now
obsolete.
Comment 16 alexander.winston 2004-01-03 20:45:42 UTC
Is it safe to say that a patch is needed, then?
Comment 17 Dominic Lachowicz 2004-01-03 20:47:06 UTC
it's safe to say that some more work needs to be done, but the work
needed isn't "critical"
Comment 18 alexander.winston 2004-01-04 00:26:31 UTC
All right, so it would be okay if I switch out the PATCH keyword for
PATCH_NEEDED?
Comment 19 Andreas J. Guelzow 2004-01-04 22:59:56 UTC
What really needs to be done is to persuade the libcups people not to
change the locale in their library.
Comment 20 Morten Welinder 2004-01-06 22:45:03 UTC
See http://www.cups.org/str.php?L509+P0+S-2+C0+I0+E0+Q
Comment 21 Andreas J. Guelzow 2004-01-11 07:41:04 UTC
Okay all setlocale calls have been removed except:
1) calls that only retrieve locale information (eg. for LC_MESSAGES)
2) the cups bug workaround calls. Unfortunately since cups changes the
locale we have to reset it to the original value.
Comment 22 Morten Welinder 2004-01-13 03:16:31 UTC
The CUPS people (Michael Sweet in particular) tells me that current
versions of CUPS indeed do save and restore the locale across calls.
We should verify this and simply require such a fixed CUPS library,
IMHO.
Comment 23 Andreas J. Guelzow 2004-01-13 16:46:43 UTC
It seems that libcups 1.2 does not exhibit the problem (it did still
happen in 1.1.19).
Comment 24 Andreas J. Guelzow 2004-01-18 18:27:38 UTC
libcups 1.1.20 also suffices.

We have bumped the version requirement of libcups and removed all
setlocale calls with non-NULL second argument.