GNOME Bugzilla – Bug 671438
Gedit enters infinite loop on using %p in date/time in locale, which soesn't have AM/PM
Last modified: 2019-03-23 20:57:18 UTC
gedit 3.2.3-0ubuntu0.1 to reproduce: 1) Switch to Russian locale 2) Enable "Insert Date/Time" plugin 3) Set "%p" format in plugin preferences 4) Select Edit -> Insert Date/Time from menu for new document Result: gedit hangs up, stdout: GLib-ERROR **: /build/buildd/glib2.0-2.30.0/./glib/gmem.c:239: failed to allocate 1660940460 bytes Expected: No date is pasted instead of "%p" Stackktrace:
+ Trace 229817
It would seem this is cause by this loop: http://git.gnome.org/browse/gedit/tree/plugins/time/gedit-time-plugin.c?h=gnome-3-6#n385 Maybe a limit should be added to this loop. From the strftime documentation: "Note that the return value 0 does not necessarily indicate an error; for example, in many locales %p yields an empty string."
Created attachment 230865 [details] [review] Avoid ininite loop I've attached a patch that should avoid and infinite loop.
isn't there an errno we can check to tell the case of a failed strftime from the %p case?
There doesn't seem to be one because the %p case is not an error. Also when the memory size is to small there still seems to be no errno.
Created attachment 230911 [details] [review] Avoid-infinite-loop.patch Ok, this patch is much better. No magic numbers this time.
Created attachment 230912 [details] [review] Avoid-infinite-loop.patch Forgot to allocate memory in the last patch. This patch still needs some work as it doesn't fix the case where more than one %p is enter i.e if I enter %p%p and infinite loop is still entered.
Review of attachment 230912 [details] [review]: ::: plugins/time/gedit-time-plugin.c @@ -385,1 +386,5 @@ - do + out = g_malloc (out_length); + formated_time_length = strftime (out, out_length, locale_format, now); + ... 2 more ... What happens if one has two %p in the string format?... not that it makes much sense, but we could do this check inside the while loop no? Also, you should use g_ascii_strcasecmp for portability
Created attachment 232134 [details] [review] Avoid-infinite-loop v3 This patch uses g_ascii_strcasecmp and handles multiple instances of %p
I looked at the code and realized that we do not have to deal with all this complexity anymore: glib now has GDateTime which allows us to get a newly allocated utf8 string. Timothy, thanks for your efforts on this and sorry for now having took a deeper look before. I committed a rework of the code on master http://git.gnome.org/browse/gedit/commit/?id=121f02a3ea066565eeae5dd42868f822ecce4924 I tested with ru_RU and %p inserts " " without going in an infinite loop
I can confirm that Paolo's fix works correctly, thanks!