gpgscm: Fix memory leaks.
authorJustus Winter <justus@g10code.com>
Tue, 28 Jun 2016 16:13:40 +0000 (18:13 +0200)
committerJustus Winter <justus@g10code.com>
Tue, 28 Jun 2016 16:21:50 +0000 (18:21 +0200)
* tests/gpgscm/ffi-private.h (ffi_schemify_name): Fix prototype.
(ffi_define_function_name): Free schemified name.
(ffi_define_function): Likewise.
(ffi_define_constant): Likewise.
(ffi_define_variable_pointer): Likewise.
* tests/gpgscm/ffi.c (do_wait_processes): Free arrays.
(ffi_schemify_name): Fix type.
* tests/gpgscm/main.c (main): Free 'sc'.

Signed-off-by: Justus Winter <justus@g10code.com>
tests/gpgscm/ffi-private.h
tests/gpgscm/ffi.c
tests/gpgscm/main.c

index 849d1b7..87f491f 100644 (file)
@@ -84,7 +84,7 @@ int ffi_bool_value (scheme *sc, pointer p);
 #define FFI_RETURN_STRING(SC, X)                       \
   FFI_RETURN_POINTER ((SC), mk_string ((SC), (X)))
 
-const char *ffi_schemify_name (const char *s, int macro);
+char *ffi_schemify_name (const char *s, int macro);
 
 void ffi_scheme_eval (scheme *sc, const char *format, ...)
   GPGRT_ATTR_PRINTF (2, 3);
@@ -93,32 +93,46 @@ pointer ffi_sprintf (scheme *sc, const char *format, ...)
 
 #define ffi_define_function_name(SC, NAME, F)                          \
   do {                                                                 \
+    char *_fname = ffi_schemify_name ("_" #F, 0);                        \
     scheme_define ((SC),                                               \
                   (SC)->global_env,                                    \
-                  mk_symbol ((SC), ffi_schemify_name ("_" #F, 0)),     \
+                  mk_symbol ((SC), _fname),                            \
                   mk_foreign_func ((SC), (do_##F)));                   \
     ffi_scheme_eval ((SC),                                             \
                     "(define (%s . a) (ffi-apply \"%s\" %s a))",       \
-                    (NAME), (NAME), ffi_schemify_name ("_" #F, 0));    \
+                    (NAME), (NAME), _fname);                           \
+    free (_fname);                                                      \
   } while (0)
 
-#define ffi_define_function(SC, F)                             \
-  ffi_define_function_name ((SC), ffi_schemify_name (#F, 0), F)
+#define ffi_define_function(SC, F)                                      \
+  do {                                                                 \
+    char *_name = ffi_schemify_name (#F, 0);                            \
+    ffi_define_function_name ((SC), _name, F);                          \
+    free (_name);                                                       \
+  } while (0)
 
 #define ffi_define_constant(SC, C)                                     \
-  scheme_define ((SC),                                                 \
-                (SC)->global_env,                                      \
-                mk_symbol ((SC), ffi_schemify_name (#C, 1)),           \
-                mk_integer ((SC), (C)))
+  do {                                                                 \
+    char *_name = ffi_schemify_name (#C, 1);                            \
+    scheme_define ((SC),                                                \
+                   (SC)->global_env,                                   \
+                   mk_symbol ((SC), _name),                             \
+                   mk_integer ((SC), (C)));                             \
+    free (_name);                                                       \
+  } while (0)
 
 #define ffi_define(SC, SYM, EXP)                                       \
   scheme_define ((SC), (SC)->global_env, mk_symbol ((SC), (SYM)), EXP)
 
 #define ffi_define_variable_pointer(SC, C, P)                          \
-  scheme_define ((SC),                                                 \
-                (SC)->global_env,                                      \
-                mk_symbol ((SC), ffi_schemify_name (#C, 0)),           \
-                (P))
+  do {                                                                 \
+    char *_name = ffi_schemify_name (#C, 0);                            \
+    scheme_define ((SC),                                                \
+                   (SC)->global_env,                                   \
+                   mk_symbol ((SC), _name),                             \
+                   (P));                                                \
+    free (_name);                                                       \
+  } while (0)
 
 #define ffi_define_variable_integer(SC, C)                             \
   ffi_define_variable_pointer ((SC), C, (SC)->vptr->mk_integer ((SC), C))
index dcdadaa..acfe1c7 100644 (file)
@@ -776,6 +776,9 @@ do_wait_processes (scheme *sc, pointer args)
                                               (long) retcodes[count-1-i]),
                         retcodes_list);
 
+  xfree (names);
+  xfree (pids);
+  xfree (retcodes);
   FFI_RETURN_POINTER (sc, retcodes_list);
 }
 
@@ -1098,7 +1101,7 @@ ffi_list2intv (scheme *sc, pointer list, int **intv, size_t *len)
 }
 
 \f
-const char *
+char *
 ffi_schemify_name (const char *s, int macro)
 {
   char *n = strdup (s), *p;
index 3414e3d..adb4e33 100644 (file)
@@ -282,5 +282,6 @@ main (int argc, char **argv)
     }
 
   scheme_deinit (sc);
+  xfree (sc);
   return EXIT_SUCCESS;
 }