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 790453 - Groups not assigned in order while setting user layouts
Groups not assigned in order while setting user layouts
Status: RESOLVED FIXED
Product: gnome-flashback
Classification: Other
Component: input-sources
unspecified
Other Linux
: Normal minor
: ---
Assigned To: GNOME Flashback Maintainers
GNOME Flashback Maintainers
Depends on:
Blocks:
 
 
Reported: 2017-11-16 16:45 UTC by Ignacy Gawędzki
Modified: 2018-05-21 14:39 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Patch to assign groups to layouts in order. (3.49 KB, patch)
2017-11-16 16:45 UTC, Ignacy Gawędzki
none Details | Review
Patch to assign groups to layouts in order. (3.45 KB, patch)
2018-05-21 10:42 UTC, Ignacy Gawędzki
none Details | Review
Patch to assign groups to layouts in order. (3.75 KB, patch)
2018-05-21 10:57 UTC, Ignacy Gawędzki
none Details | Review
Patch to assign groups to layouts in order. (3.75 KB, patch)
2018-05-21 13:59 UTC, Ignacy Gawędzki
none Details | Review
Original Patch with Formatting fixes and contains check (3.61 KB, patch)
2018-05-21 14:11 UTC, Sebastian
committed Details | Review

Description Ignacy Gawędzki 2017-11-16 16:45:22 UTC
Created attachment 363850 [details] [review]
Patch to assign groups to layouts in order.

When layouts are set in gf_keyboard_manager_set_user_layouts, the LayoutInfo instances are first inserted in the hash table and then the table elements are iterated on and groups are assigned.  This results in groups being assigned regardless of the order of user layouts, but rather in the order their names happen to be hashed.

One case where this becomes a problem is with SDL which assumes the layout in Group1 is the main one, in order to generate keycodes.

The attached patch fixes this by assigning groups while inserting the layouts in the hash table, with no need to iterate on the table entries afterwards.
Comment 1 Dmitry Shachnev 2018-05-17 11:26:47 UTC
Review of attachment 363850 [details] [review]:

I tested this patch and I can confirm that my configuration (English and Russian keyboard layouts, switch using Super+Space, fast switch using CapsLock) works fine with it.
Comment 2 Alberts Muktupāvels 2018-05-20 12:39:37 UTC
Review of attachment 363850 [details] [review]:

Please update patch to not use tabs!
Comment 3 Sebastian 2018-05-20 21:02:53 UTC
Tested the patch and it is crashing for me immediately with a segmentation fault.

(gdb) backtrace
  • #0 __strlen_avx2
    at ../sysdeps/x86_64/multiarch/strlen-avx2.S line 93
  • #1 g_strdup
  • #2 get_layouts_string
    at gf-keyboard-manager.c line 258
  • #3 apply_layout_group
    at gf-keyboard-manager.c line 320
  • #4 gf_keyboard_manager_apply
    at gf-keyboard-manager.c line 616
  • #5 activate_cb
    at gf-input-source-manager.c line 625
  • #6 g_cclosure_marshal_VOID__BOOLEANv
  • #7 0x00007f6a03b261a6 in
  • #8 g_signal_emit_valist
  • #9 g_signal_emit
  • #10 gf_input_source_activate
    at gf-input-source.c line 353
  • #11 update_mru_sources_list
    at gf-input-source-manager.c line 857
  • #12 sources_changed_cb
    at gf-input-source-manager.c line 945


(gdb) p *info
$21 = {id = 0x56054f2243e0 "de", layout = 0x56054f12ee30 "de", variant = 0x56054f3fdce0 "", group = 0x56054f08bd70, index = 1}
(gdb) p *info->group
$22 = (LayoutInfo *) 0x56054f3cd850
(gdb) p *info->group[0]
$23 = {id = 0x0, layout = 0x21 <error: Cannot access memory at address 0x21>, variant = 0x56054f3ce510 " ", group = 0x56054f08bd70, index = 0}
(gdb) p *info->group[1]
$24 = {id = 0x56054f08bd60 "", layout = 0x56054f40cad0 "\360\336?O\005V", variant = 0x0, group = 0x21, index = 1329585664}
(gdb) p *info->group[2]
$25 = {id = 0x56054f3fdcc0 "us", layout = 0x56054f3fde70 "us", variant = 0x56054f08bf90 "", group = 0x56054f08bd70, index = 2}

