Trivial cleanups around *alloc functions

- Replace malloc+memset with calloc
- Remove unnecessary casts
- Some cosmetics
This commit is contained in:
Kim Woelders 2022-01-18 14:01:20 +01:00
parent acefe58f1b
commit 23cb0977a4
12 changed files with 50 additions and 64 deletions

View File

@ -2132,9 +2132,9 @@ Epplet_textbox_insert(Epplet_gadget eg, const char *new_contents)
g = (GadTextBox *) eg;
if (g->contents)
s = (char *)malloc(len + strlen(g->contents) + 1);
s = malloc(len + strlen(g->contents) + 1);
else
s = (char *)malloc(len + 1);
s = malloc(len + 1);
if ((line_break = strchr(new_contents, '\n')))
{
@ -2206,7 +2206,7 @@ Epplet_change_textbox(Epplet_gadget eg, const char *new_contents)
{
*s = '\0'; /* kill new line */
s = (char *)malloc(sizeof(char) * len + 1);
s = malloc(sizeof(char) * len + 1);
if (s)
{
@ -2498,7 +2498,7 @@ Epplet_textbox_handle_keyevent(XEvent * ev, Epplet_gadget gadget)
(g->contents + g->cursor_pos + 1),
strlen(g->contents + g->cursor_pos + 1));
g->contents = (char *)realloc(g->contents, strlen(g->contents));
g->contents = realloc(g->contents, strlen(g->contents));
*(g->contents + contents_len) = '\0';
}
return;
@ -2548,7 +2548,7 @@ Epplet_textbox_handle_keyevent(XEvent * ev, Epplet_gadget gadget)
{
char *s;
s = (char *)malloc(strlen(g->contents) + 1);
s = malloc(strlen(g->contents) + 1);
*(g->contents + g->cursor_pos - 1) = '\0';
sprintf(s, "%s%s", g->contents, (g->contents + g->cursor_pos));
@ -2567,14 +2567,14 @@ Epplet_textbox_handle_keyevent(XEvent * ev, Epplet_gadget gadget)
if (g->contents)
{
g->contents =
(char *)realloc(g->contents, (strlen(g->contents) + len + 1));
realloc(g->contents, (strlen(g->contents) + len + 1));
}
else
{
if (!strcmp(kbuf, " "))
return;
g->contents = (char *)malloc(len + 1);
g->contents = malloc(len + 1);
*(g->contents) = '\0';
}
@ -5577,16 +5577,14 @@ Epplet_load_config_file(const char *file)
FILE *f = NULL;
if (config_dict)
{
Epplet_clear_config();
}
Epplet_clear_config();
config_dict = (ConfigDict *) malloc(sizeof(ConfigDict));
memset(config_dict, 0, sizeof(ConfigDict));
config_dict = calloc(1, sizeof(ConfigDict));
config_dict->entries = malloc(sizeof(ConfigItem));
if (!(f = fopen(file, "r")))
return;
for (; fgets(s, sizeof(s), f);)
{
s2[0] = s3[0] = '\0';
@ -5597,6 +5595,7 @@ Epplet_load_config_file(const char *file)
}
Epplet_add_config(s2, s3);
}
fclose(f);
}
@ -5668,10 +5667,10 @@ Epplet_add_config(const char *key, const char *value)
{
if (!key)
return;
if (!config_dict)
{
config_dict = (ConfigDict *) malloc(sizeof(ConfigDict));
memset(config_dict, 0, sizeof(ConfigDict));
config_dict = calloc(1, sizeof(ConfigDict));
config_dict->entries = malloc(sizeof(ConfigItem));
}
else
@ -5680,6 +5679,7 @@ Epplet_add_config(const char *key, const char *value)
realloc(config_dict->entries,
sizeof(ConfigItem) * (config_dict->num_entries + 1));
}
config_dict->entries[config_dict->num_entries].key = strdup(key);
config_dict->entries[config_dict->num_entries].value =
(value ? strdup(value) : strdup(""));
@ -5850,7 +5850,7 @@ Epplet_query_multi_config(const char *shortkey, int *num)
break;
}
/* and build result */
result = (char **)malloc(sizeof(char *) * (*num));
result = malloc(sizeof(char *) * (*num));
if (result)
{

View File

@ -441,7 +441,7 @@ main(int argc, char **argv)
return -1;
}
}
s = (char *)malloc(sizeof(MAIL_PATH "/") + strlen(username) + 1);
s = malloc(sizeof(MAIL_PATH "/") + strlen(username) + 1);
/* Whoever changed the next line to snprintf(), DON'T. The size has already been calculated.
* Besides, the sizeof() operator returns sizeof(char *) here, not the array size. -- mej */
sprintf(s, MAIL_PATH "/%s", username);

View File

