Merge pull request #1693 from mh21/wm-class-garbage-no-copy

Don't duplicate property value on class change.
This commit is contained in:
Michael Stapelberg 2015-05-06 23:55:47 -07:00
commit be2634c91f
5 changed files with 21 additions and 10 deletions

View File

@ -106,7 +106,7 @@ static int workspaces_string_cb(void *params_, const unsigned char *val, size_t
if (!strcmp(params->cur_key, "name")) { if (!strcmp(params->cur_key, "name")) {
const char *ws_name = (const char *)val; const char *ws_name = (const char *)val;
params->workspaces_walk->canonical_name = strndup(ws_name, len); params->workspaces_walk->canonical_name = sstrndup(ws_name, len);
if (config.strip_ws_numbers && params->workspaces_walk->num >= 0) { if (config.strip_ws_numbers && params->workspaces_walk->num >= 0) {
/* Special case: strip off the workspace number */ /* Special case: strip off the workspace number */

View File

@ -127,6 +127,13 @@ void *srealloc(void *ptr, size_t size);
*/ */
char *sstrdup(const char *str); char *sstrdup(const char *str);
/**
* Safe-wrapper around strndup which exits if strndup returns NULL (meaning that
* there is no more memory available)
*
*/
char *sstrndup(const char *str, size_t size);
/** /**
* Safe-wrapper around asprintf which exits if it returns -1 (meaning that * Safe-wrapper around asprintf which exits if it returns -1 (meaning that
* there is no more memory available) * there is no more memory available)

View File

@ -23,7 +23,7 @@ char *resolve_tilde(const char *path) {
char *head, *tail, *result; char *head, *tail, *result;
tail = strchr(path, '/'); tail = strchr(path, '/');
head = strndup(path, tail ? (size_t)(tail - path) : strlen(path)); head = sstrndup(path, tail ? (size_t)(tail - path) : strlen(path));
int res = glob(head, GLOB_TILDE, NULL, &globbuf); int res = glob(head, GLOB_TILDE, NULL, &globbuf);
free(head); free(head);

View File

@ -48,6 +48,13 @@ char *sstrdup(const char *str) {
return result; return result;
} }
char *sstrndup(const char *str, size_t size) {
char *result = strndup(str, size);
if (result == NULL)
err(EXIT_FAILURE, "strndup()");
return result;
}
int sasprintf(char **strp, const char *fmt, ...) { int sasprintf(char **strp, const char *fmt, ...) {
va_list args; va_list args;
int result; int result;

View File

@ -27,30 +27,27 @@ void window_update_class(i3Window *win, xcb_get_property_reply_t *prop, bool bef
* null-terminated strings (for compatibility reasons). Instead, we * null-terminated strings (for compatibility reasons). Instead, we
* use strdup() on both strings */ * use strdup() on both strings */
const size_t prop_length = xcb_get_property_value_length(prop); const size_t prop_length = xcb_get_property_value_length(prop);
char *new_class = smalloc(prop_length + 1); char *new_class = xcb_get_property_value(prop);
memcpy(new_class, xcb_get_property_value(prop), prop_length); const size_t class_class_index = strnlen(new_class, prop_length) + 1;
new_class[prop_length] = '\0';
FREE(win->class_instance); FREE(win->class_instance);
FREE(win->class_class); FREE(win->class_class);
win->class_instance = sstrdup(new_class); win->class_instance = sstrndup(new_class, prop_length);
if ((strlen(new_class) + 1) < prop_length) if (class_class_index < prop_length)
win->class_class = sstrdup(new_class + strlen(new_class) + 1); win->class_class = sstrndup(new_class + class_class_index, prop_length - class_class_index);
else else
win->class_class = NULL; win->class_class = NULL;
LOG("WM_CLASS changed to %s (instance), %s (class)\n", LOG("WM_CLASS changed to %s (instance), %s (class)\n",
win->class_instance, win->class_class); win->class_instance, win->class_class);
if (before_mgmt) { if (before_mgmt) {
free(new_class);
free(prop); free(prop);
return; return;
} }
run_assignments(win); run_assignments(win);
free(new_class);
free(prop); free(prop);
} }