From 3d614c5379a28727a8bb5ec638bd75f33d12fe88 Mon Sep 17 00:00:00 2001 From: Kjell Wooding Date: Thu, 30 Mar 2006 18:32:08 +0000 Subject: Clean up some allocations. Remove malloc casts, and some easy malloc(A*B)->calloc changes. --- usr.bin/mg/echo.c | 4 ++-- usr.bin/mg/extend.c | 24 ++++++++++++++---------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/usr.bin/mg/echo.c b/usr.bin/mg/echo.c index fcc9beb1a2a..c600ebb449c 100644 --- a/usr.bin/mg/echo.c +++ b/usr.bin/mg/echo.c @@ -1,4 +1,4 @@ -/* $OpenBSD: echo.c,v 1.44 2006/03/28 20:16:24 kjell Exp $ */ +/* $OpenBSD: echo.c,v 1.45 2006/03/30 18:32:07 kjell Exp $ */ /* This file is in the public domain. */ @@ -937,7 +937,7 @@ copy_list(struct list *lp) last = NULL; while (lp) { - current = (struct list *)malloc(sizeof(struct list)); + current = malloc(sizeof(struct list)); if (current == NULL) { /* Free what we have allocated so far */ for (current = last; current; current = nxt) { diff --git a/usr.bin/mg/extend.c b/usr.bin/mg/extend.c index a18c7d094a6..492fd89bd32 100644 --- a/usr.bin/mg/extend.c +++ b/usr.bin/mg/extend.c @@ -1,17 +1,18 @@ -/* $OpenBSD: extend.c,v 1.44 2005/12/20 06:17:36 kjell Exp $ */ +/* $OpenBSD: extend.c,v 1.45 2006/03/30 18:32:07 kjell Exp $ */ /* This file is in the public domain. */ /* * Extended (M-X) commands, rebinding, and startup file processing. */ +#include +#include + #include "chrdef.h" #include "def.h" #include "kbd.h" #include "funmap.h" -#include - #ifndef NO_MACRO #include "macro.h" #endif /* !NO_MACRO */ @@ -115,7 +116,7 @@ remap(KEYMAP *curmap, /* pointer to the map being changed */ n2 = HUGE; if (n1 <= MAPELEDEF && n1 <= n2) { ele--; - if ((pfp = (PF *)malloc((c - ele->k_base + 1) * + if ((pfp = calloc(c - ele->k_base + 1, sizeof(PF))) == NULL) { ewprintf("Out of memory"); return (FALSE); @@ -129,7 +130,7 @@ remap(KEYMAP *curmap, /* pointer to the map being changed */ ele->k_num = c; ele->k_funcp = pfp; } else if (n2 <= MAPELEDEF) { - if ((pfp = (PF *)malloc((ele->k_num - c + 1) * + if ((pfp = calloc(ele->k_num - c + 1, sizeof(PF))) == NULL) { ewprintf("Out of memory"); return (FALSE); @@ -170,7 +171,7 @@ remap(KEYMAP *curmap, /* pointer to the map being changed */ if (pref_map != NULL) ele->k_prefmap = pref_map; else { - if ((mp = (KEYMAP *)malloc(sizeof(KEYMAP) + + if ((mp = malloc(sizeof(KEYMAP) + (MAPINIT - 1) * sizeof(struct map_element))) == NULL) { ewprintf("Out of memory"); ele->k_funcp[c - ele->k_base] = @@ -224,7 +225,7 @@ remap(KEYMAP *curmap, /* pointer to the map being changed */ if (curmap->map_num >= curmap->map_max && (curmap = reallocmap(curmap)) == NULL) return (FALSE); - if ((pfp = malloc((ele->k_num - c + !n2) * + if ((pfp = calloc(ele->k_num - c + !n2, sizeof(PF))) == NULL) { ewprintf("Out of memory"); return (FALSE); @@ -274,9 +275,12 @@ reallocmap(KEYMAP *curmap) KEYMAP *mp; int i; - if ((mp = (KEYMAP *)malloc((unsigned)(sizeof(KEYMAP) + - (curmap->map_max + (MAPGROW - 1)) * - sizeof(struct map_element)))) == NULL) { + if (curmap->map_max > SHRT_MAX - MAPGROW) { + ewprintf("keymap too large"); + return (NULL); + } + if ((mp = malloc(sizeof(KEYMAP) + (curmap->map_max + (MAPGROW - 1)) * + sizeof(struct map_element))) == NULL) { ewprintf("Out of memory"); return (NULL); } -- cgit v1.2.3