From 8d85bd1e2f9473958b235caf7af9913b518f73dd Mon Sep 17 00:00:00 2001 From: Alan Coopersmith Date: Sat, 10 Dec 2022 14:07:18 -0800 Subject: Replace uTypedRecalloc() with direct recallocarray() calls Retains uRecalloc() as a fallback for platforms without recallocarray() Signed-off-by: Alan Coopersmith --- configure.ac | 2 +- keytypes.c | 10 +++++----- parseutils.c | 8 ++++---- symbols.c | 16 ++++++++-------- utils.c | 12 +++++++----- utils.h | 15 +++++++++------ 6 files changed, 34 insertions(+), 29 deletions(-) diff --git a/configure.ac b/configure.ac index bdf5589..b4c2d2c 100644 --- a/configure.ac +++ b/configure.ac @@ -50,7 +50,7 @@ if test ! -f "$srcdir/xkbparse.c"; then fi fi -AC_CHECK_FUNCS([reallocarray strdup strcasecmp]) +AC_CHECK_FUNCS([reallocarray recallocarray strdup strcasecmp]) # Checks for pkg-config packages PKG_CHECK_MODULES(XKBCOMP, [x11 xkbfile xproto >= 7.0.17]) diff --git a/keytypes.c b/keytypes.c index a8aaadd..707ee6f 100644 --- a/keytypes.c +++ b/keytypes.c @@ -525,9 +525,9 @@ NextMapEntry(KeyTypeInfo * type) else if (type->nEntries >= type->szEntries) { type->szEntries *= 2; - type->entries = uTypedRecalloc(type->entries, - type->nEntries, type->szEntries, - XkbKTMapEntryRec); + type->entries = recallocarray(type->entries, + type->nEntries, type->szEntries, + sizeof(XkbKTMapEntryRec)); if (type->entries == NULL) { ERROR("Couldn't reallocate map entries for %s\n", TypeTxt(type)); @@ -787,8 +787,8 @@ AddLevelName(KeyTypeInfo * type, { if ((type->lvlNames == NULL) || (type->szNames <= level)) { - type->lvlNames = - uTypedRecalloc(type->lvlNames, type->szNames, level + 1, Atom); + type->lvlNames = recallocarray(type->lvlNames, type->szNames, + level + 1, sizeof(Atom)); if (type->lvlNames == NULL) { ERROR("Couldn't allocate level names for type %s\n", diff --git a/parseutils.c b/parseutils.c index 7498b75..76876fb 100644 --- a/parseutils.c +++ b/parseutils.c @@ -650,10 +650,10 @@ AppendKeysymList(ExprDef * list, char *sym) if (list->value.list.nSyms >= list->value.list.szSyms) { list->value.list.szSyms *= 2; - list->value.list.syms = uTypedRecalloc(list->value.list.syms, - list->value.list.nSyms, - list->value.list.szSyms, - char *); + list->value.list.syms = recallocarray(list->value.list.syms, + list->value.list.nSyms, + list->value.list.szSyms, + sizeof(char *)); if (list->value.list.syms == NULL) { FATAL("Couldn't resize list of symbols for append\n"); diff --git a/symbols.c b/symbols.c index b026ce7..310ad07 100644 --- a/symbols.c +++ b/symbols.c @@ -297,18 +297,18 @@ ResizeKeyGroup(KeyInfo * key, if ((key->syms[group] == NULL) || tooSmall) { - key->syms[group] = uTypedRecalloc(key->syms[group], - key->numLevels[group], newWidth, - KeySym); + key->syms[group] = recallocarray(key->syms[group], + key->numLevels[group], newWidth, + sizeof(KeySym)); if (!key->syms[group]) return False; } if (((forceActions) && (tooSmall || (key->acts[group] == NULL))) || (tooSmall && (key->acts[group] != NULL))) { - key->acts[group] = uTypedRecalloc(key->acts[group], - key->numLevels[group], newWidth, - XkbAction); + key->acts[group] = recallocarray(key->acts[group], + key->numLevels[group], newWidth, + sizeof(XkbAction)); if (!key->acts[group]) return False; } @@ -607,8 +607,8 @@ AddKeySymbols(SymbolsInfo * info, KeyInfo * key, XkbDescPtr xkb) if (info->nKeys >= info->szKeys) { info->szKeys += SYMBOLS_CHUNK; - info->keys = - uTypedRecalloc(info->keys, info->nKeys, info->szKeys, KeyInfo); + info->keys = recallocarray(info->keys, info->nKeys, info->szKeys, + sizeof(KeyInfo)); if (!info->keys) { WSGO("Could not allocate key symbols descriptions\n"); diff --git a/utils.c b/utils.c index 61f04b6..120ef13 100644 --- a/utils.c +++ b/utils.c @@ -33,23 +33,25 @@ /***====================================================================***/ -Opaque -uRecalloc(Opaque old, unsigned nOld, unsigned nNew, unsigned itemSize) +#ifndef HAVE_RECALLOCARRAY +void * +uRecalloc(void *old, size_t nOld, size_t nNew, size_t itemSize) { char *rtrn; if (old == NULL) - rtrn = (char *) calloc(nNew, itemSize); + rtrn = calloc(nNew, itemSize); else { - rtrn = (char *) realloc((char *) old, nNew * itemSize); + rtrn = reallocarray(old, nNew, itemSize); if ((rtrn) && (nNew > nOld)) { bzero(&rtrn[nOld * itemSize], (nNew - nOld) * itemSize); } } - return (Opaque) rtrn; + return (void *) rtrn; } +#endif /***====================================================================***/ diff --git a/utils.h b/utils.h index 36f9e2a..1d33396 100644 --- a/utils.h +++ b/utils.h @@ -80,16 +80,19 @@ typedef int Comparison; #define reallocarray(p, n, s) realloc(p, (n) * (s)) #endif -extern Opaque uRecalloc(Opaque /* old */ , - unsigned /* nOld */ , - unsigned /* nNew */ , - unsigned /* newSize */ +#ifndef HAVE_RECALLOCARRAY +#define recallocarray uRecalloc + +extern void *uRecalloc(void * /* old */ , + size_t /* nOld */ , + size_t /* nNew */ , + size_t /* newSize */ ); +#endif + extern void uFree(Opaque /* ptr */ ); -#define uTypedRecalloc(pO,o,n,t) ((t *)uRecalloc((Opaque)pO,((unsigned)o),((unsigned)n),sizeof(t))) - /***====================================================================***/ extern Boolean uSetErrorFile(char * /* name */ -- cgit v1.2.3