chiark / gitweb /
Some configuration-related memory hygeine.
authorRichard Kettlewell <rjk@greenend.org.uk>
Wed, 25 Nov 2009 10:57:30 +0000 (10:57 +0000)
committerRichard Kettlewell <rjk@greenend.org.uk>
Wed, 25 Nov 2009 10:57:30 +0000 (10:57 +0000)
Use of libgc means we don't have to worry about memory leakage as such
in most of the code.  But in some cases libgc isn't appropriate and
GTK+ doesn't get on well with it (or at least used not to).

clients/disorder.c
disobedience/login.c
lib/asprintf.c
lib/configuration.c
lib/configuration.h
lib/vector.c
lib/vector.h

index 1d55b8db6a7f14eedba2f555e69a5e13872dfb88..298494a9befa8bca4eaa2929a90f78e2e63c475f 100644 (file)
@@ -864,11 +864,14 @@ int main(int argc, char **argv) {
   }
   if(config_read(0, NULL)) disorder_fatal(0, "cannot read configuration");
   if(user) {
   }
   if(config_read(0, NULL)) disorder_fatal(0, "cannot read configuration");
   if(user) {
-    config->username = user;
+    xfree(config->username);
+    config->username = xstrdup(user);
     config->password = 0;
   }
     config->password = 0;
   }
-  if(password)
-    config->password = password;
+  if(password) {
+    xfree(config->password);
+    config->password = xstrdup(password);
+  }
   if(local)
     config->connect.af = -1;
   if(wfr)
   if(local)
     config->connect.af = -1;
   if(wfr)
@@ -899,10 +902,12 @@ int main(int argc, char **argv) {
       vector_append(&args, nullcheck(mb2utf8(argv[n + j])));
     vector_terminate(&args);
     commands[i].fn(args.vec + 1);
       vector_append(&args, nullcheck(mb2utf8(argv[n + j])));
     vector_terminate(&args);
     commands[i].fn(args.vec + 1);
+    xfree(args.vec);
     n += j;
   }
   if(client && disorder_close(client)) exit(EXIT_FAILURE);
   if(fclose(stdout) < 0) disorder_fatal(errno, "error closing stdout");
     n += j;
   }
   if(client && disorder_close(client)) exit(EXIT_FAILURE);
   if(fclose(stdout) < 0) disorder_fatal(errno, "error closing stdout");
+  config_free(config);
   return status;
 }
 
   return status;
 }
 
index 36850c11da60b9f201c59da9b0033c3bea1cdedc..90eaa0dde4ddcd30dbd7b4f9065b35a2720538c4 100644 (file)
@@ -120,11 +120,13 @@ static void set_service(struct config *c, const char *s) {
 }
 
 static void set_username(struct config *c, const char *s) {
 }
 
 static void set_username(struct config *c, const char *s) {
-  c->username = s;
+  xfree(c->username);
+  c->username = xstrdup(s);
 }
 
 static void set_password(struct config *c, const char *s) {
 }
 
 static void set_password(struct config *c, const char *s) {
-  c->password = s;
+  xfree(c->password);
+  c->password = xstrdup(s);
 }
 
 /** @brief Table used to generate the form */
 }
 
 /** @brief Table used to generate the form */
index da37319012c19f5f6a85a9c8f0f7a5577f280d48..ddfc12ece96cae4e269cb339a9df9a74e58f84c6 100644 (file)
 int byte_vasprintf(char **ptrp,
                   const char *fmt,
                   va_list ap) {
 int byte_vasprintf(char **ptrp,
                   const char *fmt,
                   va_list ap) {
+  struct sink *s;
   struct dynstr d;
   int n;
 
   dynstr_init(&d);
   struct dynstr d;
   int n;
 
   dynstr_init(&d);
-  if((n = byte_vsinkprintf(sink_dynstr(&d), fmt, ap)) >= 0) {
+  s = sink_dynstr(&d);
+  n = byte_vsinkprintf(s, fmt, ap);
+  xfree(s);
+  if(n >= 0) {
     dynstr_terminate(&d);
     *ptrp = d.vec;
   }
     dynstr_terminate(&d);
     *ptrp = d.vec;
   }
index e2af880368ee04290c147167c7be6b988ef83e08..c059bf7718400da36c086dbfeedd7af0a067dc34 100644 (file)
@@ -240,6 +240,7 @@ static int set_string(const struct config_state *cs,
                   cs->path, cs->line, whoami->name);
     return -1;
   }
                   cs->path, cs->line, whoami->name);
     return -1;
   }
+  xfree(VALUE(cs->config, char *));
   VALUE(cs->config, char *) = xstrdup(vec[0]);
   return 0;
 }
   VALUE(cs->config, char *) = xstrdup(vec[0]);
   return 0;
 }