@ -254,15 +254,15 @@ parse_answers(const char *path)
}
if (cnt)
{
answers = (char **)realloc(answers, sizeof(char *) * (++cnt));
answers = realloc(answers, sizeof(char *) * (++cnt));
}
else
{
cnt = 1;
answers = (char **)malloc(sizeof(char *));
answers = malloc(sizeof(char *));
}
len = strlen(buff) + 1;
answers[idx] = (char *)malloc(len);
answers[idx] = malloc(len);
strcpy(answers[idx], buff);
for (; !(done = !(fgets(buff, sizeof(buff), fp)));)
@ -274,7 +274,7 @@ parse_answers(const char *path)
break;
}
len += strlen(buff) + 1;
answers[idx] = (char *)realloc(answers[idx], len);
answers[idx] = realloc(answers[idx], len);
strcat(answers[idx], "\n");
strcat(answers[idx], buff);
}

View File

@ -797,20 +797,18 @@ AddMountPoint(char *device, char *path)
if (!tiles)
{
tiles = (Tile *) malloc(sizeof(Tile));
tiles = calloc(1, sizeof(Tile));
if (tiles)
{
memset(tiles, 0, sizeof(Tile));
num_tiles = 1;
current_tile = tail_tile = tiles;
}
}
else
{
newtile = (Tile *) malloc(sizeof(Tile));
newtile = calloc(1, sizeof(Tile));
if (newtile)
{
memset(newtile, 0, sizeof(Tile));
newtile->next = NULL;
newtile->prev = tail_tile;
tail_tile->next = newtile;
@ -822,10 +820,9 @@ AddMountPoint(char *device, char *path)
if (tail_tile)
{
tail_tile->mountpoint = (MountPoint *) malloc(sizeof(MountPoint));
tail_tile->mountpoint = calloc(1, sizeof(MountPoint));
if (tail_tile->mountpoint)
{
memset(tail_tile->mountpoint, 0, sizeof(MountPoint));
if (device)
tail_tile->mountpoint->device = strdup(device);
if (path)
@ -906,19 +903,17 @@ AddMountPointType(char *key, char *image)
if (!types)
{
types = (MountPointType *) malloc(sizeof(MountPointType));
types = calloc(1, sizeof(MountPointType));
if (types)
{
memset(types, 0, sizeof(MountPointType));
num_types = 1;
}
}
else
{
newtype = (MountPointType *) malloc(sizeof(MountPointType));
newtype = calloc(1, sizeof(MountPointType));
if (newtype)
{
memset(newtype, 0, sizeof(MountPointType));
newtype->next = types;
types->prev = newtype;
types = newtype;
@ -1733,7 +1728,7 @@ SyncConfigs(void)
snprintf(s, sizeof(s), "%i", mode.polling_interval);
Epplet_modify_config("POLLINTVAL", s);
strings = (char **)malloc(sizeof(char *) * num_types);
strings = malloc(sizeof(char *) * num_types);
if (strings)
{
for (mpt = types, i = 0; mpt; mpt = mpt->next, i++)

View File

@ -464,13 +464,13 @@ main(int argc, char **argv)
b_help = Epplet_create_button(NULL, NULL,
82, 2, 0, 0, "HELP", win, NULL, cb_help, NULL);
if (!(result = (char *)malloc(BUF_LEN)))
if (!(result = malloc(BUF_LEN)))
{
perror("main: malloc result");
return (-1);
}
*result = '\0';
if (!(new_result = (char *)malloc(BUF_LEN)))
if (!(new_result = malloc(BUF_LEN)))
{
perror("main: malloc new_result");
return (-1);

View File

@ -407,7 +407,7 @@ update_cddb_tracks(void)
free(tracks);
tracks = (int *)calloc((size_t)DiscInfo.disc_total_tracks, sizeof(int));
tracks = calloc((size_t)DiscInfo.disc_total_tracks, sizeof(int));
/* Causes segfault...
* if(tracks_popup != NULL)
@ -437,7 +437,7 @@ fake_cddb_tracks(void)
free(tracks);
tracks = (int *)calloc((size_t)DiscInfo.disc_total_tracks, sizeof(int));
tracks = calloc((size_t)DiscInfo.disc_total_tracks, sizeof(int));
tracks_popup = Epplet_create_popup();

View File

@ -95,7 +95,7 @@ dirscan(char *dir, unsigned long *num)
*num = 0;
return ((char **)NULL);
}
names = (char **)malloc(dirlen * sizeof(char *));
names = malloc(dirlen * sizeof(char *));
D((" -> Storing names at %8p.\n", names));
if (!names)
@ -144,7 +144,7 @@ dirscan(char *dir, unsigned long *num)
}
closedir(dirp);
*num = dirlen;
names = (char **)realloc(names, dirlen * sizeof(char *));
names = realloc(names, dirlen * sizeof(char *));
D((" -> Final directory length is %lu. List moved to %8p\n", *num, names));
if (randomize)

View File

@ -333,9 +333,7 @@ config_cb(void *data __UNUSED__)
if (config_win)
return;
cfg_gads =
(tool_config_t *) malloc(sizeof(tool_config_t) * (button_cnt + 3));
memset(cfg_gads, 0, sizeof(tool_config_t) * (button_cnt + 3));
cfg_gads = calloc(button_cnt + 3, sizeof(tool_config_t));
ch = 40 + ((button_cnt + 3) * 30);
config_win =
@ -527,16 +525,12 @@ parse_config(void)
{
if (button_cnt)
{
buttons =
(toolbutton_t *) realloc(buttons,
sizeof(toolbutton_t) * (button_cnt +
1));
buttons = realloc(buttons,
sizeof(toolbutton_t) * (button_cnt + 1));
}
else
{
buttons =
(toolbutton_t *) malloc(sizeof(toolbutton_t) *
(button_cnt + 1));
buttons = malloc(sizeof(toolbutton_t) * (button_cnt + 1));
}
memset(&buttons[button_cnt], 0, sizeof(toolbutton_t));

View File

@ -227,8 +227,7 @@ main(int argc, char *argv[])
Epplet_adjust_priority(10);
cpu_hist_size = WIDTH;
cpu_hist = (int *)malloc(sizeof(int) * cpu_hist_size);
memset(cpu_hist, 0x00, sizeof(int) * cpu_hist_size);
cpu_hist = calloc(cpu_hist_size, sizeof(int));
hist_pos = 0;
Epplet_Init("E-Wireless", "0.1", "E 802.11 signal monitoring epplet",

View File

@ -198,7 +198,7 @@ apply_config(void)
{
idx = 0;
}
hacks = (char **)realloc(hacks, sizeof(char *) * (hack_cnt));
hacks = realloc(hacks, sizeof(char *) * (hack_cnt));
DEC_HACK();
change_hack(NULL);
}
@ -236,10 +236,9 @@ config_cb(void *data __UNUSED__)
if (config_win)
return;
hacks = (char **)realloc(hacks, sizeof(char *) * (++hack_cnt));
hacks = realloc(hacks, sizeof(char *) * (++hack_cnt));
cfg_tb_hacks = (Epplet_gadget *) malloc(sizeof(Epplet_gadget) * hack_cnt);
memset(cfg_tb_hacks, 0, sizeof(Epplet_gadget) * hack_cnt);
cfg_tb_hacks = calloc(hack_cnt, sizeof(Epplet_gadget));
h = 70 + (hack_cnt * 41);
config_win =
@ -281,7 +280,7 @@ static void
parse_config(void)
{
delay = atof(Epplet_query_config_def("delay", "60.0"));
hacks = (char **)malloc(sizeof(char *) * (hack_cnt + 1));
hacks = malloc(sizeof(char *) * (hack_cnt + 1));
for (; 1; hack_cnt++)
{
@ -294,13 +293,13 @@ parse_config(void)
{
break;
}
hacks = (char **)realloc(hacks, sizeof(char *) * (hack_cnt + 1));
hacks = realloc(hacks, sizeof(char *) * (hack_cnt + 1));
hacks[hack_cnt] = (char *)tmp;
}
if (hack_cnt == 0)
{
hack_cnt = 3;
hacks = (char **)realloc(hacks, sizeof(char *) * (hack_cnt));
hacks = realloc(hacks, sizeof(char *) * (hack_cnt));
hacks[0] = (char *)"kaleidescope";
hacks[1] = (char *)"rorschach";
hacks[2] = (char *)"qix -solid -delay 0 -segments 100";

View File

@ -107,11 +107,11 @@ safe_realloc(void **p, size_t siz)
return;
}
if (*p)
r = (void *)realloc(*p, siz);
r = realloc(*p, siz);
else
{
/* realloc(NULL, nbytes) doesn't seem to work under SunOS 4.1.x */
r = (void *)malloc(siz);
r = malloc(siz);
}
*p = r;
}
@ -349,7 +349,7 @@ parse_mime_header(FILE * fp)
int status = FALSE;
if (!buffer)
buffer = (char *)malloc(buflen);
buffer = malloc(buflen);
while (*(buffer = read_rfc822_line(fp, buffer, &buflen)) != 0)
{
@ -548,13 +548,13 @@ maildir_folder_count(char *path, int force)
last_update = s.st_mtime;
curdir = (char *)malloc(strlen(path) + 5);
curdir = malloc(strlen(path) + 5);
if (!curdir)
{
D((" -> Unable to allocate memory.\n"));
return 0;
}
newdir = (char *)malloc(strlen(path) + 5);
newdir = malloc(strlen(path) + 5);
if (!newdir)
{
D((" -> Unable to allocate memory.\n"));

View File

@ -44,8 +44,7 @@ net_get_devices(unsigned long *count)
char buff[256], **names = NULL, *s;
unsigned long i;
names = (char **)malloc(sizeof(char *));
memset(names, 0, sizeof(char *));
names = calloc(1, sizeof(char *));
#ifdef linux
fp = fopen("/proc/net/dev", "r");
@ -66,7 +65,7 @@ net_get_devices(unsigned long *count)
*s = 0;
for (s = buff; isspace(*s); s++);
names[i] = strdup(s);
names = (char **)realloc(names, sizeof(char *) * ((++i) + 1));
names = realloc(names, sizeof(char *) * ((++i) + 1));
names[i] = (char *)NULL;
}
if (count)