]> git.neil.brown.name Git - wiggle.git/commitdiff
nul terminate stream read from a file.
authorNeil Brown <neilb@suse.de>
Mon, 22 Aug 2011 08:08:02 +0000 (18:08 +1000)
committerNeil Brown <neilb@suse.de>
Mon, 22 Aug 2011 08:08:02 +0000 (18:08 +1000)
Though we carry around the length of a string and so will not normally
need a nul terminator, we sometimes use sscanf which cannot take a
length and so needs nul termination.

So add a nul when loading from a file or stdin.

Signed-off-by: NeilBrown <neilb@suse.de>
extract.c
load.c
split.c
wiggle.h

index 92d50a33a5f7197a5be46590602be2373e12db03..ce40310e120a70674f361379b90920867d9b6786 100644 (file)
--- a/extract.c
+++ b/extract.c
@@ -118,15 +118,15 @@ int split_patch(struct stream f, struct stream *f1, struct stream *f2)
                                buf[0] = 0;
                                chunks++;
                                sprintf(buf+1, "%5d %5d %5d\n", chunks, a, acnt);
-                               memcpy(r1.body+r1.len, buf, 19);
-                               r1.len += 19;
+                               memcpy(r1.body+r1.len, buf, 20);
+                               r1.len += 20;
                        }
                        if (state==2 || state == 3) {
                                char buf[20];
                                buf[0] = 0;
                                sprintf(buf+1, "%5d %5d %5d\n", chunks, c, bcnt);
-                               memcpy(r2.body+r2.len, buf, 19);
-                               r2.len += 19;
+                               memcpy(r2.body+r2.len, buf, 20);
+                               r2.len += 20;
                        }
                        break;
                case 1:
diff --git a/load.c b/load.c
index 23e4e247f3b4904d6d6e42d5a10ff5bef30c0259..b544169f8f1a47e23202f188772972dc4a7bb54b 100644 (file)
--- a/load.c
+++ b/load.c
@@ -52,7 +52,7 @@ static void join_streams(struct stream list[], int cnt)
        for (i=0; i<cnt ; i++)
                len += list[i].len;
 
-       c = realloc(list[0].body, len);
+       c = realloc(list[0].body, len+1);
        if (c == NULL)
                die();
 
@@ -64,6 +64,7 @@ static void join_streams(struct stream list[], int cnt)
                c += list[i].len;
                list[i].len = 0;
        }
+       c[0] = 0;
 }
 
 static struct stream load_regular(int fd)
@@ -73,13 +74,14 @@ static struct stream load_regular(int fd)
        fstat(fd, &stb);
 
        s.len = stb.st_size;
-       s.body = malloc(s.len);
+       s.body = malloc(s.len+1);
        if (s.body) {
                if (read(fd, s.body, s.len) != s.len) {
                        free(s.body);
                        s.body = NULL;
                }
        } else die();
+       s.body[s.len] = 0;
        return s;
 }
 
diff --git a/split.c b/split.c
index 4dfd3f379a8e12534b88a38c260023dac7d0af4c..ae61408fb10fe4f5a9ce1c33de6283a4cf07bbe3 100644 (file)
--- a/split.c
+++ b/split.c
@@ -60,9 +60,9 @@ static int split_internal(char *start, char *end, int type, struct elmnt *list,
        while (start < end) {
                char *cp = start;
 
-               if (*cp == '\0' && cp+16 < end && cp[18] == '\n') {
+               if (*cp == '\0' && cp+19 < end && cp[18] == '\n') {
                        /* special word */
-                       cp += 19;
+                       cp += 20;
                } else switch(type) {
                case ByLine:
                        while (cp < end && *cp != '\n')
index 76d42134665c1d3ac03fb1b657c07a90310c85f1..73fe809c2c92e722f6184aa0eeabceb47e1b7497 100644 (file)
--- a/wiggle.h
+++ b/wiggle.h
@@ -49,6 +49,8 @@ static  inline int match(struct elmnt *a, struct elmnt *b)
 
 static inline int ends_line(struct elmnt e)
 {
+       if (e.len == 20 && e.start[0] == 0)
+               return 1;
        return e.len &&  e.start[e.len-1] == '\n';
 }