From 8a91517ca6ea77633476595b0eb5b213357c60e5 Mon Sep 17 00:00:00 2001 From: Alan Coopersmith Date: Sun, 8 Oct 2023 13:22:13 -0700 Subject: xkbtext.c: Add tbGetBufferString helper function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Handles common case of allocating & copying string to temporary buffer As a side effect, clears up gcc warning: xkbtext.c: In function ‘XkbBehaviorText’: xkbtext.c:1476:19: warning: declaration of ‘tmp’ shadows a previous local [-Wshadow] 1476 | char *tmp; | ^~~ xkbtext.c:1457:21: note: shadowed declaration is here 1457 | char buf[256], *tmp; | ^~~ Signed-off-by: Alan Coopersmith --- src/xkbtext.c | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/xkbtext.c b/src/xkbtext.c index fe30d2b..d3a37a1 100644 --- a/src/xkbtext.c +++ b/src/xkbtext.c @@ -68,6 +68,20 @@ tbGetBuffer(unsigned size) /***====================================================================***/ +static inline char * +tbGetBufferString(const char *str) +{ + size_t size = strlen(str) + 1; + char *rtrn = tbGetBuffer((unsigned) size); + + if (rtrn != NULL) + memcpy(rtrn, str, size); + + return rtrn; +} + +/***====================================================================***/ + char * XkbAtomText(Display *dpy, Atom atm, unsigned format) { @@ -236,7 +250,6 @@ static const char *modNames[XkbNumModifiers] = { char * XkbModIndexText(unsigned ndx, unsigned format) { - char *rtrn; char buf[100]; if (format == XkbCFile) { @@ -255,9 +268,7 @@ XkbModIndexText(unsigned ndx, unsigned format) else snprintf(buf, sizeof(buf), "ILLEGAL_%02x", ndx); } - rtrn = tbGetBuffer(strlen(buf) + 1); - strcpy(rtrn, buf); - return rtrn; + return tbGetBufferString(buf); } char * @@ -548,9 +559,7 @@ XkbNKNDetailMaskText(unsigned detail, unsigned format) tmp = "0"; else if (format == XkbMessage) tmp = "none"; - buf = tbGetBuffer(strlen(tmp) + 1); - strcpy(buf, tmp); - return buf; + return tbGetBufferString(tmp); } else if ((detail & XkbAllNewKeyboardEventsMask) == XkbAllNewKeyboardEventsMask) { @@ -560,9 +569,7 @@ XkbNKNDetailMaskText(unsigned detail, unsigned format) tmp = "XkbAllNewKeyboardEventsMask"; else tmp = "all"; - buf = tbGetBuffer(strlen(tmp) + 1); - strcpy(buf, tmp); - return buf; + return tbGetBufferString(tmp); } if (format == XkbMessage) { prefix = ""; @@ -1424,7 +1431,7 @@ static actionCopy copyActionArgs[XkbSA_NumActions] = { char * XkbActionText(Display *dpy, XkbDescPtr xkb, XkbAction *action, unsigned format) { - char buf[ACTION_SZ], *tmp; + char buf[ACTION_SZ]; int sz; if (format == XkbCFile) { @@ -1445,16 +1452,13 @@ XkbActionText(Display *dpy, XkbDescPtr xkb, XkbAction *action, unsigned format) CopyOtherArgs(dpy, xkb, action, buf, &sz); TryCopyStr(buf, ")", &sz); } - tmp = tbGetBuffer(strlen(buf) + 1); - if (tmp != NULL) - strcpy(tmp, buf); - return tmp; + return tbGetBufferString(buf); } char * XkbBehaviorText(XkbDescPtr xkb, XkbBehavior * behavior, unsigned format) { - char buf[256], *tmp; + char buf[256]; if (format == XkbCFile) { if (behavior->type == XkbKB_Default) @@ -1509,10 +1513,7 @@ XkbBehaviorText(XkbDescPtr xkb, XkbBehavior * behavior, unsigned format) snprintf(buf, sizeof(buf), "overlay%d= %s", ndx, kn); } } - tmp = tbGetBuffer(strlen(buf) + 1); - if (tmp != NULL) - strcpy(tmp, buf); - return tmp; + return tbGetBufferString(buf); } /***====================================================================***/ -- cgit v1.2.3