Commit c2154611 authored by Peter Pentchev's avatar Peter Pentchev

Abort on memory allocation failure.

parent 9deb25b1
......@@ -2,6 +2,8 @@ Change log for hexer, the multi-view binary editor
0.2.1 not yet ;)
- fix some more typographical errors
- abort on memory allocation failures (malloc(), realloc(),
strdup())
0.2.0 2015/12/09
- fix a lot of C compiler warnings
......
To-do list for hexer, the simple binary editor:
- check for malloc()/realloc()/strdup() result in almost all of the code!
- check for errors from the hexer internal routines, too
......@@ -128,14 +128,11 @@ new_buffer_block(const unsigned long blocksize, char * const data)
{
BufferBlock *new_block;
if (!(new_block = (BufferBlock *)malloc(sizeof(BufferBlock)))) return 0;
new_block = (BufferBlock *)malloc_fatal(sizeof(BufferBlock));
new_block->next_block = 0;
if (data != 0) new_block->data = data;
else if (!(new_block->data = (char *)malloc(blocksize))) {
free((char *)new_block);
return 0;
}
else new_block->data = (char *)malloc_fatal(blocksize);
return new_block;
}
/* new_buffer_block */
......@@ -159,7 +156,7 @@ new_buffer(struct BufferOptions *arg_options)
struct BufferOptions options;
options = arg_options ? *arg_options : b_default_options;
if (!(new_buf = (Buffer *)malloc(sizeof(Buffer)))) return 0;
new_buf = (Buffer *)malloc_fatal(sizeof(Buffer));
new_buf->first_block = 0;
new_buf->size = 0;
new_buf->blocksize = options.blocksize;
......@@ -470,11 +467,7 @@ b_read_buffer_from_file(Buffer * const buffer, const char * const filename)
do {
ssize_t bytes_read;
tmp = (char *)malloc(buffer->blocksize);
if (tmp == 0) {
close(file);
return -1;
}
tmp = (char *)malloc_fatal(buffer->blocksize);
bytes_read = read(file, tmp, buffer->blocksize);
if (bytes_read > 0) {
if (buffer->first_block == 0) {
......@@ -533,12 +526,12 @@ b_write_buffer_to_file(Buffer *buffer, char *filename)
long
b_copy_to_file(Buffer *buffer, const char *filename, unsigned long position, unsigned long count)
{
char *tmp = malloc(buffer->blocksize);
char *tmp = malloc_fatal(buffer->blocksize);
long bytes_read = 0, bytes_wrote = 0;
int file = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (file < 0 || !tmp) {
if (tmp) free((char *)tmp);
if (file < 0) {
free((char *)tmp);
if (file >= 0) close(file);
return -1;
}
......@@ -565,13 +558,13 @@ b_copy_to_file(Buffer *buffer, const char *filename, unsigned long position, uns
long
b_paste_from_file(Buffer *buffer, const char *filename, unsigned long position)
{
char *tmp = malloc(buffer->blocksize);
char *tmp = malloc_fatal(buffer->blocksize);
long bytes_read, bytes_wrote = 0;
int file = open(filename, O_RDONLY);
assert(!buffer->read_only);
if (file < 0 || !tmp) {
if (tmp) free((char *)tmp);
if (file < 0) {
free((char *)tmp);
if (file >= 0) close(file);
return -1;
}
......
......@@ -66,6 +66,7 @@
#include "calc.h"
#include "readline.h"
#include "tio.h"
#include "util.h"
#define CALC_STACKSIZE 1024
......@@ -305,7 +306,7 @@ calc_error(struct calc_object_s *x, const char *fmt, ...)
vsprintf(buf, fmt, ap);
if (x->type != CO_ERROR) {
x->type = CO_ERROR;
x->u.s = strdup(buf);
x->u.s = strdup_fatal(buf);
}
va_end(ap);
return 0;
......@@ -341,9 +342,9 @@ calc_assign(struct calc_object_s x, struct calc_object_s y)
i->object = y;
return 0;
}
i = (struct calc_variable_s *)malloc(sizeof *i);
i = (struct calc_variable_s *)malloc_fatal(sizeof *i);
i->object = y;
i->name = strdup(x.u.s);
i->name = strdup_fatal(x.u.s);
i->next = calc_variable_list;
calc_variable_list = i;
return 0;
......@@ -1042,7 +1043,7 @@ dec: base = 10, r = p;
} else if (isalpha(*p) || *p == '_' || *p == '$') {
for (q = p + 1; isalnum(*q) || *q == '_' || *q == '$'; ++q);
x.type = CO_VARIABLE;
x.u.s = strdup(p);
x.u.s = strdup_fatal(p);
x.u.s[q - p] = 0;
p = q;
} else {
......
......@@ -432,7 +432,7 @@ substitute:
end -= match_len - replace_len;
/* create an entry for the undo-list */
if (match_len) {
data = (char *)malloc(match_len);
data = (char *)malloc_fatal(match_len);
if (b_read(hedit->buffer, data, position, match_len) < 0) {
he_message(1, "could not read data from the internal buffer");
free((char *)data);
......@@ -628,7 +628,7 @@ exhcmd_read(struct he_s *hedit, const char *file, long position, long end __unus
l = b_paste_from_file(hedit->buffer, file, position);
if (l == -1)
return skip;
data = (char *)malloc(l);
data = (char *)malloc_fatal(l);
fp = fopen(file, "r");
if (fread(data, 1, l, fp) != (unsigned long)l) {
free(data);
......@@ -676,7 +676,7 @@ exhcmd_edit(struct he_s *hedit __unused, const char *name, long begin __unused,
if (he_select_buffer(name) < 0) {
new_buffer_f = 1;
if (*name == '~') {
user = strdup(name + 1);
user = strdup_fatal(name + 1);
for (p = user; *p && *p != '/'; ++p);
*p = 0;
if (*user) {
......@@ -686,7 +686,7 @@ exhcmd_edit(struct he_s *hedit __unused, const char *name, long begin __unused,
for (setpwent(); (pe = getpwent());)
if (uid == pe->pw_uid) break;
if (pe) {
pathbuf = (char *)malloc(strlen(name) + strlen(pe->pw_dir) + 4);
pathbuf = (char *)malloc_fatal(strlen(name) + strlen(pe->pw_dir) + 4);
strcpy(pathbuf, pe->pw_dir);
strcat(pathbuf, name + strlen(user) + 1);
path = pathbuf;
......@@ -732,9 +732,9 @@ exhcmd_buffer(struct he_s *hedit, const char *name, long begin __unused, long en
for (i = buffer_list, k = 0; i; i = i->next, ++k)
if (strlen(i->hedit->buffer_name) > name_maxlen)
name_maxlen = strlen(i->hedit->buffer_name);
list = (char **)malloc((k + 1) * sizeof(char *));
list = (char **)malloc_fatal((k + 1) * sizeof(char *));
for (i = buffer_list, k = 0; i; i = i->next, ++k) {
list[k] = (char *)malloc(strlen(i->hedit->buffer_name) + 512);
list[k] = (char *)malloc_fatal(strlen(i->hedit->buffer_name) + 512);
sprintf(list[k], " `%s' ", i->hedit->buffer_name);
c = list[k] + strlen(list[k]);
for (j = name_maxlen - strlen(i->hedit->buffer_name); j >= 0; --j)
......@@ -1018,7 +1018,7 @@ exhcmd_delete(struct he_s *hedit, const char *args, long begin, long end)
util_trunc(p);
args = p;
if (!count) return skip;
data = (char *)malloc(count);
data = (char *)malloc_fatal(count);
b_read(hedit->buffer, data, begin, count);
if (!kill_buffer)
kill_buffer = new_buffer(0);
......
......@@ -61,6 +61,7 @@
#include "edit.h"
#include "readline.h"
#include "tio.h"
#include "util.h"
int he_hex_column[] = { 12, 15, 18, 21, 24, 27, 30, 33,
37, 40, 43, 46, 49, 52, 55, 58 };
......@@ -372,14 +373,14 @@ he_subcommand(struct he_s *hedit, int type, unsigned long position, unsigned lon
command = 0;
} else { /* (type >= 0) */
if (!command) {
command = (struct he_command_s *)malloc(sizeof(*command));
command = (struct he_command_s *)malloc_fatal(sizeof(*command));
again = 1;
last_position = position;
i = command;
} else {
if (again) if (position != last_position) again = 0;
for (i = command; i->next_subcommand; i = i->next_subcommand);
i->next_subcommand = (struct he_command_s *)malloc(sizeof(*command));
i->next_subcommand = (struct he_command_s *)malloc_fatal(sizeof(*command));
i = i->next_subcommand;
}
i->next_subcommand = 0;
......@@ -484,7 +485,7 @@ he_read_command(struct he_s *hedit)
}
if (fgetc(hedit->undo.swapfile) == EOF)
return 0;
c = command = (struct he_command_s *)malloc(sizeof(struct he_command_s));
c = command = (struct he_command_s *)malloc_fatal(sizeof(struct he_command_s));
for (i = 1; c; ++i) {
int n;
c->next_command = 0;
......@@ -499,12 +500,12 @@ he_read_command(struct he_s *hedit)
if (fread(bigendian, 4, 1, hedit->undo.swapfile) != 1)
return 0;
c->count = he_bigendian_to_ulong(bigendian);
c->data = (char *)malloc(c->count);
c->data = (char *)malloc_fatal(c->count);
if (fread(c->data, 1, c->count, hedit->undo.swapfile) != c->count)
return 0;
if (i < n_subcommands) {
c->next_subcommand =
(struct he_command_s *)malloc(sizeof(struct he_command_s));
(struct he_command_s *)malloc_fatal(sizeof(struct he_command_s));
c = c->next_subcommand;
} else {
c->next_subcommand = 0;
......@@ -639,13 +640,13 @@ he_again(struct he_s *hedit, long position)
* be necessary to adjust `j->count' in `command2'.
*/
struct he_command_s *command2, *j;
command2 = (struct he_command_s *)malloc(sizeof(struct he_command_s));
command2 = (struct he_command_s *)malloc_fatal(sizeof(struct he_command_s));
for (i = command, j = command2; i;) {
*j = *i;
j->next_command = 0;
j->prev_command = 0;
if (i->count) {
j->data = (char *)malloc(i->count);
j->data = (char *)malloc_fatal(i->count);
if (i->type) /* insert */
memcpy(j->data, i->data, i->count);
else { /* delete */
......@@ -654,14 +655,14 @@ he_again(struct he_s *hedit, long position)
assert(0); /* FIXME: better handling */
} else if ((unsigned long)x < j->count) {
/* `x > j->count' is impossible here. */
j->data = (char *)realloc(j->data, x);
j->data = (char *)realloc_fatal(j->data, x);
j->count = x;
}
}
}
if (i->next_subcommand) {
j->next_subcommand =
(struct he_command_s *)malloc(sizeof(struct he_command_s));
(struct he_command_s *)malloc_fatal(sizeof(struct he_command_s));
j = j->next_subcommand;
i = i->next_subcommand;
} else
......@@ -699,7 +700,7 @@ he_delete(struct he_s *hedit, long count, int dont_save)
count = hedit->buffer->size - start;
}
if (!count) return;
data = (char *)malloc(count);
data = (char *)malloc_fatal(count);
b_read(hedit->buffer, data, start, count);
if (!dont_save) {
if (!kill_buffer)
......@@ -731,7 +732,7 @@ he_paste(struct he_s *hedit, long count)
if (kill_buffer ? !(c = kill_buffer->size) : 1) return;
if (count < 1) count = 1;
while (count--) {
data = (char *)malloc(c);
data = (char *)malloc_fatal(c);
b_read(kill_buffer, data, 0, c);
b_insert(hedit->buffer, hedit->position, c);
b_write(hedit->buffer, data, hedit->position, c);
......@@ -755,8 +756,8 @@ he_paste_over(struct he_s *hedit, long count)
if (kill_buffer ? !(c = kill_buffer->size) : 1) return;
if (count < 1) count = 1;
data_insert = (char *)malloc(c * count);
data_delete = (char *)malloc(c * count);
data_insert = (char *)malloc_fatal(c * count);
data_delete = (char *)malloc_fatal(c * count);
b_read(kill_buffer, data_insert, 0, c);
for (i = 1; i < count; ++i) memcpy(data_insert + c * i, data_insert, c);
bytes_deleted = b_read(hedit->buffer, data_delete,
......@@ -764,7 +765,7 @@ he_paste_over(struct he_s *hedit, long count)
b_write_append(hedit->buffer, data_insert, hedit->position, c * count);
if (bytes_deleted < c * count)
data_delete =
(char *)realloc(data_delete, bytes_deleted + !bytes_deleted);
(char *)realloc_fatal(data_delete, bytes_deleted + !bytes_deleted);
he_subcommand(hedit, 0, hedit->position, bytes_deleted, data_delete);
he_subcommand(hedit, 1, hedit->position, c * count, data_insert);
he_subcommand(hedit, -1, 0, 0, 0);
......@@ -1529,11 +1530,11 @@ he_insert_mode(struct he_s *hedit, int replace_mode, long count)
/* Moving the cursor eliminates the counter.
*/
if (i != hedit->position && insert->size) {
data = (char *)malloc(insert->size);
data = (char *)malloc_fatal(insert->size);
b_read(insert, data, 0, insert->size);
if (replace_mode && replace->size) {
char *data_replace;
data_replace = (char *)malloc(replace->size);
data_replace = (char *)malloc_fatal(replace->size);
b_read(replace, data_replace, 0, replace->size);
he_subcommand(hedit, 0, position, replace->size, data_replace);
b_clear(replace);
......@@ -1707,7 +1708,7 @@ hx_exit_insert_mode:
if (replace_mode) {
b_insert(hedit->buffer, position, replace->size);
b_copy(hedit->buffer, replace, position, 0, replace->size);
data_replace = (char *)malloc(replace->size * count);
data_replace = (char *)malloc_fatal(replace->size * count);
replace_size2 =
b_read(hedit->buffer, data_replace, position, replace->size * count);
if (replace_size2 < 0) {
......@@ -1715,13 +1716,13 @@ hx_exit_insert_mode:
} else if ((unsigned long)replace_size2 < replace->size * count)
/* We don't want to eat up more memory than needed.
*/
data_replace = (char *)realloc(data_replace, (unsigned long)replace_size2);
data_replace = (char *)realloc_fatal(data_replace, (unsigned long)replace_size2);
b_delete(hedit->buffer, position, replace_size2);
he_subcommand(hedit, 0, position, replace_size2, data_replace);
}
data = (char *)malloc(insert->size);
data = (char *)malloc_fatal(insert->size);
b_read(insert, data, 0, insert->size);
data = (char *)realloc(data, insert->size * count);
data = (char *)realloc_fatal(data, insert->size * count);
for (c = insert->size * (count - 1); c > 0; c -= insert->size)
memcpy(data + c, data, insert->size);
b_insert(hedit->buffer, position, insert->size * count);
......@@ -1734,11 +1735,11 @@ hx_exit_insert_mode:
} else
he_refresh_part(hedit, hedit->position, hedit->buffer->size - 1);
} else {
data = (char *)malloc(insert->size);
data = (char *)malloc_fatal(insert->size);
b_read(insert, data, 0, insert->size);
if (replace_mode)
if (replace->size) {
data_replace = (char *)malloc(replace->size);
data_replace = (char *)malloc_fatal(replace->size);
b_read(replace, data_replace, 0, replace->size);
he_subcommand(hedit, 0, position, replace->size, data_replace);
}
......
......@@ -111,10 +111,10 @@ exh_shell_command(const char * const command, int pager_f)
tio_suspend();
tio_clear();
tio_flush();
if (!(*shell = getenv("SHELL"))) *shell = strdup(EXH_DEFAULT_SHELL);
*shell = strdup(*shell);
if (!(*pager = getenv("PAGER"))) *pager = strdup(HE_DEFAULT_PAGER);
*pager = strdup(*pager);
if (!(*shell = getenv("SHELL"))) *shell = strdup_fatal(EXH_DEFAULT_SHELL);
*shell = strdup_fatal(*shell);
if (!(*pager = getenv("PAGER"))) *pager = strdup_fatal(HE_DEFAULT_PAGER);
*pager = strdup_fatal(*pager);
/* break `*shell' and `*pager' down into whitespace separated
* substrings. it is *not* possible to mask whitespace characters in any
* way. */
......@@ -265,8 +265,8 @@ exh_subshell(void)
tio_suspend();
tio_clear();
tio_flush();
if (!(*shell = getenv("SHELL"))) *shell = strdup(EXH_DEFAULT_SHELL);
*shell = strdup(*shell);
if (!(*shell = getenv("SHELL"))) *shell = strdup_fatal(EXH_DEFAULT_SHELL);
*shell = strdup_fatal(*shell);
/* break `*shell' down into whitespace separated substrings.
* it is *not* possible to mask whitespace characters in any way. */
for (i = 0; *shell[i]; shell[++i] = p) {
......@@ -678,7 +678,7 @@ exh_cpl_file_list(const char * const prefix)
/* check if the `prefix' starts with a "~user/"-abbrev..
*/
if (*prefix == '~') {
user = strdup(prefix + 1);
user = strdup_fatal(prefix + 1);
for (p = user; *p && *p != '/'; ++p);
if (!*p) {
/* hmm... the username `user' is possibly incomplete. check how many
......@@ -690,13 +690,13 @@ exh_cpl_file_list(const char * const prefix)
setpwent();
/* make the list.
*/
*(list = (char **)malloc(sizeof(char *) * ++i)) = 0;
*(list = (char **)malloc_fatal(sizeof(char *) * ++i)) = 0;
for (i = 0; (pe = getpwent());)
if (*user ? !strncmp(pe->pw_name, user, strlen(user)) : 1) {
*d_name = '~';
strcpy(d_name + 1, pe->pw_name);
strcat(d_name, "/");
list[i++] = strdup(d_name);
list[i++] = strdup_fatal(d_name);
}
list[i] = 0;
endpwent();
......@@ -736,20 +736,20 @@ exh_cpl_file_list(const char * const prefix)
strcat(d_name, de->d_name);
if (!strncmp(d_name, prefix, plen)) ++i;
}
list = (char **)malloc(sizeof(char *) * ++i);
list = (char **)malloc_fatal(sizeof(char *) * ++i);
for (rewinddir(dp), i = 0; (de = readdir(dp));) {
struct stat st;
char *rp;
strcpy(d_name, dirname);
strcat(d_name, de->d_name);
if (!strncmp(d_name, prefix, plen)) {
rp = (char *)malloc(strlen(path) + strlen(de->d_name) + 4);
rp = (char *)malloc_fatal(strlen(path) + strlen(de->d_name) + 4);
strcpy(rp, path);
strcat(rp, "/");
strcat(rp, de->d_name);
if (!stat(rp, &st)) if (S_ISDIR(st.st_mode)) strcat(d_name, "/");
free(rp);
list[i++] = strdup(d_name);
list[i++] = strdup_fatal(d_name);
}
}
list[i] = 0;
......@@ -759,7 +759,7 @@ exit:
return list;
empty_list:
if (user) free(user);
*(list = (char **)malloc(sizeof(char *))) = 0;
*(list = (char **)malloc_fatal(sizeof(char *))) = 0;
return list;
}
/* exh_cpl_file_list */
......@@ -777,18 +777,18 @@ exh_cpl_command_list(const char * const prefix)
&& exh_commands[i].cmd_char
&& (plen ? exh_commands[i].cmd_char == *prefix : 1)) ++j;
}
list = (char **)malloc((j + 1) * sizeof(char *));
list = (char **)malloc_fatal((j + 1) * sizeof(char *));
if (j) {
for (i = 0, j = 0; exh_commands[i].cmd_name; ++i) {
if (!strncmp(exh_commands[i].cmd_name, prefix, plen)) {
list[j] =
(char *)malloc(strlen(exh_commands[i].cmd_name) + 1);
(char *)malloc_fatal(strlen(exh_commands[i].cmd_name) + 1);
strcpy(list[j++], exh_commands[i].cmd_name);
}
if (exh_commands[i].cmd_name[0] != exh_commands[i].cmd_char
&& exh_commands[i].cmd_char
&& (plen ? exh_commands[i].cmd_char == *prefix : 1)) {
list[j] = (char *)malloc(2);
list[j] = (char *)malloc_fatal(2);
list[j][0] = exh_commands[i].cmd_char;
list[j++][1] = 0;
}
......@@ -810,11 +810,11 @@ exh_cpl_buffer_list(const char * const prefix)
for (j = 0, i = buffer_list; i; i = i->next)
if (!strncmp(i->hedit->buffer_name, prefix, plen)) ++j;
list = (char **)malloc((j + 1) * sizeof(char *));
list = (char **)malloc_fatal((j + 1) * sizeof(char *));
if (j) {
for (j = 0, i = buffer_list; i; i = i->next)
if (!strncmp(i->hedit->buffer_name, prefix, plen)) {
list[j] = (char *)malloc(strlen(i->hedit->buffer_name) + 1);
list[j] = (char *)malloc_fatal(strlen(i->hedit->buffer_name) + 1);
strcpy(list[j++], i->hedit->buffer_name);
}
list[j] = 0;
......@@ -840,7 +840,7 @@ exh_cpl_file_and_buffer_list(const char * const prefix)
for (i = 0; list1[i]; ++i);
for (j = k = 0; list2[j]; ++j) if (list2[j] != (char *)1) ++k;
list1 = (char **)realloc(list1, (i + k + 1) * sizeof(char *));
list1 = (char **)realloc_fatal(list1, (i + k + 1) * sizeof(char *));
for (j = k = i; list2[j - i]; ++j)
if (list2[j - i] != (char *)1) {
list1[k] = list2[j - i];
......@@ -863,7 +863,7 @@ exh_cpl_option_list(const char *prefix)
i = list = s_option_list(prefix, no_f);
if (no_f) {
while (*i) {
*i = (char *)realloc(*i, k = strlen(*i) + 2);
*i = (char *)realloc_fatal(*i, k = strlen(*i) + 2);
for (j = k; j >= 0; --j) (*i)[j] = (*i)[j - 2];
**i = 'n', (*i)[1] = 'o';
++i;
......@@ -930,7 +930,7 @@ exh_completer(const char * const prefix, const char * const command, char * cons
}
exit_exh_competer:
if (expect < 0) {
char **list = (char **)malloc(sizeof(char *));
char **list = (char **)malloc_fatal(sizeof(char *));
*list = 0;
return list;
} else
......
......@@ -89,11 +89,11 @@ he_message(const int beep, const char * const fmt, ...)
va_start(ap, fmt);
/* length = tio_nprintf(fmt, ap); */
m = (struct he_message_s *)malloc(sizeof(struct he_message_s));
m = (struct he_message_s *)malloc_fatal(sizeof(struct he_message_s));
m->next = he_messages;
m->beep = beep;
/* m->message = (char *)malloc(length + 1); */
m->message = (char *)malloc(512); /* FIXME */
/* m->message = (char *)malloc_fatal(length + 1); */
m->message = (char *)malloc_fatal(512); /* FIXME */
vsprintf(m->message, fmt, ap);
he_messages = m;
va_end(ap);
......@@ -206,13 +206,13 @@ he_open_buffer(const char * const name, const char * const path)
return -1;
}
}
*(buffer = (struct buffer_s *)malloc(sizeof(struct buffer_s))) = NO_BUFFER;
buffer->hedit = (struct he_s *)malloc(sizeof(struct he_s));
*(buffer = (struct buffer_s *)malloc_fatal(sizeof(struct buffer_s))) = NO_BUFFER;
buffer->hedit = (struct he_s *)malloc_fatal(sizeof(struct he_s));
memset(buffer->hedit, 0, sizeof (struct he_s));
buffer->hedit->begin_selection = -1;
buffer->hedit->end_selection = -1;
buffer->hedit->insert_position = -1;
buffer->hedit->buffer_name = strdup(name);
buffer->hedit->buffer_name = strdup_fatal(name);
if (path && !no_file_f) {
if (!(fp = fopen(path, "r"))) {
he_message(1, "`%s': @Ab%s@~", path, strerror(errno));
......@@ -228,13 +228,13 @@ he_open_buffer(const char * const name, const char * const path)
buffer->visited_f = 1;
}
if (path) {
buffer->path = strdup(path);
buffer->path = strdup_fatal(path);
if (!getcwd(cwd, PATH_MAX)) {
he_message(0, "@Abcan't get cwd: %s@~", strerror(errno));
buffer->fullpath = strdup(path);
buffer->fullpath = strdup_fatal(path);
} else {
buffer->fullpath =
(char *)malloc(strlen(path) + strlen(cwd) + 2);
(char *)malloc_fatal(strlen(path) + strlen(cwd) + 2);
sprintf(buffer->fullpath, "%s/%s", cwd, path);
}
}
......@@ -273,7 +273,7 @@ he_select_buffer_(const struct buffer_s * const buffer)
}
/* check out if we can open a swapfile */
i->hedit->swapfile =
(char *)malloc(strlen(hexer_ext) + strlen(i->path) + 1);
(char *)malloc_fatal(strlen(hexer_ext) + strlen(i->path) + 1);
strcpy(i->hedit->swapfile, i->path);
strcat(i->hedit->swapfile, hexer_ext);
if (access(i->hedit->swapfile, R_OK)) {
......@@ -291,7 +291,7 @@ he_select_buffer_(const struct buffer_s * const buffer)
he_message(1, "@Abwarning: swapfile@~ `%s' @Abexists@~",
i->hedit->swapfile);
i->hedit->swapfile =
(char *)realloc(i->hedit->swapfile,
(char *)realloc_fatal(i->hedit->swapfile,
strlen(i->hedit->swapfile) + strlen(swap_template) + 1);
strcat(i->hedit->swapfile, swap_template);
if ((swapfd = mkstemp(i->hedit->swapfile)) < 0)
......
......@@ -54,6 +54,7 @@
#include "buffer.h"
#include "hexer.h"
#include "util.h"
#undef TIO_MAP
#define TIO_MAP 1
......@@ -120,7 +121,7 @@ key_strrep(const int *ks)
key = *i;
n += strlen(tio_keyrep(key));
}
s = t = (char *)malloc(n + 1);
s = t = (char *)malloc_fatal(n + 1);
for (i = ks; *i; ++i) {
if (*i == MAP_ESC)
key = *++i + HXKEY_BIAS;
......@@ -141,7 +142,7 @@ key_strrep_simple(ks)
{
char *s, *t;
s = t = (char *)malloc(key_strlen(ks) + 1);
s = t = (char *)malloc_fatal(key_strlen(ks) + 1);
for (; *ks; ++ks) if (*ks < 0x100) *t++ = *ks; else *t++ = 0xff;
*t = 0;
return s;
......@@ -331,7 +332,7 @@ tio_map(int map, char *from, char *to, int special_f)
key_strcpy(i->to, kto);
return 0;
}
i = (struct map_s *)malloc(sizeof(struct map_s));
i = (struct map_s *)malloc_fatal(sizeof(struct map_s));
i->next = 0;
key_strcpy(i->from, kfrom);
key_strcpy(i->to, kto);
......@@ -375,12 +376,12 @@ tio_maplist(int map)
for (i = map_first[map], n = 0; i; i = i->next, ++n);
if (!n) {
*(list = (char **)malloc(sizeof(char *))) = 0;
*(list = (char **)malloc_fatal(sizeof(char *))) = 0;
return list;
}
from = (char **)alloca(n * sizeof(char *));
to = (char **)alloca(n * sizeof(char *));
list = (char **)malloc((n + 1) * sizeof(char *));
list = (char **)malloc_fatal((n + 1) * sizeof(char *));
for (i = map_first[map], k = 0; i; i = i->next, ++k) {
from[k] = key_strrep(i->from);
to[k] = key_strrep(i->to);
......@@ -390,7 +391,7 @@ tio_maplist(int map)
if (strlen(from[k]) > from_maxwidth) from_maxwidth = strlen(from[k]);
from_maxwidth += 2;
for (k = 0; k < n; ++k) {
list[k] = (char *)malloc(from_maxwidth + strlen(to[k]) + 2);
list[k] = (char *)malloc_fatal(from_maxwidth + strlen(to[k]) + 2);
strcpy(list[k], from[k]);
memset(list[k] + strlen(list[k]), ' ', from_maxwidth - strlen(list[k]));
strcpy(list[k] + from_maxwidth, to[k]);
......@@ -422,7 +423,7 @@ tio_mapentry(map, from)
if (!key_strcmp(i->from, kfrom)) {
length = strlen(sfrom = key_strrep(i->from))
+ strlen(sto = key_strrep(i->to)) + 3;
rep = (char *)malloc(length);
rep = (char *)malloc_fatal(length);
strcpy(rep, sfrom);
strcat(rep, " ");
strcat(rep, sto);
......
......@@ -155,11 +155,11 @@ rl_history_init(int max)
for (i = 0; i < RL_MAX_CONTEXTS; ++i) {
rl_history[i] =
(struct rl_line_s *)malloc(max * sizeof(struct rl_line_s));
(struct rl_line_s *)malloc_fatal(max * sizeof(struct rl_line_s));
for (j = 0; j < max; ++j) {
rl_history[i][j].line = (char *)malloc(1);
rl_history[i][j].line = (char *)malloc_fatal(1);
*rl_history[i][j].line = 0;
rl_history[i][j].vline = (char *)malloc(1);
rl_history[i][j].vline = (char *)malloc_fatal(1);
*rl_history[i][j].vline = 0;
}
rl_history_c[i] = 1;
......@@ -189,12 +189,12 @@ rl_history_add(struct rl_line_s line)
rl_history[rl_current_context][i] =
rl_history[rl_current_context][i + 1];
rl_history[rl_current_context][i].line =
(char *)malloc(strlen(line.line) + 1);
(char *)malloc_fatal(strlen(line.line) + 1);
} else {
i = rl_history_c[rl_current_context] - 1;
++rl_history_c[rl_current_context];
rl_history[rl_current_context][i].line =
(char *)realloc(rl_history[rl_current_context][i].line,
(char *)realloc_fatal(rl_history[rl_current_context][i].line,
strlen(line.line) + 1);
}
strcpy(rl_history[rl_current_context][i].line, line.line);
......@@ -247,7 +247,7 @@ rl_history_set(struct rl_line_s line)
*/
{
rl_history[rl_current_context][rl_index].line =
(char *)realloc(rl_history[rl_current_context][rl_index].line,
(char *)realloc_fatal(rl_history[rl_current_context][rl_index].line,
strlen(line.line) + 1);
strcpy(rl_history[rl_current_context][rl_index].line, line.line);
}
......@@ -483,7 +483,7 @@ rl_insert(int x)
size_t position = rl_get_position();
size_t vposition;
int append = (position == strlen(rl.line));
char *s = strdup(tio_keyrep(x));
char *s = strdup_fatal(tio_keyrep(x));
size_t sl = strlen(s);
size_t special_f = 0;
int redisplay_f = 0;
......@@ -931,7 +931,7 @@ rl_complete(int context, int again)
util_strsort(list);
k = (n - 1) / m + 1; /* number of lines */
/* rearrange the list sorted in hx_columns */
list2 = (char **)malloc(m * k * sizeof(char *));
list2 = (char **)malloc_fatal(m * k * sizeof(char *));
memset(list2, 0, m * k * sizeof(char *));
for (i = 0, j = 0; list[i]; ++i) {
list2[m * (i % k) + j] = list[i];
......
......@@ -65,6 +65,7 @@
#include "config.h"
#include "defs.h"
#include "util.h"
#undef EOF
......@@ -380,7 +381,7 @@ regex_ref(int slot)
position = rx_store_begin[slot];
count = rx_store_end[slot] - position;
if (!rx_store_match[slot] && count) {
rx_store_match[slot] = malloc(count);
rx_store_match[slot] = malloc_fatal(count);
rx_seek(position);
rx_read(rx_store_match[slot], count);
return rx_store_match[slot];
......@@ -654,7 +655,7 @@ regex_match_(long *regex, long position, int *parstack, int *parsp)
if (max < 0) max = rx_maxmatch;
if (max > min) {
long *stack, sp;
stack = (long *)malloc((max - min + 1) * sizeof(long));
stack = (long *)malloc_fatal((max - min + 1) * sizeof(long));
for (sp = 0; max < 0 ? 1 : i < max; ++i) {
if (!regex_match_(pp, p, local_parstack, &local_parsp)) break;
stack[sp++] = p;
......@@ -727,7 +728,7 @@ regex_match(long *regex, long position, char **replace_str, long *replace_len, l
if (regex_match_(regex + 1, position, 0, &zero)) {
*match_len = rx_match_skip - position;
if (!*pp) {
*replace_str = (char *)malloc(1);
*replace_str = (char *)malloc_fatal(1);
**replace_str = 0;
*replace_len = 0;
} else {
......@@ -759,7 +760,7 @@ regex_match(long *regex, long position, char **replace_str, long *replace_len, l
}
assert(!*pp