make use of en*alloc functions

This commit is contained in:
Jakob Kramer 2015-02-11 02:08:17 +01:00 committed by sin
parent c0a3c66a84
commit 0fcad66c75
3 changed files with 8 additions and 25 deletions

8
expr.c
View File

@ -115,9 +115,7 @@ match(Val vstr, Val vregx)
str = valstr(vstr, buf1, sizeof(buf1)); str = valstr(vstr, buf1, sizeof(buf1));
regx = valstr(vregx, buf2, sizeof(buf2)); regx = valstr(vregx, buf2, sizeof(buf2));
anchreg = malloc(strlen(regx) + 2); anchreg = enmalloc(3, strlen(regx) + 2);
if (!anchreg)
enprintf(3, "malloc:");
snprintf(anchreg, strlen(regx) + 2, "^%s", regx); snprintf(anchreg, strlen(regx) + 2, "^%s", regx);
enregcomp(3, &re, anchreg, 0); enregcomp(3, &re, anchreg, 0);
@ -131,9 +129,7 @@ match(Val vstr, Val vregx)
if (re.re_nsub) { if (re.re_nsub) {
regfree(&re); regfree(&re);
len = matches[1].rm_eo - matches[1].rm_so + 1; len = matches[1].rm_eo - matches[1].rm_so + 1;
ret = malloc(len); ret = enmalloc(3, len);
if (!ret)
enprintf(3, "malloc:");
strlcpy(ret, str + matches[1].rm_so, len); strlcpy(ret, str + matches[1].rm_so, len);
d = strtoimax(ret, &p, 10); d = strtoimax(ret, &p, 10);
if (*ret && !*p) { if (*ret && !*p) {

16
grep.c
View File

@ -162,18 +162,14 @@ addpattern(const char *pattern)
pattern = "."; pattern = ".";
if (!Fflag && xflag) { if (!Fflag && xflag) {
tmp = malloc(strlen(pattern) + 3); tmp = enmalloc(Error, strlen(pattern) + 3);
if (!tmp)
enprintf(Error, "malloc:");
snprintf(tmp, strlen(pattern) + 3, "%s%s%s", snprintf(tmp, strlen(pattern) + 3, "%s%s%s",
pattern[0] == '^' ? "" : "^", pattern[0] == '^' ? "" : "^",
pattern, pattern,
pattern[strlen(pattern) - 1] == '$' ? "" : "$"); pattern[strlen(pattern) - 1] == '$' ? "" : "$");
} else if (!Fflag && wflag) { } else if (!Fflag && wflag) {
len = strlen(pattern) + 5 + (Eflag ? 2 : 4); len = strlen(pattern) + 5 + (Eflag ? 2 : 4);
tmp = malloc(len); tmp = enmalloc(Error, len);
if (!tmp)
enprintf(Error, "malloc:");
bol = eol = 0; bol = eol = 0;
if (pattern[0] == '^') if (pattern[0] == '^')
@ -188,14 +184,10 @@ addpattern(const char *pattern)
Eflag ? ")" : "\\)", Eflag ? ")" : "\\)",
eol ? "$" : ""); eol ? "$" : "");
} else { } else {
tmp = strdup(pattern); tmp = enstrdup(Error, pattern);
if (!tmp)
enprintf(Error, "strdup:");
} }
pnode = malloc(sizeof(*pnode)); pnode = enmalloc(Error, sizeof(*pnode));
if (!pnode)
enprintf(Error, "malloc:");
pnode->pattern = tmp; pnode->pattern = tmp;
SLIST_INSERT_HEAD(&phead, pnode, entry); SLIST_INSERT_HEAD(&phead, pnode, entry);
} }

9
sort.c
View File

@ -131,9 +131,7 @@ addkeydef(char *def, int flags)
{ {
struct kdlist *node; struct kdlist *node;
node = malloc(sizeof(*node)); node = enmalloc(2, sizeof(*node));
if (!node)
enprintf(2, "malloc:");
if (!head) if (!head)
head = node; head = node;
if (parse_keydef(&node->keydef, def, flags)) if (parse_keydef(&node->keydef, def, flags))
@ -282,7 +280,6 @@ static char *
columns(char *line, const struct keydef *kd) columns(char *line, const struct keydef *kd)
{ {
char *start, *end; char *start, *end;
char *res;
int i; int i;
for (i = 1, start = line; i < kd->start_column; i++) for (i = 1, start = line; i < kd->start_column; i++)
@ -305,7 +302,5 @@ columns(char *line, const struct keydef *kd)
end = strchr(line, '\0'); end = strchr(line, '\0');
} }
if (!(res = strndup(start, end - start))) return enstrndup(2, start, end - start);
enprintf(2, "strndup:");
return res;
} }