From 6afd4ae4473e7c1701ae3be9813ca2857395f2a5 Mon Sep 17 00:00:00 2001 From: Alexander Golubev Date: Sun, 17 Mar 2024 03:51:37 +0300 Subject: Add XkbRF_FreeVarDefs() a destructor for XkbRF_VarDefsRec. The function properly frees memory allocated for XkbRF_VarDefsRec by XkbRF_GetNamesProp(). Based on original contribution by Martin Weber Closes: https://gitlab.freedesktop.org/xorg/lib/libxkbfile/-/issues/6 Signed-off-by: Alexander Golubev --- include/X11/extensions/XKBrules.h | 12 ++++++++++++ src/maprules.c | 28 +++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/include/X11/extensions/XKBrules.h b/include/X11/extensions/XKBrules.h index 4736056..8580b55 100644 --- a/include/X11/extensions/XKBrules.h +++ b/include/X11/extensions/XKBrules.h @@ -191,6 +191,18 @@ extern Bool XkbRF_SetNamesProp( XkbRF_VarDefsPtr /* var_defs */ ); +/** + * @brief Destroy @struct XkbRF_VarDefsRec + * @param[in] var_defs - a XkbRF_VarDefsRec to clean up + * @param[in] freeVarDefs - if @c True also free the @p var_defs itself + * + * Frees the memory associated with the @p var_defs. If @p free_var_defs is + * @c True also frees the @p var_defs pointer itself. + */ +extern void XkbRF_FreeVarDefs( + XkbRF_VarDefsPtr /* var_defs */, + Bool /* freeVarDefs */ +); _XFUNCPROTOEND diff --git a/src/maprules.c b/src/maprules.c index bd83f71..7e5bbd5 100644 --- a/src/maprules.c +++ b/src/maprules.c @@ -1458,7 +1458,10 @@ XkbRF_GetNamesProp(Display * dpy, char **rf_rtrn, XkbRF_VarDefsPtr vd_rtrn) vd_rtrn->options = _XkbDupString(out); out += strlen(out) + 1; } - + /* + * For now extra_options and extra_rules are not used + * update XkbRF_FreeVarDefs if that changes + */ XFree(data); return True; } @@ -1528,3 +1531,26 @@ XkbRF_SetNamesProp(Display *dpy, char *rules_file, XkbRF_VarDefsPtr var_defs) return True; } +void +XkbRF_FreeVarDefs(XkbRF_VarDefsPtr var_defs, Bool freeVarDefs) +{ + if (!var_defs) + return; + + _XkbFree(var_defs->model); + var_defs->model = NULL; + _XkbFree(var_defs->layout); + var_defs->layout = NULL; + _XkbFree(var_defs->variant); + var_defs->variant = NULL; + _XkbFree(var_defs->options); + var_defs->options = NULL; + + _XkbFree(var_defs->extra_names); + var_defs->extra_names = NULL; + _XkbFree(var_defs->extra_values); + var_defs->extra_values = NULL; + + if(freeVarDefs) + _XkbFree(var_defs); +} -- cgit v1.2.3