chiark / gitweb /
More memory hygeine
authorRichard Kettlewell <rjk@greenend.org.uk>
Wed, 25 Nov 2009 11:17:45 +0000 (11:17 +0000)
committerRichard Kettlewell <rjk@greenend.org.uk>
Wed, 25 Nov 2009 11:17:45 +0000 (11:17 +0000)
12 files changed:
clients/disorder.c
lib/Makefile.am
lib/asprintf.c
lib/authhash.c
lib/authhash.h
lib/charset.h
lib/charsetf.c [new file with mode: 0644]
lib/client-common.c
lib/client.c
lib/fprintf.c
lib/vector.c
lib/vector.h

index 298494a9befa8bca4eaa2929a90f78e2e63c475f..02f6582dde189d4afd924376840feba532f96fa1 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * This file is part of DisOrder.
- * Copyright (C) 2004-2008 Richard Kettlewell
+ * Copyright (C) 2004-2009 Richard Kettlewell
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -98,7 +98,9 @@ static void cf_version(char attribute((unused)) **argv) {
   char *v;
 
   if(disorder_version(getclient(), &v)) exit(EXIT_FAILURE);
-  xprintf("%s\n", nullcheck(utf82mb(v)));
+  v = nullcheck(utf82mb_f(v));
+  xprintf("%s\n", v);
+  xfree(v);
 }
 
 static void print_queue_entry(const struct queue_entry *q) {
@@ -257,7 +259,7 @@ static void cf_get(char **argv) {
   char *value;
 
   if(disorder_get(getclient(), argv[0], argv[1], &value)) exit(EXIT_FAILURE);
-  xprintf("%s\n", nullcheck(utf82mb(value)));
+  xprintf("%s\n", nullcheck(utf82mb_f(value)));
 }
 
 static void cf_length(char **argv) {
@@ -339,7 +341,7 @@ static void cf_part(char **argv) {
   char *s;
 
   if(disorder_part(getclient(), &s, argv[0], argv[1], argv[2])) exit(EXIT_FAILURE);
-  xprintf("%s\n", nullcheck(utf82mb(s)));
+  xprintf("%s\n", nullcheck(utf82mb_f(s)));
 }
 
 static int isarg_filename(const char *s) {
@@ -354,7 +356,7 @@ static void cf_resolve(char **argv) {
   char *track;
 
   if(disorder_resolve(getclient(), &track, argv[0])) exit(EXIT_FAILURE);
-  xprintf("%s\n", nullcheck(utf82mb(track)));
+  xprintf("%s\n", nullcheck(utf82mb_f(track)));
 }
 
 static void cf_pause(char attribute((unused)) **argv) {
@@ -385,7 +387,7 @@ static void cf_get_global(char **argv) {
   char *value;
 
   if(disorder_get_global(getclient(), argv[0], &value)) exit(EXIT_FAILURE);
-  xprintf("%s\n", nullcheck(utf82mb(value)));
+  xprintf("%s\n", nullcheck(utf82mb_f(value)));
 }
 
 static void cf_set_global(char **argv) {
@@ -446,7 +448,7 @@ static void cf_userinfo(char **argv) {
 
   if(disorder_userinfo(getclient(), argv[0], argv[1], &s))
     exit(EXIT_FAILURE);
-  xprintf("%s\n", nullcheck(utf82mb(s)));
+  xprintf("%s\n", nullcheck(utf82mb_f(s)));
 }
 
 static int isarg_option(const char *arg) {
index 3c1e2f107af9ccd903b74224ed9229e5776b6c5e..ec674f6fcdd0cc3b752bd2e4ee5e2f12be1e2bfb 100644 (file)
@@ -25,7 +25,7 @@ else
 TRACKDB=trackdb-stub.c
 endif
 
-libdisorder_a_SOURCES=charset.c charset.h              \
+libdisorder_a_SOURCES=charset.c charsetf.c charset.h   \
        addr.c addr.h                                   \
        arcfour.c arcfour.h                             \
        authhash.c authhash.h                           \
index ddfc12ece96cae4e269cb339a9df9a74e58f84c6..fd9fa6cbdce2dde0faadc6e6c1ed48e07469f547 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * This file is part of DisOrder
- * Copyright (C) 2004-2008 Richard Kettlewell
+ * Copyright (C) 2004-2009 Richard Kettlewell
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
index 8cc96f904542949d984c5104efe83be04040ff42..c06c233ed26642833a92517ce9ae425a93ba758e 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * This file is part of DisOrder
- * Copyright (C) 2004, 2006, 2007 Richard Kettlewell
+ * Copyright (C) 2004, 2006, 2007, 2009 Richard Kettlewell
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -64,10 +64,10 @@ static const struct algorithm algorithms[] = {
  * Computes H(challenge|password) and returns it as a newly allocated hex
  * string, or returns NULL on error.
  */
-const char *authhash(const void *challenge, size_t nchallenge,
-                    const char *password, const char *algo) {
+char *authhash(const void *challenge, size_t nchallenge,
+               const char *password, const char *algo) {
   gcrypt_hash_handle h;
-  const char *res;
+  char *res;
   size_t n;
   int id;
 
index 8604a13948f1a81e602ea15992a87ecc15c35c29..7b73d67ea7acfd8a3b7df235ecbf4b945c40d395 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * This file is part of DisOrder.
- * Copyright (C) 2004, 2006, 2007, 2008 Richard Kettlewell
+ * Copyright (C) 2004, 2006-2009 Richard Kettlewell
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -19,8 +19,8 @@
 #ifndef AUTHHASH_H
 #define AUTHHASH_H
 
-const char *authhash(const void *challenge, size_t nchallenge,
-                    const char *user, const char *algo);
+char *authhash(const void *challenge, size_t nchallenge,
+               const char *user, const char *algo);
 int valid_authhash(const char *algo);
 
 #endif /* AUTHHASH_H */
index be2b05a45ae5a3ced48f189fb1b69167b701a330..e3c3a4eb59b560e9a6a9ce4afcd5fd714582e835 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * This file is part of DisOrder.
- * Copyright (C) 2004, 2005, 2007, 2008 Richard Kettlewell
+ * Copyright (C) 2004, 2005, 2007-2009 Richard Kettlewell
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -49,6 +49,16 @@ char *any2any(const char *from/*encoding or 0*/,
  * that iconv knows.  If FROM and TO are both 0 then ANY is returned
  * unchanged. */
 
+char *mb2utf8_f(char *mb);
+char *utf82mb_f(char *utf8);
+char *any2utf8_f(const char *from/*encoding*/,
+                 char *any/*string*/);
+char *any2mb_f(const char *from/*encoding or 0*/,
+               char *any/*string*/);
+char *any2any_f(const char *from/*encoding or 0*/,
+                const char *to/*encoding to 0*/,
+                char *any/*string*/);
+
 /** @brief Insist that @p s is not null
  * @param s Pointer to check
  * @return @p s
diff --git a/lib/charsetf.c b/lib/charsetf.c
new file mode 100644 (file)
index 0000000..cb27974
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * This file is part of DisOrder.
+ * Copyright (C) 2004, 2005, 2007, 2008 Richard Kettlewell
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+/** @file lib/charsetf.c @brief Character set conversion with free() */
+
+#include "common.h"
+
+#include "charset.h"
+#include "mem.h"
+
+char *mb2utf8_f(char *mb) {
+  char *s = mb2utf8(mb);
+  xfree(mb);
+  return s;
+}
+
+char *utf82mb_f(char *utf8) {
+  char *s = utf82mb(utf8);
+  xfree(utf8);
+  return s;
+}
+
+char *any2utf8_f(const char *from,
+                 char *any) {
+  char *s = any2utf8(from, any);
+  xfree(any);
+  return s;
+}
+
+char *any2mb_f(const char *from,
+               char *any) {
+  char *s = any2mb(from, any);
+  xfree(any);
+  return s;
+}
+
+char *any2any_f(const char *from,
+                const char *to,
+                char *any) {
+  char *s = any2any(from, to, any);
+  xfree(any);
+  return s;
+}
+
+/*
+Local Variables:
+c-basic-offset:2
+comment-column:40
+fill-column:79
+indent-tabs-mode:nil
+End:
+*/
index c55da8939fb6084e9f52e98910ef65ef5b6bfeb6..d5c61ce62f6f08ee650fc4802843a81a8c38f00b 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * This file is part of DisOrder
- * Copyright (C) 2004, 2005, 2006, 2007 Richard Kettlewell
+ * Copyright (C) 2004, 2005, 2006, 2007, 2009 Richard Kettlewell
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -63,6 +63,7 @@ socklen_t find_server(struct config *c,
     strcpy(su.sun_path, name);
     sa = (struct sockaddr *)&su;
     len = sizeof su;
+    xfree(name);
   }
   *sap = xmalloc_noptr(len);
   memcpy(*sap, sa, len);
index 67f8a530f156d6ac0da7d821f48551b164c97c45..d9023a985bd3074d1da1d571ca5f0969a19a821f 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * This file is part of DisOrder.
- * Copyright (C) 2004-2008 Richard Kettlewell
+ * Copyright (C) 2004-2009 Richard Kettlewell
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -141,10 +141,12 @@ static int check_response(disorder_client *c, char **rp) {
   else if(rc / 100 == 2) {
     if(rp)
       *rp = (rc % 10 == 9) ? 0 : xstrdup(r + 4);
+    xfree(r);
     return 0;
   } else {
     if(c->verbose)
       disorder_error(0, "from %s: %s", c->ident, utf82mb(r));
+    xfree(r);
     return rc;
   }
 }
@@ -200,6 +202,7 @@ static int disorder_simple_v(disorder_client *c,
     D(("command: %s", d.vec));
     if(fputs(d.vec, c->fpout) < 0)
       goto write_error;
+    xfree(d.vec);
     if(body) {
       if(nbody < 0)
         for(nbody = 0; body[nbody]; ++nbody)
@@ -291,7 +294,9 @@ static int dequote(int rc, char **rp) {
 
   if(!rc) {
     if((rr = split(*rp, 0, SPLIT_QUOTES, 0, 0)) && *rr) {
+      xfree(*rp);
       *rp = *rr;
+      xfree(rr);
       return 0;
     }
     disorder_error(0, "invalid reply: %s", *rp);
@@ -316,13 +321,13 @@ int disorder_connect_generic(struct config *conf,
                              const char *username,
                              const char *password,
                              const char *cookie) {
-  int fd = -1, fd2 = -1, nrvec, rc;
-  unsigned char *nonce;
+  int fd = -1, fd2 = -1, nrvec = 0, rc;
+  unsigned char *nonce = NULL;
   size_t nl;
-  const char *res;
-  char *r, **rvec;
+  char *res = NULL;
+  char *r = NULL, **rvec = NULL;
   const char *protocol, *algorithm, *challenge;
-  struct sockaddr *sa;
+  struct sockaddr *sa = NULL;
   socklen_t salen;
 
   if((salen = find_server(conf, &sa, &c->ident)) == (socklen_t)-1)
@@ -364,14 +369,14 @@ int disorder_connect_generic(struct config *conf,
     disorder_error(0, "cannot parse server greeting %s", r);
     goto error;
   }
-  protocol = *rvec++;
+  protocol = rvec[0];
   if(strcmp(protocol, "2")) {
     c->last = "unknown protocol version";
     disorder_error(0, "unknown protocol version: %s", protocol);
     goto error;
   }
-  algorithm = *rvec++;
-  challenge = *rvec++;
+  algorithm = rvec[1];
+  challenge = rvec[2];
   if(!(nonce = unhex(challenge, &nl)))
     goto error;
   if(cookie) {
@@ -391,6 +396,11 @@ int disorder_connect_generic(struct config *conf,
   if((rc = disorder_simple(c, 0, "user", username, res, (char *)0)))
     goto error_rc;
   c->user = xstrdup(username);
+  xfree(res);
+  free_strings(nrvec, rvec);
+  xfree(nonce);
+  xfree(sa);
+  xfree(r);
   return 0;
 error:
   rc = -1;
@@ -505,7 +515,9 @@ int disorder_close(disorder_client *c) {
     }
     c->fpout = 0;
   }
+  xfree(c->ident);
   c->ident = 0;
+  xfree(c->user);
   c->user = 0;
   return 0;
 }
index ac0c0d813f8e070acc2df1e258403234ea53df9b..3227f12f38fd3f5af07de8b255d424257613d3dd 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * This file is part of DisOrder
- * Copyright (C) 2004, 2007, 2008 Richard Kettlewell
+ * Copyright (C) 2004, 2007-2009 Richard Kettlewell
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -26,6 +26,7 @@
 
 #include "printf.h"
 #include "sink.h"
+#include "mem.h"
 
 /** @brief vfprintf() workalike that always accepts UTF-8
  * @param fp Stream to write to
  * @return -1 on error or bytes written on success
  */
 int byte_vfprintf(FILE *fp, const char *fmt, va_list ap) {
-  return byte_vsinkprintf(sink_stdio(0, fp), fmt, ap);
+  struct sink *s = sink_stdio(0, fp);
+  int rc = byte_vsinkprintf(s, fmt, ap);
+  xfree(s);
+  return rc;
 }
 
 /** @brief fprintf() workalike that always accepts UTF-8
index 65a84e024490b7c4ad3a613f072264eff95a1978..fa8f9e388df6d0c90f157eacc94c7e14d6db4cd7 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * This file is part of DisOrder.
- * Copyright (C) 2004, 2007, 2008 Richard Kettlewell
+ * Copyright (C) 2004, 2007-2009 Richard Kettlewell
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
index 5bd9b9754b2585ab6be6630416fdcf1f9bf50a23..da84784e39ab0e45be79cff6b85a2a86b325cee1 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * This file is part of DisOrder.
- * Copyright (C) 2004, 2005, 2007, 2008 Richard Kettlewell
+ * Copyright (C) 2004, 2005, 2007-2009 Richard Kettlewell
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by