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

View File

@ -441,7 +441,7 @@ main(int argc, char **argv)
return -1; 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. /* 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 */ * Besides, the sizeof() operator returns sizeof(char *) here, not the array size. -- mej */
sprintf(s, MAIL_PATH "/%s", username); sprintf(s, MAIL_PATH "/%s", username);

View File

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

View File

@ -797,20 +797,18 @@ AddMountPoint(char *device, char *path)
if (!tiles) if (!tiles)
{ {
tiles = (Tile *) malloc(sizeof(Tile)); tiles = calloc(1, sizeof(Tile));
if (tiles) if (tiles)
{ {
memset(tiles, 0, sizeof(Tile));
num_tiles = 1; num_tiles = 1;
current_tile = tail_tile = tiles; current_tile = tail_tile = tiles;
} }
} }
else else
{ {
newtile = (Tile *) malloc(sizeof(Tile)); newtile = calloc(1, sizeof(Tile));
if (newtile) if (newtile)
{ {
memset(newtile, 0, sizeof(Tile));
newtile->next = NULL; newtile->next = NULL;
newtile->prev = tail_tile; newtile->prev = tail_tile;
tail_tile->next = newtile; tail_tile->next = newtile;
@ -822,10 +820,9 @@ AddMountPoint(char *device, char *path)
if (tail_tile) if (tail_tile)
{ {
tail_tile->mountpoint = (MountPoint *) malloc(sizeof(MountPoint)); tail_tile->mountpoint = calloc(1, sizeof(MountPoint));
if (tail_tile->mountpoint) if (tail_tile->mountpoint)
{ {
memset(tail_tile->mountpoint, 0, sizeof(MountPoint));
if (device) if (device)
tail_tile->mountpoint->device = strdup(device); tail_tile->mountpoint->device = strdup(device);
if (path) if (path)
@ -906,19 +903,17 @@ AddMountPointType(char *key, char *image)
if (!types) if (!types)
{ {
types = (MountPointType *) malloc(sizeof(MountPointType)); types = calloc(1, sizeof(MountPointType));
if (types) if (types)
{ {
memset(types, 0, sizeof(MountPointType));
num_types = 1; num_types = 1;
} }
} }
else else
{ {
newtype = (MountPointType *) malloc(sizeof(MountPointType)); newtype = calloc(1, sizeof(MountPointType));
if (newtype) if (newtype)
{ {
memset(newtype, 0, sizeof(MountPointType));
newtype->next = types; newtype->next = types;
types->prev = newtype; types->prev = newtype;
types = newtype; types = newtype;
@ -1733,7 +1728,7 @@ SyncConfigs(void)
snprintf(s, sizeof(s), "%i", mode.polling_interval); snprintf(s, sizeof(s), "%i", mode.polling_interval);
Epplet_modify_config("POLLINTVAL", s); Epplet_modify_config("POLLINTVAL", s);
strings = (char **)malloc(sizeof(char *) * num_types); strings = malloc(sizeof(char *) * num_types);
if (strings) if (strings)
{ {
for (mpt = types, i = 0; mpt; mpt = mpt->next, i++) 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, b_help = Epplet_create_button(NULL, NULL,
82, 2, 0, 0, "HELP", win, NULL, cb_help, 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"); perror("main: malloc result");
return (-1); return (-1);
} }
*result = '\0'; *result = '\0';
if (!(new_result = (char *)malloc(BUF_LEN))) if (!(new_result = malloc(BUF_LEN)))
{ {
perror("main: malloc new_result"); perror("main: malloc new_result");
return (-1); return (-1);

View File

@ -407,7 +407,7 @@ update_cddb_tracks(void)
free(tracks); 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... /* Causes segfault...
* if(tracks_popup != NULL) * if(tracks_popup != NULL)
@ -437,7 +437,7 @@ fake_cddb_tracks(void)
free(tracks); 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(); tracks_popup = Epplet_create_popup();

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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