From c0a43a03122602105374f4ab80d8ccfdf8ddd235 Mon Sep 17 00:00:00 2001 From: Kjell Wooding Date: Thu, 21 Dec 2006 18:18:14 +0000 Subject: Fix a realloc-style bug (curmap = reallocmap(curmap)) in keymap repapping. spotted by theo --- usr.bin/mg/extend.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) (limited to 'usr.bin') diff --git a/usr.bin/mg/extend.c b/usr.bin/mg/extend.c index 54f551c1134..a7695171f94 100644 --- a/usr.bin/mg/extend.c +++ b/usr.bin/mg/extend.c @@ -1,4 +1,4 @@ -/* $OpenBSD: extend.c,v 1.48 2006/07/25 08:27:09 kjell Exp $ */ +/* $OpenBSD: extend.c,v 1.49 2006/12/21 18:18:13 kjell Exp $ */ /* This file is in the public domain. */ @@ -91,12 +91,14 @@ insert(int f, int n) * Structure assignments would come in real handy, but K&R based compilers * don't have them. Care is taken so running out of memory will leave * the keymap in a usable state. + * Parameters are: + * curmap: pointer to the map being changed + * c: character being changed + * funct: function being changed to + * pref_map: if funct==NULL, map to bind to or NULL for new */ static int -remap(KEYMAP *curmap, /* pointer to the map being changed */ - int c, /* character being changed */ - PF funct, /* function being changed to */ - KEYMAP *pref_map) /* if funct==NULL, map to bind to or NULL for new */ +remap(KEYMAP *curmap, int c, PF funct, KEYMAP *pref_map) { int i, n1, n2, nold; KEYMAP *mp, *newmap; @@ -222,9 +224,11 @@ remap(KEYMAP *curmap, /* pointer to the map being changed */ n2 = 1; for (i = 0; n2 && i < n1; i++) n2 &= ele->k_funcp[i] != NULL; - if (curmap->map_num >= curmap->map_max && - (curmap = reallocmap(curmap)) == NULL) - return (FALSE); + if (curmap->map_num >= curmap->map_max) { + if ((newmap = reallocmap(curmap)) == NULL) + return (FALSE); + curmap = newmap; + } if ((pfp = calloc(ele->k_num - c + !n2, sizeof(PF))) == NULL) { ewprintf("Out of memory"); @@ -266,7 +270,8 @@ remap(KEYMAP *curmap, /* pointer to the map being changed */ } /* - * Reallocate a keymap, used above. + * Reallocate a keymap. Returns NULL (without trashing the current map) + * on failure. */ static KEYMAP * reallocmap(KEYMAP *curmap) -- cgit v1.2.3