Skip to content

Commit ec4ffda

Browse files
committed
Applying a major patch by Juan Lasca, 2022-08-14
It includes fixing: * memory leaks (mostly adding calls to 'free' when returning from a function) * uninitialized variables (giving them a reasonable initial value when declared) * multiple definitions (changed app.h,c and process.h,c to declare global variables, instead of implementing them upon #inclusion). Seems like modern compilers treat this as an error, and is also prone to hard-to-debug problems if ignored. Also, the patch includes former proposals by Carl Hansen: * changed the macro 'CHECK_ALLOC' to log an error and exit the application immediately (most of these calls were made to check for memory exhaustion errors anyway). * added the macro 'PERROR' as a more verbose alternative to the perror function, using the 'REPORT' macro.
1 parent bf5354a commit ec4ffda

18 files changed

+128
-55
lines changed

src/aio.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ aio_open_conc (xmlTextReader * xml)
242242
int rule = -1, sen_depth = 0, line_num = 0;
243243
unsigned char * text, * file;
244244
text = file = NULL;
245-
short * refs;
245+
short * refs = 0;
246246
int i;
247247

248248
got_rule = got_refs = got_depth = got_file = got_text = 0;

src/app.c

+14-7
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
#include "elm-conn.xpm"
4747
#include "nil-conn.xpm"
4848

49+
aris_app * the_app = 0;
50+
4951
// Macro to get the length of a file.
5052

5153
#ifndef WIN32
@@ -77,7 +79,7 @@ init_app (int boolean, int verbose)
7779
aris_app * app = (aris_app *) calloc (1, sizeof (aris_app));
7880
if (!app)
7981
{
80-
perror (NULL);
82+
PERROR (NULL);
8183
return NULL;
8284
}
8385

@@ -212,8 +214,8 @@ the_app_get_conn_by_type (char * type)
212214
int
213215
the_app_get_color_by_type (aris_app * app, char * type)
214216
{
215-
int ret;
216217
int i;
218+
int ret = 0;
217219
for (i = 4; i < NUM_DISPLAY_CONFS; i++)
218220
{
219221
if (!strcmp (type, display_conf[i].label))
@@ -337,7 +339,7 @@ the_app_read_config_file (aris_app * app)
337339
conf_file = fopen (path, "r+");
338340
if (!conf_file)
339341
{
340-
perror ("load_config_file");
342+
PERROR ("load_config_file");
341343
return -2;
342344
}
343345
free (path);
@@ -386,22 +388,26 @@ the_app_make_default_config_file (char * path)
386388
config_file = fopen (path, "w");
387389
if (!config_file)
388390
{
389-
perror (NULL);
391+
PERROR (NULL);
390392
return -2;
391393
}
392394

393395
unsigned char * conf_def;
394396
conf_def = config_default ();
395397
if (!conf_def)
396-
return AEC_MEM;
398+
{
399+
fclose (config_file);
400+
return AEC_MEM;
401+
}
397402

398403
int len, rc;
399404
len = strlen (conf_def);
400405

401406
rc = fwrite (conf_def, 1, len, config_file);
402407
if (rc != len)
403408
{
404-
perror ("default_config_file_fwrite");
409+
PERROR ("default_config_file_fwrite");
410+
fclose (config_file);
405411
return -2;
406412
}
407413

@@ -849,6 +855,7 @@ the_app_submit (const char * user_email, const char * instr_email,
849855
if (ret_chk != 1)
850856
{
851857
printf ("Submission Error - please specify a valid email address.\n");
858+
free (email_base);
852859
return -3;
853860
}
854861

@@ -903,7 +910,7 @@ the_app_submit (const char * user_email, const char * instr_email,
903910
if (!ap_file)
904911
{
905912
free (ap_file_name);
906-
perror (NULL);
913+
PERROR (NULL);
907914
return AEC_IO;
908915
}
909916

src/aris-proof.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ aris_proof_create_sentence (aris_proof * ap, sen_data * sd, int undo)
613613

614614
//fprintf (stderr, "create_sentence: sen->line_num == %i\n", sentence_get_line_no (sen));
615615

616-
undo_info ui;
616+
undo_info ui = { 0 };
617617
ui.type = -1;
618618
if (undo)
619619
ui = undo_info_init_one (ap, sen, UIT_ADD_SEN);

src/aris.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ a maximum of 256 filenames can be specified, ignoring file \"%s\".\n", optarg);
565565
sizeof (char));
566566
if (!ai->file_name[cur_file])
567567
{
568-
perror (NULL);
568+
PERROR (NULL);
569569
exit (EXIT_FAILURE);
570570
}
571571

@@ -638,7 +638,7 @@ a maximum of 256 filenames can be specified, ignoring file \"%s\".\n", optarg);
638638
sizeof (char));
639639
if (!ai->latex_name[cur_latex])
640640
{
641-
perror (NULL);
641+
PERROR (NULL);
642642
exit (EXIT_FAILURE);
643643
}
644644

@@ -671,7 +671,7 @@ ignoring unrecognized option: \"%c\" .\n",c);
671671
return 0;
672672
}
673673

