irctk

libircclient binding for scripts
git clone https://a3nm.net/git/irctk/
Log | Files | Refs | README

commit b37eee2062fa957d092ed2f603d73da082c3f7f5
parent af23be5fbf336ebe8164668bdedbb48c1a224749
Author: Antoine Amarilli <a3nm@a3nm.net>
Date:   Mon, 25 Jun 2012 16:46:43 +0200

fix multiple memory management mistakes

Diffstat:
irctk.c | 23+++++++++++++++--------
1 file changed, 15 insertions(+), 8 deletions(-)

diff --git a/irctk.c b/irctk.c @@ -646,7 +646,7 @@ void push_one(fifo_set *s, char *fl, char *dest, char *l, int next_dep_fifoidx, int next_dep_lineidx, int *fifoidx, int *lineidx) { int c = index_of_chan(s, dest); - debug("push_one \"%s\" %d %d ioc %d", l, next_dep_fifoidx, next_dep_lineidx, c); + debug("PUSH_ONE \"%s\" (%p), nextdep %d %d ioc %d", l, l, next_dep_fifoidx, next_dep_lineidx, c); fifo *f = &(s->fifos[c]); if (s->was_pushed[c]) return; // already pushed @@ -679,7 +679,7 @@ void reset_chan_marks(fifo_set *s) { } void push_fifo_set(fifo_set *s, char *fl, char *dests, char *l) { - debug("push_fifo_set \"%s\"", fl); + debug("push_fifo_set full_line \"%s\" (%p) \"%s\" (%p)", fl, fl, l, l); int was_empty; pthread_mutex_lock(&s->ctrl.mutex); while (full_fifo_set(s)) @@ -939,9 +939,9 @@ int do_cmd_msg(irc_session_t *s, char* chan, char* line) void cmd_msg(char *full_line, char *target, char *line) { int i = 0; - char *msg[2*MAX_LEN+2]; + char *msg; - debug("cmd_msg %s %s, last_in %s", target, line, args.last_chan_in); + debug("cmd_msg %s \"%s\" (%p), last_in %s", target, line, line, args.last_chan_in); /* Manage the fact that target may be "" */ if (!target[0]) { @@ -955,11 +955,15 @@ void cmd_msg(char *full_line, char *target, char *line) if (args.show_nick_prefix && args.last_nick_in[0] && args.last_chan_in[0] == '#') { if (line[0] != '\n') { + msg = malloc((2*MAX_LEN + 2)* sizeof(char)); + assert(msg); msg[0] = 0; strncat((char*) msg, args.last_nick_in, MAX_LEN-1); strcat((char*) msg, ": "); strncat((char*) msg, line, MAX_LEN-1); - line = (char*) msg; + free(full_line); + full_line = msg; + line = msg; } else { args.last_nick_in[0] = 0; } @@ -977,7 +981,7 @@ void cmd_msg(char *full_line, char *target, char *line) //TODO2: ugly, could have generated the comma-separated string //like for last_chans_out! for (i = 0; i < args.n_channels; i++) - push_fifo_set(&fifos, full_line, args.channels[i], line); + push_fifo_set(&fifos, i?NULL:full_line, args.channels[i], line); /* TODO return value */ default: @@ -1289,8 +1293,8 @@ static void* fifo_in_thread (void *arg) { assert(line); while ((res = getline((char**) &line, (size_t*) &size, stdin)) != -1) { - debug("in_thread reads %s", line); - debug("i need to free %p", line); + debug("[thread_in] READS %s", line); + debug("someone will have to free %p", line); if (line[0] == '[' && args.destination_prefix) { int i=0; @@ -1308,10 +1312,12 @@ static void* fifo_in_thread (void *arg) { msg++; if (i) line[i] = 0; + debug("[thread_in] calling with line target msg %p %p %p", line, target, msg); cmd_msg(line, target, msg); strncpy(args.last_chans_out, target, MAX_LEN-1); } else { /* No target specified, we attempt the default */ + debug("[thread_in] calling with line target msg %p %p %p", line, "", line); cmd_msg(line, "", line); } @@ -1393,6 +1399,7 @@ int start (int max_wait) for (i = 0; i < n_results; i++) if (results[i].full_line) free(results[i].full_line); + free(results); debug("[main] I managed my lines"); if (cont && !args.ready) { info("Connection lost, reconnecting...");