@@ -520,8 +521,7 @@ static int set_rights(const struct config_state *cs,
                   cs->path, cs->line, vec[0]);
     return -1;
   }
                   cs->path, cs->line, vec[0]);
     return -1;
   }
-  *ADDRESS(cs->config, char *) = vec[0];
-  return 0;
+  return set_string(cs, whoami, nvec, vec);
 }
 
 static int set_netaddress(const struct config_state *cs,
 }
 
 static int set_netaddress(const struct config_state *cs,
@@ -644,7 +644,7 @@ static const struct conftype
   type_namepart = { set_namepart, free_namepartlist },
   type_transform = { set_transform, free_transformlist },
   type_netaddress = { set_netaddress, free_netaddress },
   type_namepart = { set_namepart, free_namepartlist },
   type_transform = { set_transform, free_transformlist },
   type_netaddress = { set_netaddress, free_netaddress },
-  type_rights = { set_rights, free_none };
+  type_rights = { set_rights, free_string };
 
 /* specific validation routine */
 
 
 /* specific validation routine */
 
@@ -1047,6 +1047,7 @@ static int validate_destaddr(const struct config_state attribute((unused)) *cs,
     disorder_error(0, "%s:%d: destination address required", cs->path, cs->line);
     return -1;
   }
     disorder_error(0, "%s:%d: destination address required", cs->path, cs->line);
     return -1;
   }
+  xfree(na->address);
   return 0;
 }
 
   return 0;
 }
 
@@ -1174,7 +1175,9 @@ static int config_set_args(const struct config_state *cs,
     vector_append(v, s);
   va_end(ap);
   vector_terminate(v);
     vector_append(v, s);
   va_end(ap);
   vector_terminate(v);
-  return config_set(cs, v->nvec, v->vec);
+  int rc = config_set(cs, v->nvec, v->vec);
+  xfree(v->vec);
+  return rc;
 }
 
 /** @brief Error callback used by config_include()
 }
 
 /** @brief Error callback used by config_include()
@@ -1415,7 +1418,7 @@ static void set_configfile(void) {
  *
  * @p c is indeterminate after this function is called.
  */
  *
  * @p c is indeterminate after this function is called.
  */
-static void config_free(struct config *c) {
+void config_free(struct config *c) {
   int n;
 
   if(c) {
   int n;
 
   if(c) {
index 9170a2f2b752c118e937099a1fa38e116a4015d1..f1794aa1c5b92dd03d316932ea544dc5c7982cc5 100644 (file)
@@ -195,10 +195,10 @@ struct config {
   const char *home;
 
   /** @brief Login username */
   const char *home;
 
   /** @brief Login username */
-  const char *username;
+  char *username;
 
   /** @brief Login password */
 
   /** @brief Login password */
-  const char *password;
+  char *password;
 
   /** @brief Address to connect to */
   struct netaddress connect;
 
   /** @brief Address to connect to */
   struct netaddress connect;
@@ -320,6 +320,8 @@ char *config_usersysconf(const struct passwd *pw );
 char *config_private(void);
 /* get the private config file */
 
 char *config_private(void);
 /* get the private config file */
 
+void config_free(struct config *c);
+
 extern char *configfile;
 extern int config_per_user;
 
 extern char *configfile;
 extern int config_per_user;
 
index 4d798f1b31037554a22a466a1271e8cb1f1e8bba..65a84e024490b7c4ad3a613f072264eff95a1978 100644 (file)
@@ -38,6 +38,19 @@ void dynstr_append_bytes(struct dynstr *v, const char *ptr, size_t n) {
   }
 }
 
   }
 }
 
+/** @brief Free a string list */
+void free_strings(int nvec, char **vec) {
+  for(int n = 0; n < nvec; ++n)
+    xfree(vec[n]);
+  xfree(vec);
+}
+
+/** @brief Free and re-initialize a vector */
+void vector_clear(struct vector *v) {
+  free_strings(v->nvec, v->vec);
+  vector_init(v);
+}
+
 /*
 Local Variables:
 c-basic-offset:2
 /*
 Local Variables:
 c-basic-offset:2
index 31dfeda5e86ffee911719a8e901a1e927f781cdc..5bd9b9754b2585ab6be6630416fdcf1f9bf50a23 100644 (file)
@@ -94,6 +94,9 @@ static inline void dynstr_append_string(struct dynstr *v, const char *ptr) {
   dynstr_append_bytes(v, ptr, strlen(ptr));
 }
 
   dynstr_append_bytes(v, ptr, strlen(ptr));
 }
 
+void free_strings(int nvec, char **vec);
+void vector_clear(struct vector *v);
+
 #endif /* VECTOR_H */
 
 /*
 #endif /* VECTOR_H */
 
 /*