]> git.neil.brown.name Git - wiggle.git/commitdiff
Make some fatal errors more helpful.
authorNeilBrown <neilb@suse.de>
Tue, 20 Aug 2013 05:29:53 +0000 (15:29 +1000)
committerNeilBrown <neilb@suse.de>
Tue, 20 Aug 2013 05:29:53 +0000 (15:29 +1000)
Complain sensibly if something is a directory, and
mention that read or malloc failed if it did.

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

diff --git a/load.c b/load.c
index 2dfa846beadd0ee891db712ab8e12d643bd901a7..108b6449c861ffe1c3472d03551e3e0f5150cfa6 100644 (file)
--- a/load.c
+++ b/load.c
@@ -54,7 +54,7 @@ static void join_streams(struct stream list[], int cnt)
 
        c = realloc(list[0].body, len+1);
        if (c == NULL)
-               die();
+               die("memory allocation");
 
        list[0].body = c;
        c  += list[0].len;
@@ -77,7 +77,7 @@ static struct stream load_regular(int fd)
        s.len = stb.st_size;
        s.body = xmalloc(s.len+1);
        if (read(fd, s.body, s.len) != s.len)
-               die();
+               die("file read");
 
        s.body[s.len] = 0;
        return s;
@@ -93,7 +93,7 @@ static struct stream load_other(int fd)
                list[i].body = xmalloc(8192);
                list[i].len = read(fd, list[i].body, 8192);
                if (list[i].len < 0)
-                       die();
+                       die("file read");
                if (list[i].len == 0)
                        break;
                i++;
@@ -114,7 +114,7 @@ struct stream load_segment(FILE *f,
        s.body = xmalloc(s.len+1);
        fseek(f, start, 0);
        if (fread(s.body, 1, s.len, f) != (size_t)s.len)
-               die();
+               die("file read");
        /* ensure string is 'nul' terminated - for sscanf */
        s.body[s.len] = 0;
        return s;
@@ -148,7 +148,7 @@ struct stream load_file(char *name)
                        if (fd < 0)
                                return s;
                }
-
+               check_dir(name, fd);
                if (fstat(fd, &stb) == 0) {
 
                        if (S_ISREG(stb.st_mode))
index f93b52288511ab8d07a21cbc7ecdbbccc672524c..bca7533a8ab823be94b18e711a39ea6b8e1e68d4 100644 (file)
--- a/vpatch.c
+++ b/vpatch.c
@@ -3059,6 +3059,7 @@ int vpatch(int argc, char *argv[], int patch, int strip,
                        fprintf(stderr, "%s: cannot open %s\n", Cmd, argv[0]);
                        exit(1);
                }
+               check_dir(argv[0], fileno(f));
                if (patch) {
                        pl = parse_patch(f, NULL, &num_patches);
                        if (set_prefix(pl, num_patches, strip) == 0) {
@@ -3080,6 +3081,7 @@ int vpatch(int argc, char *argv[], int patch, int strip,
                break;
        case 2: /* an orig and a diff/.ref */
                f = fopen(argv[1], "r");
+               check_dir(argv[1], fileno(f));
                if (!f) {
                        fprintf(stderr, "%s: cannot open %s\n", Cmd, argv[0]);
                        exit(1);
index 39907a93edc0d79387d2d6dbd53712c47b621877..ad89929e087fc66651eb4db49dbb48fd1f74cdf2 100644 (file)
--- a/wiggle.c
+++ b/wiggle.c
 #include       <stdlib.h>
 #include       <stdio.h>
 #include       <ctype.h>
+#include       <sys/stat.h>
 
 char *Cmd = "wiggle";
 int do_trace = 0;
 
-void die()
+void die(char *reason)
 {
-       fprintf(stderr, "%s: fatal error\n", Cmd);
-       abort();
+       fprintf(stderr, "%s: fatal error: %s failure\n", Cmd, reason);
        exit(3);
 }
 
+void check_dir(char *name, int fd)
+{
+       struct stat st;
+       if (fstat(fd, &st) != 0) {
+               fprintf(stderr, "%s: fatal: %s is strange\n", Cmd, name);
+               exit(3);
+       }
+       if (S_ISDIR(st.st_mode)) {
+               fprintf(stderr, "%s: %s is a directory\n", Cmd, name);
+               exit(3);
+       }
+}
+
 void *xmalloc(int size)
 {
        void *rv = malloc(size);
@@ -612,6 +625,7 @@ static int multi_merge(int argc, char *argv[], int obj, int blanks,
                        Cmd, filename);
                return 2;
        }
+       check_dir(filename, fileno(f));
        pl = parse_patch(f, NULL, &num_patches);
        fclose(f);
        if (set_prefix(pl, num_patches, strip) == 0) {
index 1a7afb9157f2278ee122d14cc5c3edb8c33b1be9..87f4c4d2e814874d154d7803f819496c446568e3 100644 (file)
--- a/wiggle.h
+++ b/wiggle.h
@@ -177,7 +177,8 @@ extern struct ci make_merger(struct file a, struct file b, struct file c,
                             struct csl *c1, struct csl *c2, int words,
                             int ignore_already, int show_wiggles);
 
-extern void die(void);
+extern void die(char *reason);
+extern void check_dir(char *name, int fd);
 extern void *xmalloc(int len);
 extern int do_trace;