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

fix(studio): Ensure null-termination of layer name read from settings #2478

Merged
merged 1 commit into from
Sep 18, 2024

Conversation

xingrz
Copy link
Contributor

@xingrz xingrz commented Sep 14, 2024

This fixes the string leak when a layer name is changed to a longer one, but is discarded and reverted to the original shorter one from ZMK Studio.

@xingrz xingrz requested a review from a team as a code owner September 14, 2024 17:55
app/src/keymap.c Outdated
@@ -793,6 +793,8 @@ static int keymap_handle_set(const char *name, size_t len, settings_read_cb read
LOG_ERR("Failed to handle keymap layer name from settings (err %d)", err);
return err;
}

zmk_keymap_layer_names[layer][len] = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to be careful just in case someone has changed the CONFIG_ZMK_KEYMAP_LAYER_NAME_MAX_LEN value and lowered it since a setting was stored. This should probably be:

Suggested change
zmk_keymap_layer_names[layer][len] = 0;
zmk_keymap_layer_names[layer][MIN(len, CONFIG_ZMK_KEYMAP_LAYER_NAME_MAX_LEN - 1)] = 0;

To be safe.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe using zmk_keymap_layer_names[layer][err] would be better? According to the comment in settings_read_cb, it represents the number of bytes read.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The length of the stored string may be longer than the allocated memory into which we are loading it. So we need to be more conservative. See the previous comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but theoretically, err shouldn’t be longer than the length passed to read_cb, which is already MIN(len, CONFIG_ZMK_KEYMAP_LAYER_NAME_MAX_LEN - 1).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh indeed! That should be fine.

This fixes the string leak when a layer name is changed to a longer one,
but is discarded and reverted to the original shorter one from ZMK Studio.
Copy link
Contributor

@petejohanson petejohanson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@petejohanson petejohanson merged commit 62900c6 into zmkfirmware:main Sep 18, 2024
47 checks passed
@xingrz xingrz deleted the pr/fix-layer-name-null-termi branch September 19, 2024 02:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants