From f74f4f32805a96d5079b90455264a160d91be3af Mon Sep 17 00:00:00 2001 Message-Id: From: Mark Wooding Date: Wed, 25 Nov 2009 11:17:45 +0000 Subject: [PATCH] More memory hygeine Organization: Straylight/Edgeware From: Richard Kettlewell --- clients/disorder.c | 16 ++++++----- lib/Makefile.am | 2 +- lib/asprintf.c | 2 +- lib/authhash.c | 8 +++--- lib/authhash.h | 6 ++--- lib/charset.h | 12 ++++++++- lib/charsetf.c | 66 +++++++++++++++++++++++++++++++++++++++++++++ lib/client-common.c | 3 ++- lib/client.c | 30 ++++++++++++++------- lib/fprintf.c | 8 ++++-- lib/vector.c | 2 +- lib/vector.h | 2 +- 12 files changed, 126 insertions(+), 31 deletions(-) create mode 100644 lib/charsetf.c diff --git a/clients/disorder.c b/clients/disorder.c index 298494a..02f6582 100644 --- a/clients/disorder.c +++ b/clients/disorder.c @@ -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) { diff --git a/lib/Makefile.am b/lib/Makefile.am index 3c1e2f1..ec674f6 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -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 \ diff --git a/lib/asprintf.c b/lib/asprintf.c index ddfc12e..fd9fa6c 100644 --- a/lib/asprintf.c +++ b/lib/asprintf.c @@ -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 diff --git a/lib/authhash.c b/lib/authhash.c index 8cc96f9..c06c233 100644 --- a/lib/authhash.c +++ b/lib/authhash.c @@ -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; diff --git a/lib/authhash.h b/lib/authhash.h index 8604a13..7b73d67 100644 --- a/lib/authhash.h +++ b/lib/authhash.h @@ -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 */ diff --git a/lib/charset.h b/lib/charset.h index be2b05a..e3c3a4e 100644 --- a/lib/charset.h +++ b/lib/charset.h @@ -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 index 0000000..cb27974 --- /dev/null +++ b/lib/charsetf.c @@ -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 . + */ +/** @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: +*/ diff --git a/lib/client-common.c b/lib/client-common.c index c55da89..d5c61ce 100644 --- a/lib/client-common.c +++ b/lib/client-common.c @@ -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); diff --git a/lib/client.c b/lib/client.c index 67f8a53..d9023a9 100644 --- a/lib/client.c +++ b/lib/client.c @@ -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; } diff --git a/lib/fprintf.c b/lib/fprintf.c index ac0c0d8..3227f12 100644 --- a/lib/fprintf.c +++ b/lib/fprintf.c @@ -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 @@ -34,7 +35,10 @@ * @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 diff --git a/lib/vector.c b/lib/vector.c index 65a84e0..fa8f9e3 100644 --- a/lib/vector.c +++ b/lib/vector.c @@ -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 diff --git a/lib/vector.h b/lib/vector.h index 5bd9b97..da84784 100644 --- a/lib/vector.h +++ b/lib/vector.h @@ -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 -- [mdw]