I currently have the following layouts configured:

 1. German
 2. English (US)
 3. Chinese (Intelligent Pinyin)
 4. Chinese (SunPinyin)

I cannot get it to crash with the released version.
Comment 4 Ignacy Gawędzki 2018-05-21 10:42:10 UTC
Created attachment 372280 [details] [review]
Patch to assign groups to layouts in order.
Comment 5 Ignacy Gawędzki 2018-05-21 10:44:01 UTC
Okay, the crash was triggered by setups that happened to have twice the same layout in the set during gf_keyboard_manager_set_user_layouts.

The new attached patch addresses that and uses spaces instead of tabs.
Comment 6 Alberts Muktupāvels 2018-05-21 10:47:52 UTC
Did you attach old patch?
Comment 7 Alberts Muktupāvels 2018-05-21 10:52:41 UTC
Also that crash is not caused by your patch, if you have fix then please as separate patch.
Comment 8 Ignacy Gawędzki 2018-05-21 10:57:00 UTC
Created attachment 372281 [details] [review]
Patch to assign groups to layouts in order.
Comment 9 Ignacy Gawędzki 2018-05-21 11:06:14 UTC
I just uploaded the right patch.  Sorry for the mess.

The crash *was* caused by my previous patch because it did not exactly do what it said: if a layout is added twice, the second instance replaces the first in the hash table, and thus assigning layouts to groups as they are inserted will result in a layout in the group pointing to unallocated memory (the first instance that has been freed by the insertion of the second one).  Without the previous patch, the layouts are assigned to groups by iterating over the entries in the hash table after all layouts have been inserted/replaced, so even if they are in incorrect order, no layout in the group points to unallocated memory.

The new patch simply first checks whether the layout currently being added is already in the hash table.  If so, only the layout name and variant name are replaced, (if, for whatever reason, they are different from the previous instance).  Otherwise, it is assigned to the group and inserted in the hash table.
Comment 10 Alberts Muktupāvels 2018-05-21 11:18:55 UTC
(In reply to Ignacy Gawędzki from comment #9)
> I just uploaded the right patch.  Sorry for the mess.
> 
> The crash *was* caused by my previous patch because it did not exactly do
> what it said: if a layout is added twice, the second instance replaces the
> first in the hash table, and thus assigning layouts to groups as they are
> inserted will result in a layout in the group pointing to unallocated memory
> (the first instance that has been freed by the insertion of the second one).
> Without the previous patch, the layouts are assigned to groups by iterating
> over the entries in the hash table after all layouts have been
> inserted/replaced, so even if they are in incorrect order, no layout in the
> group points to unallocated memory.

Right, but still it is not problem with your patch. :) It just helped to find bug.

> The new patch simply first checks whether the layout currently being added
> is already in the hash table.  If so, only the layout name and variant name
> are replaced, (if, for whatever reason, they are different from the previous
> instance).  Otherwise, it is assigned to the group and inserted in the hash
> table.

No, please patch without that. First GNOME Control Center does not add duplicates so I dont think we should worry about that. Even if we want then I think we should just skip next duplicate. if (info != NULL) continue; but then again it would just hide bug again...

The problem here is that mentioned input sources are not duplicates and they all should work, nothing should be removed.
Comment 11 Alberts Muktupāvels 2018-05-21 13:21:33 UTC
Review of attachment 372281 [details] [review]:

::: gnome-flashback/libinput-sources/gf-keyboard-manager.c
@@ +566,3 @@
+          info = (LayoutInfo *) g_hash_table_lookup (manager->layout_infos,
+                                                     ids[i]);
+          if (info != NULL)