674-
aris_app *the_app;
674+
extern aris_app *the_app;
675675

676676
/* Main function. */
677677
int
@@ -763,7 +763,7 @@ a conclusion must be specified in evaluation mode.\n");
763763
proof = (proof_t **) calloc (cur_file, sizeof (proof_t *));
764764
if (!proof)
765765
{
766-
perror (NULL);
766+
PERROR (NULL);
767767
exit (EXIT_FAILURE);
768768
}
769769

src/callbacks.c

+20-7
Original file line numberDiff line numberDiff line change
@@ -651,22 +651,34 @@ gui_save (aris_proof * ap, int save_as)
651651

652652
proof = aris_proof_to_proof (ap);
653653
if (!proof)
654-
return AEC_MEM;
654+
{
655+
free (fname);
656+
return AEC_MEM;
657+
}
655658

656659
ret = aio_save (proof, fname);
657660
if (ret < 0)
658-
return AEC_MEM;
661+
{
662+
free (fname);
663+
return AEC_MEM;
664+
}
659665

660-
undo_info ui;
666+
undo_info ui = { 0 };
661667
ui.type = -1;
662668

663669
ret = aris_proof_set_changed (ap, 0, ui);
664670
if (ret < 0)
665-
return AEC_MEM;
671+
{
672+
free (fname);
673+
return AEC_MEM;
674+
}
666675

667676
ret = aris_proof_set_filename (ap, fname);
668677
if (ret < 0)
669-
return AEC_MEM;
678+
{
679+
free (fname);
680+
return AEC_MEM;
681+
}
670682

671683
free (fname);
672684
}
@@ -1084,7 +1096,8 @@ gui_customize_show (GtkWidget * window)
10841096

10851097
FILE * conf_file;
10861098
char * path, * home_dir;
1087-
int alloc_size, path_pos = 0;
1099+
int alloc_size = 0;
1100+
int path_pos = 0;
10881101

10891102
home_dir = getenv ("HOME");
10901103
if (!home_dir)
@@ -1105,7 +1118,7 @@ gui_customize_show (GtkWidget * window)
11051118
conf_file = fopen (path, "w");
11061119
if (!conf_file)
11071120
{
1108-
perror (NULL);
1121+
PERROR (NULL);
11091122
gtk_widget_destroy (dialog);
11101123
return -2;
11111124
}

src/conf-file.c

+2
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ conf_menu_value (conf_obj * obj, int get)
333333
}
334334
else
335335
{
336+
free (ret);
336337
ret = NULL;
337338
}
338339

@@ -441,6 +442,7 @@ conf_font_value (conf_obj * obj, int get)
441442
if (ret_chk != 1)
442443
strcpy (size, _("Default"));
443444
*/
445+
free (size);
444446
size = obj->label;
445447

446448
alloc_size = strlen (size) + 16 + (int) log10 ((double) val) + 1;

src/goal.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ goal_add_line (goal_t * goal, sen_data * sd)
303303
NULL, GTK_POS_BOTTOM, 1, 1);
304304
gtk_widget_show_all (sen->panel);
305305

306-
undo_info ui;
306+
undo_info ui = { 0 };
307307
ui.type = -1;
308308

309309
int ret;
@@ -335,7 +335,7 @@ goal_rem_line (goal_t * goal)
335335
gtk_widget_override_background_color (sen->eventbox, GTK_STATE_NORMAL, NULL);
336336
}
337337

338-
undo_info ui;
338+
undo_info ui = { 0 };
339339
ui.type = -1;
340340

341341
sen_parent_rem_sentence ((sen_parent *) goal, SEN_PARENT (goal)->focused->value);
@@ -369,5 +369,7 @@ goal_update_title (goal_t * goal)
369369

370370
gtk_window_set_title (GTK_WINDOW (SEN_PARENT (goal)->window), new_title);
371371

372+
free (new_title);
373+
372374
return 0;
373375
}

src/interop-isar.c

+26-9
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,7 @@ isar_parse_theory (char * input_str)
604604
// next will be the name.
605605
// That's not exactly important though.
606606
// Mostly want the includes.
607+
return 0;
607608
}
608609

609610
int
@@ -1465,7 +1466,7 @@ isar_parse_fun (char * fun, char ** out_str)
14651466
pos += 14;
14661467
}
14671468

1468-
int fun_pos;
1469+
int fun_pos = 0;
14691470

14701471
//TODO: Calculate the ACTUAL size this should be.
14711472

@@ -1708,7 +1709,8 @@ isar_parse_theorem (char * thm, char ** out_str)
17081709
int
17091710
isar_parse_datatype (char * type, vec_t * constructors)
17101711
{
1711-
int init_pos, pos;
1712+
int init_pos = 0;
1713+
int pos = 0;
17121714
int i, chk;
17131715

17141716
init_pos = 0;
@@ -1741,7 +1743,8 @@ isar_parse_primrec (char * prim, char ** out_str)
17411743
int
17421744
isar_parse_case (char * cs, char ** out_str)
17431745
{
1744-
vec_t * pre, * post;
1746+
vec_t * pre = 0;
1747+
vec_t * post = 0;
17451748
int i, pos, init_pos;
17461749
char * var;
17471750
int out_pos;
@@ -1782,7 +1785,7 @@ isar_parse_case (char * cs, char ** out_str)
17821785
tmp_str[pos - init_pos] = '\0';
17831786

17841787
// Process it, converting it to a notation aris will recognize.
1785-
char * new_str;
1788+
char * new_str = 0;
17861789
//new_str = isar_convert (tmp_str);
17871790
//if (!new_str)
17881791
// return -1;
@@ -2064,7 +2067,7 @@ parse_thy (char * filename, proof_t * proof)
20642067
if (chk)
20652068
{
20662069
fclose (file);
2067-
perror (NULL);
2070+
PERROR (NULL);
20682071
return -1;
20692072
}
20702073

@@ -2074,7 +2077,7 @@ parse_thy (char * filename, proof_t * proof)
20742077
if (chk)
20752078
{
20762079
fclose (file);
2077-
perror (NULL);
2080+
PERROR (NULL);
20782081
return -1;
20792082
}
20802083

@@ -2093,6 +2096,7 @@ parse_thy (char * filename, proof_t * proof)
20932096
if (!ret_chk_str)
20942097
{
20952098
// Invalid .thy file, return an error.
2099+
free (buffer);
20962100
return -2;
20972101
}
20982102

@@ -2122,22 +2126,35 @@ parse_thy (char * filename, proof_t * proof)
21222126

21232127
chk = get_std_seqs ();
21242128
if (chk == -1)
2125-
return -1;
2129+
{
2130+
free (buffer);
2131+
return -1;
2132+
}
21262133

21272134
refs = init_vec (sizeof (char *));
21282135
if (!refs)
2129-
return -1;
2136+
{
2137+
free (buffer);
2138+
return -1;
2139+
}
21302140

21312141
lms = init_vec (sizeof (char *));
21322142
if (!lms)
2133-
return -1;
2143+
{
2144+
free (buffer);
2145+
destroy_vec (refs);
2146+
return -1;
2147+
}
21342148

21352149
// After this, buf_pos will point to the position of 'begin'.
21362150
buf_pos = strstr (buffer, "begin");
21372151

21382152
if (strncmp (buf_pos, "begin", 5))
21392153
{
21402154
// Error stuff.
2155+
free (buffer);
2156+
destroy_vec (refs);
2157+
destroy_vec (lms);
21412158
return -2;
21422159
}
21432160

src/list.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ init_list ()
3737
ls = (list_t *) calloc (1, sizeof (list_t));
3838
if (!ls)
3939
{
40-
perror (NULL);
40+
PERROR (NULL);
4141
return NULL;
4242
}
4343

@@ -107,7 +107,7 @@ ls_ins_obj (list_t * ls, void * obj, item_t * it)
107107
ins_itm = (item_t *) calloc (1, sizeof (item_t));
108108
if (!ins_itm)
109109
{
110-
perror (NULL);
110+
PERROR (NULL);
111111
return NULL;
112112
}
113113
ins_itm->prev = ins_itm->next = NULL;

0 commit comments

Comments
 (0)