Browse files

fix join trailing nul

strcat adds the nul be we are not giving it room here
  • Loading branch information...
1 parent 334002e commit 441f39b3c572de1eafeec3a44ecd591ad882df05 @tj committed Aug 17, 2012
Showing with 7 additions and 2 deletions.
  1. +5 −0 History.md
  2. +2 −2 src/watch.c
View
5 History.md
@@ -1,4 +1,9 @@
+0.2.1 / 2012-08-17
+==================
+
+ * fix join()
+
0.2.0 / 2012-06-08
==================
View
4 src/watch.c
@@ -17,7 +17,7 @@
* Command version.
*/
-#define VERSION "0.2.0"
+#define VERSION "0.2.1"
/*
* Default interval in milliseconds.
@@ -131,7 +131,7 @@ length(char **strs) {
char *
join(char **strs, int len, char *val) {
--len;
- char *buf = calloc(1, length(strs) + len * strlen(val));
+ char *buf = malloc(length(strs) + len * strlen(val) + 1);
char *str;
while ((str = *strs++)) {
strcat(buf, str);

6 comments on commit 441f39b

@stephenvisser

I'm not sure if you meant to reject my change... An alternative to changing malloc -> calloc would be to strcpy to specific locations (may actually be the preferred solution vs strcat)

@tj
Owner

strcat should be fine, I don't have 1.8 on any of my machines so I can't confirm but this should have been the issue, zero'd mem shouldn't matter

@stephenvisser

Fair enough -- I just mean that you somehow replaced calloc with malloc in the commit, so you didn't make any fixes.

@tj
Owner

well that's what I mean, as long as we have room for the trailing nul there should be no need for calloc

@stephenvisser
@tj
Owner

ohhh I see what you're saying now, I guess that makes sense since we don't add an initial null, fair enough I'll add that back

Please sign in to comment.