[Tux4kids-commits] r391 - tuxmath/trunk/src
tholy-guest at alioth.debian.org
tholy-guest at alioth.debian.org
Mon Dec 24 12:55:31 UTC 2007
Author: tholy-guest
Date: 2007-12-24 12:55:31 +0000 (Mon, 24 Dec 2007)
New Revision: 391
Modified:
tuxmath/trunk/src/fileops.c
tuxmath/trunk/src/setup.c
tuxmath/trunk/src/titlescreen.c
Log:
Fix memory leaks detected by Valgrind.
Modified: tuxmath/trunk/src/fileops.c
===================================================================
--- tuxmath/trunk/src/fileops.c 2007-12-24 10:44:00 UTC (rev 390)
+++ tuxmath/trunk/src/fileops.c 2007-12-24 12:55:31 UTC (rev 391)
@@ -440,7 +440,15 @@
if (user_data_dir != NULL)
free(user_data_dir); // clear the previous setting
- user_data_dir = strdup(dirname);
+ // Allocate space for the directory name. We do it with +2 because
+ // we have to leave room for a possible addition of a "/"
+ // terminator.
+ user_data_dir = (char*) malloc((strlen(dirname)+2)*sizeof(char));
+ if (user_data_dir == NULL) {
+ fprintf(stderr,"Error: insufficient memory for duplicating string %s.\n",dirname);
+ exit(EXIT_FAILURE);
+ }
+ strcpy(user_data_dir,dirname);
// Check to see that dirname is properly terminated
len = strlen(user_data_dir);
@@ -1177,7 +1185,7 @@
get_user_data_dir_with_subdir(opt_path);
strncpy(user_login_questions_file,opt_path,PATH_MAX);
strncat(user_login_questions_file,USER_LOGIN_QUESTIONS_FILENAME,PATH_MAX-strlen(user_login_questions_file));
- n_entries = 0;
+ n_entries = 0;
fp = fopen(user_login_questions_file,"r");
if (fp)
{
@@ -1198,16 +1206,33 @@
{
DIR *dir;
- if (user_data_dir != NULL)
+ // The space for user_data_dir has to have sufficient memory
+ // available for concatenating subdir and a possible final "/",
+ // hence the +2s.
+ if (user_data_dir != NULL) {
+ user_data_dir = (char*) realloc(user_data_dir,(strlen(user_data_dir) + strlen(subdir) + 2)*sizeof(char));
+ if (user_data_dir == NULL) {
+ fprintf(stderr,"Error allocating memory in user_data_dirname_down.\n");
+ exit(EXIT_FAILURE);
+ }
strcat(user_data_dir,subdir);
- else
- user_data_dir = strdup(subdir);
+ }
+ else {
+ user_data_dir = (char*) malloc((strlen(subdir)+2)*sizeof(char));
+ if (user_data_dir == NULL) {
+ fprintf(stderr,"Error allocating memory in user_data_dirname_down.\n");
+ exit(EXIT_FAILURE);
+ }
+ strcpy(user_data_dir,subdir);
+ }
strcat(user_data_dir,"/");
dir = opendir(user_data_dir);
if (dir == NULL) {
printf("User data directory cannot be opened, there is a configuration error\n");
printf("Continuing anyway without saving or loading individual settings.\n");
}
+ else
+ closedir(dir);
}
@@ -2812,7 +2837,7 @@
continue;
}
n_entries++;
- *lines = realloc(*lines,n_entries*sizeof(char*));
+ *lines = (char**) realloc(*lines,n_entries*sizeof(char*));
if (*lines == NULL) {
// Memory allocation error
printf("Error #1 allocating memory in read_lines_from_file\n");
Modified: tuxmath/trunk/src/setup.c
===================================================================
--- tuxmath/trunk/src/setup.c 2007-12-24 10:44:00 UTC (rev 390)
+++ tuxmath/trunk/src/setup.c 2007-12-24 12:55:31 UTC (rev 391)
@@ -194,6 +194,7 @@
/* Handle any arguments passed from command line */
void handle_command_args(int argc, char* argv[])
{
+ DIR *dirp;
int i;
for (i = 1; i < argc; i++)
@@ -286,10 +287,12 @@
}
else // see whether the specified name is a directory
{
- if (opendir(argv[i+1]) == NULL)
+ if ((dirp = opendir(argv[i+1])) == NULL)
fprintf(stderr,"homedir: %s is not a directory, or it could not be read\n", argv[i+1]);
- else
+ else {
set_user_data_dir(argv[i+1]); // copy the homedir setting
+ closedir(dirp);
+ }
i++; // to pass over the next argument, so remaining options parsed
}
}
Modified: tuxmath/trunk/src/titlescreen.c
===================================================================
--- tuxmath/trunk/src/titlescreen.c 2007-12-24 10:44:00 UTC (rev 390)
+++ tuxmath/trunk/src/titlescreen.c 2007-12-24 12:55:31 UTC (rev 391)
@@ -964,8 +964,14 @@
// User pressed escape or selected Quit/Back, handle by quitting
// or going up a level
if (level == 0) {
- // We are going to quit without logging in. So, we don't have
- // to worry about cleaning up memory.
+ // We are going to quit without logging in.
+ // Clean up memory (prob. not necessary, but prevents Valgrind errors!)
+ for (i = 0; i < n_login_questions; i++)
+ free(user_login_questions[i]);
+ free(user_login_questions);
+ for (i = 0; i < n_users; i++)
+ free(user_names[i]);
+ free(user_names);
return -1;
}
else {
@@ -1002,7 +1008,12 @@
n_users = read_user_menu_entries(&user_names);
}
- // The user home directory is set, signal success
+ // The user home directory is set, clean up remaining memory
+ for (i = 0; i < n_login_questions; i++)
+ free(user_login_questions[i]);
+ free(user_login_questions);
+
+ // Signal success
return 0;
}
More information about the Tux4kids-commits
mailing list