Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v2: do not create uninitialized sway_keyboard #3189

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Hi-Angel
Copy link
Contributor

It's not good to create a uninitialized struct, because it might become
a possible error in future. I actually got hit by this while working on
#3155

v2: just move initialization inside the keyboard creation, leave the rest in place.

It's not good to create a uninitialized struct, because it might become
a possible error in future. I actually got hit by this while working on
swaywm#3155

Signed-off-by: Konstantin Kharlamov <[email protected]>
@emersion
Copy link
Member

I don't get this change. In both cases, sway_keyboard_configure is already called. Also, there's no uninitialized struct because we use calloc everywhere.

@Hi-Angel
Copy link
Contributor Author

Hi-Angel commented Nov 25, 2018

calloc can't initialize struct fields correctly. For example, there was some pointer which was set to zero, whilst in properly configured keyboard it should've pointed to something, which led to a crash for me until I figured that just after sway_keyboard_create() finished execution, the sway_keyboard is still partially uninitialized.

This change makes sure that if we ever created a sway_keyboard, it's properly initialized.

upd: s/uninitialized/partially uninitialized

@Hi-Angel
Copy link
Contributor Author

There's also a more general point (it doesn't immediately apply to this situation though) is that when you have two separate calls for struct creation and initialization, you need to create a temporary variable, and pass it between two calls. Often such design also makes problems with constifying variables.

@ghost
Copy link

ghost commented Nov 27, 2018

calloc can't initialize struct fields correctly. For example, there was some pointer which was set to zero, whilst in properly configured keyboard it should've pointed to something

Seems like you're using overloaded terminology here. An uninitialized struct will have fields of indeterminate value. calloc does initialize the struct with all zeroes, so the struct is already initialized. However, it sounds like your problem is you tried to dereference a pointer before it pointed to anything. What were you trying to dereference between calls of sway_keyboard_create and sway_keyboard_configure?

@Hi-Angel
Copy link
Contributor Author

Hi-Angel commented Nov 27, 2018

Seems like you're using overloaded terminology here. An uninitialized struct will have fields of indeterminate value. calloc does initialize the struct with all zeroes, so the struct is already initialized.

Ok, maybe I do. Although I'd argue there's little difference between a random value assigned to a field (i.e. uninitialized), and a wrong value assigned to the same field. But sure, a field initialized to wrong value is still initialized.

Still, terminology doesn't make this pull request less useful.

However, it sounds like your problem is you tried to dereference a pointer before it pointed to anything. What were you trying to dereference between calls of sway_keyboard_create and sway_keyboard_configure?

This line:

keyboard->default_kbd_layout =sway_keyboard_get_layout(wlr_device->keyboard->xkb_state);

The xkb_state is zero between the calls.

@ghost
Copy link

ghost commented Nov 27, 2018

Is there any reason you can't move that code after wlr_keyboard_set_keymap? Adding more calls to sway_keyboard_configure may or may not have additional side effects.

@Hi-Angel
Copy link
Contributor Author

Is there any reason you can't move that code after wlr_keyboard_set_keymap? Adding more calls to sway_keyboard_configure may or may not have additional side effects.

I kind of did, the diff I referred to in my prev. comment have this code after the wlr_keyboard_set_keymap(). Or do you mean, like, put it right after the call? (I put it to the end of the sway_keyboard_configure())?

@Hi-Angel
Copy link
Contributor Author

Hi-Angel commented Nov 27, 2018

@remyabel I think you misunderstood me. The code I referred to works right now, I just showed the line that per my memory resulted in crash back when the line was inside the sway_keyboard_create call.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants