瀏覽代碼

Fixed memory leak

isundil 9 年之前
父節點
當前提交
22b3c3f2da
共有 4 個文件被更改,包括 19 次插入1 次删除
  1. 15 0
      src/pathutil.c
  2. 1 0
      src/sandbox.h
  3. 1 0
      test/write/functions.c
  4. 2 1
      test/write/main.c

+ 15 - 0
src/pathutil.c

@@ -72,3 +72,18 @@ unsigned long long int hash_path(const char *filename)
 	return result;
 }
 
+static inline int release_fds_struct(void **t_fds, void *nullptr)
+{
+	free(*(t_fd**)t_fds);
+	return 0;
+}
+
+int release_file(void **t_fi, void *env)
+{
+	sllist_foreach((*(t_fileinfo**)t_fi)->fds, release_fds_struct, NULL);
+	sllist_destroy((*(t_fileinfo**)t_fi)->fds);
+	free((*(t_fileinfo**)t_fi)->filename);
+	free(*(t_fileinfo**)t_fi);
+	return 0;
+}
+

+ 1 - 0
src/sandbox.h

@@ -89,6 +89,7 @@ t_fileinfo *get_fileinfo_fd(const struct s_sandboxenv *env, const int fd);
 unsigned long long int hash_path(const char *filename);
 int file_exists(const char *filename);
 int fileinfo_compare_tfd(const void *a, const void *b);
+int release_file(void **t_fileinfo, void *env);
 
 /* applay.c */
 void prompt_sandbox(struct s_sandboxenv *env);

+ 1 - 0
test/write/functions.c

@@ -45,6 +45,7 @@ void tests_init_env(struct s_sandboxenv *env, t_param *params)
 void tests_release_env(struct s_sandboxenv *env, t_param *params)
 {
 	free(params->tmppath);
+	sllist_foreach(env->filetable, release_file, env);
 	sllist_destroy(env->filetable);
 }
 

+ 2 - 1
test/write/main.c

@@ -67,6 +67,7 @@ int test_open(struct s_sandboxenv *env, int *final_fd)
 	_assertEqual(fd_struct->fd, fd);
 	_assertEqual(fd_struct->flags, O_WRONLY);
 	_assertEqual(fd_struct->mod, 0646);
+	*final_fd = fd;
 	return 0;
 }
 
@@ -87,8 +88,8 @@ int main()
 	success &= !test_open(&env, &fd);
 	success &= !test_write(&env);
 
-	tests_release_env(&env, &params);
 	fakeclose(fd, &env);
+	tests_release_env(&env, &params);
 	unlink("_test");
 	exit(success ? EXIT_SUCCESS: EXIT_FAILURE);
 }