empty space before this line.

@@ +572,3 @@
+              g_free (info->variant);
+              info->variant = g_strdup (variant);
+              continue;

I think it enough to just use continue.For same id you will get same layout and variant so there is no need to free & g_strdup.
Comment 12 Ignacy Gawędzki 2018-05-21 13:54:40 UTC
(In reply to Alberts Muktupāvels from comment #10)
> (In reply to Ignacy Gawędzki from comment #9)
> > I just uploaded the right patch.  Sorry for the mess.
> > 
> > The crash *was* caused by my previous patch because it did not exactly do
> > what it said: if a layout is added twice, the second instance replaces the
> > first in the hash table, and thus assigning layouts to groups as they are
> > inserted will result in a layout in the group pointing to unallocated memory
> > (the first instance that has been freed by the insertion of the second one).
> > Without the previous patch, the layouts are assigned to groups by iterating
> > over the entries in the hash table after all layouts have been
> > inserted/replaced, so even if they are in incorrect order, no layout in the
> > group points to unallocated memory.
> 
> Right, but still it is not problem with your patch. :) It just helped to
> find bug.

Nevertheless, with that bug present, the first patch introduced a behavior that leads to a crash in some situations, which is obviously not desirable.

My goal is to fix one aspect of the software that annoys me without breaking it for others.

> > The new patch simply first checks whether the layout currently being added
> > is already in the hash table.  If so, only the layout name and variant name
> > are replaced, (if, for whatever reason, they are different from the previous
> > instance).  Otherwise, it is assigned to the group and inserted in the hash
> > table.
> 
> No, please patch without that. First GNOME Control Center does not add
> duplicates so I dont think we should worry about that. Even if we want then
> I think we should just skip next duplicate. if (info != NULL) continue; but
> then again it would just hide bug again...
> 
> The problem here is that mentioned input sources are not duplicates and they
> all should work, nothing should be removed.

Fine then, I can simply skip duplicates.
Comment 13 Ignacy Gawędzki 2018-05-21 13:59:22 UTC
Created attachment 372290 [details] [review]
Patch to assign groups to layouts in order.
Comment 14 Alberts Muktupāvels 2018-05-21 14:02:14 UTC
Review of attachment 372290 [details] [review]:

Again wrong patch?

::: gnome-flashback/libinput-sources/gf-keyboard-manager.c
@@ +573,3 @@
+              info->variant = g_strdup (variant);
+              continue;
+            }

if (g_hash_table_contains (manager->layout_infos, ids[i]))
  continue;
Comment 15 Alberts Muktupāvels 2018-05-21 14:04:00 UTC
Review of attachment 372290 [details] [review]:

Again wrong patch?

::: gnome-flashback/libinput-sources/gf-keyboard-manager.c
@@ +573,3 @@
+              info->variant = g_strdup (variant);
+              continue;
+            }

if (g_hash_table_contains (manager->layout_infos, ids[i]))
  continue;
Comment 16 Sebastian 2018-05-21 14:11:25 UTC
Created attachment 372291 [details] [review]
Original Patch with Formatting fixes and contains check
Comment 17 Ignacy Gawędzki 2018-05-21 14:24:47 UTC
(In reply to Alberts Muktupāvels from comment #15)
> Review of attachment 372290 [details] [review] [review]:
> 
> Again wrong patch?
> 
> ::: gnome-flashback/libinput-sources/gf-keyboard-manager.c
> @@ +573,3 @@
> +              info->variant = g_strdup (variant);
> +              continue;
> +            }
> 
> if (g_hash_table_contains (manager->layout_infos, ids[i]))
>   continue;

Yeah, well... I notoriously forget to add -p to git amend. :/
Comment 18 Ignacy Gawędzki 2018-05-21 14:25:47 UTC
Review of attachment 372291 [details] [review]:

Look fine to me.  Thanks for the correction.
Comment 19 Alberts Muktupāvels 2018-05-21 14:39:14 UTC
Thanks! Sorry that this took so long...