commit c25f564046633197ea2b34ce7813e9e11a8e0b4e
parent 7b34b878ea194e6fa085be08ba9afb6801f9cda2
Author: Antoine Amarilli <a3nm@a3nm.net>
Date: Tue, 10 Mar 2015 20:06:24 +0100
fix memory errors when tracking with -u
Thanks to elarnon for reporting this
Diffstat:
irctk.c | | | 49 | +++++++++++++++++++++++++++++++++++++++++-------- |
1 file changed, 41 insertions(+), 8 deletions(-)
diff --git a/irctk.c b/irctk.c
@@ -1284,7 +1284,7 @@ void cmd_msg(char *full_line, char *target, char *line)
debug("cmd_msg %s \"%s\" (%p), last_in %s\n", target, line, line, args.last_chan_in);
if (args.track != NO) {
- char lline[MAX_LINE_LEN + MAX_NICK_LEN + 2];
+ char real_nick[MAX_NICK_LEN];
int seppos = -1;
int i;
for (i=0; i<strlen(line); i++) {
@@ -1302,14 +1302,47 @@ void cmd_msg(char *full_line, char *target, char *line)
pthread_mutex_lock(&fifos.ctrl.mutex);
int c = scan_collection(&fifos, (const char **) fifos.surface_name, line);
if (c >= 0)
- strncpy(lline, fifos.chans[c], MAX_NICK_LEN-1);
+ strncpy(real_nick, fifos.chans[c], MAX_NICK_LEN);
pthread_mutex_unlock(&fifos.ctrl.mutex);
line[seppos] = sep;
if (c >= 0) {
- strncpy(lline, line + seppos, MAX_LINE_LEN-1);
- free(full_line);
- line = lline;
- full_line = lline;
+ // replace surface name by real name
+ // we must move temporarily the line out of the way
+ debug("i will allocate %d bytes and copy %s\n", (strlen(line) + 1) * sizeof(char), line);
+ char* old_line = malloc((strlen(line) + 1) * sizeof(char));
+ assert(old_line);
+ strcpy(old_line, line);
+
+ // the size of the line needs to change
+ // first, count the part before line starts
+ // then count the length of the line plus \0 byte
+ // then adjust: the size of the real_nick
+ // minus seppos, the size of the old nick
+ int new_size = (line-full_line) + strlen(line) + 1 + (strlen(real_nick) - seppos);
+ int target_offset = target ? (target - full_line) : (-1);
+ int line_offset = line - full_line;
+ debug("-1 my full line is: %s\n", full_line);
+ char *nfull_line = realloc(full_line, new_size*sizeof(char));
+ assert(nfull_line);
+ full_line = nfull_line;
+ debug("resized to: %d\n", new_size*sizeof(char));
+ debug("0 my full line is: %s\n", full_line);
+ if (target)
+ target = full_line + target_offset;
+ line = full_line + line_offset;
+
+ // now do the copy
+ debug("1 my full line is: %s\n", full_line);
+ strcpy(line, real_nick);
+ debug("2 my full line is: %s\n", full_line);
+ strcat(line, old_line + seppos);
+ debug("3 my full line is: %s\n", full_line);
+
+ free(old_line);
+
+ debug("now my full line is: %s\n", full_line);
+ debug("now my target is: %s\n", target);
+ debug("now my line is: %s\n", line);
}
}
}
@@ -1826,15 +1859,15 @@ static void* fifo_in_thread (void *arg) {
if (i)
line[i] = 0;
debug("[thread_in] calling with line target msg %p %p %p\n", line, target, msg);
- cmd_msg(line, target, msg);
strncpy(args.last_chans_out, target, MAX_CHANS_LEN-1);
+ cmd_msg(line, target, msg);
} else {
/* No target specified, we attempt the default */
debug("[thread_in] calling with line target msg %p %p %p\n", line, NULL, line);
cmd_msg(line, NULL, line);
}
- // reallocate line, the popper will free the line
+ // the popper will free the line
line = NULL;
}
// sentinel