Skip to content

Commit 3480183

Browse files
committed
Fix (potential) memory leaks
1 parent b92b6a2 commit 3480183

2 files changed

Lines changed: 29 additions & 13 deletions

File tree

src/libkita.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1511,7 +1511,7 @@ kita_child_free(kita_child_s** child)
15111511
}
15121512

15131513
// send SIGKILL if child is still running
1514-
kita_child_kill(c);
1514+
//kita_child_kill(c);
15151515

15161516
// free the child's cmd string
15171517
free(c->cmd);

src/succade.c

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,23 @@ static volatile int handled; // last signal that has been handled
2121
*/
2222
static void free_thing(thing_s *thing)
2323
{
24-
free(thing->sid);
25-
free(thing->output);
24+
if (thing->sid)
25+
{
26+
free(thing->sid);
27+
}
28+
29+
if (thing->output)
30+
{
31+
free(thing->output);
32+
}
33+
2634
cfg_free(&thing->cfg);
2735

28-
char *arg = kita_child_get_arg(thing->child);
29-
free(arg);
36+
if (thing->child)
37+
{
38+
char *arg = kita_child_get_arg(thing->child);
39+
free(arg);
40+
}
3041
}
3142

3243
/*
@@ -329,7 +340,7 @@ static double time_to_wait(state_s *state, double now)
329340
* the buffer; hence there is no need for malloc. We could just hand in
330341
* a buffer and the buffer length (BUFFER_BLOCK_STR) instead.
331342
*/
332-
static char *blockstr(const thing_s *block)
343+
int blockstr(const thing_s *block, char *buf, size_t len)
333344
{
334345
// for convenience
335346
const cfg_s *bcfg = &block->cfg;
@@ -414,8 +425,9 @@ static char *blockstr(const thing_s *block)
414425
// but of course, replacing a couple bytes with lots of malloc
415426
// would not be great either, so... not sure about it yet.
416427

417-
char *str = malloc(BUFFER_BLOCK_STR);
418-
snprintf(str, BUFFER_BLOCK_STR,
428+
//char *str = malloc(BUFFER_BLOCK_STR);
429+
//snprintf(str, BUFFER_BLOCK_STR,
430+
int res = snprintf(buf, len,
419431
"%%{O%d}" // margin left
420432
"%s" // action start
421433
"%%{F%s B%s U%s %co %cu}" // format start
@@ -447,7 +459,8 @@ static char *blockstr(const thing_s *block)
447459
);
448460

449461
free(result);
450-
return str;
462+
//return str;
463+
return res;
451464
}
452465

453466
/*
@@ -484,6 +497,7 @@ static char *barstr(const state_s *state)
484497
char align[5];
485498
int last_align = -1;
486499

500+
char block_str[BUFFER_BLOCK_STR];
487501
const thing_s *block = NULL;
488502
for (size_t i = 0; i < num_blocks; ++i)
489503
{
@@ -497,8 +511,7 @@ static char *barstr(const state_s *state)
497511

498512
int block_align = cfg_get_int(&block->cfg, BLOCK_OPT_ALIGN);
499513

500-
char *block_str = blockstr(block);
501-
size_t block_str_len = strlen(block_str);
514+
int block_str_len = blockstr(block, block_str, BUFFER_BLOCK_STR);
502515
if (block_align != last_align)
503516
{
504517
last_align = block_align;
@@ -514,7 +527,6 @@ static char *barstr(const state_s *state)
514527
bar_str = realloc(bar_str, bar_str_len);
515528
}
516529
strcat(bar_str, block_str);
517-
free(block_str);
518530
}
519531
strcat(bar_str, "\n");
520532
bar_str = realloc(bar_str, strlen(bar_str) + 1);
@@ -811,6 +823,7 @@ int run_cmd(const char *cmd)
811823
kita_child_s *child = kita_child_new(cmd, 0, 0, 0);
812824
kita_child_open(child); // runs the child via fork/execvp
813825
kita_child_close(child); // does not stop the child, just closes com channels
826+
kita_child_free(&child);
814827
return 1; // TODO
815828
}
816829

@@ -1091,6 +1104,9 @@ static void cleanup(state_s *state)
10911104
state->sparks = NULL;
10921105
state->num_sparks = 0;
10931106

1107+
// free albedo
1108+
free_thing(&state->albedo);
1109+
10941110
// free blocks
10951111
free_blocks(state);
10961112
free(state->blocks);
@@ -1284,7 +1300,7 @@ int main(int argc, char **argv)
12841300
//
12851301

12861302
thing_s *albedo = &(state.albedo); // For convenience
1287-
albedo->sid = ALBEDO_SID;
1303+
albedo->sid = strdup(ALBEDO_SID);
12881304
albedo->t_type = THING_BLOCK;
12891305
albedo->b_type = BLOCK_NONE;
12901306
cfg_init(&albedo->cfg, ALBEDO_SID, BLOCK_OPT_COUNT);

0 commit comments

Comments
 (0)