From: NeilBrown Date: Tue, 20 Aug 2013 05:29:53 +0000 (+1000) Subject: Make some fatal errors more helpful. X-Git-Tag: v1.0~30 X-Git-Url: http://git.neil.brown.name/?a=commitdiff_plain;h=97f2558129ab118fadce95b45a93b7f63a2cbcbd;p=wiggle.git Make some fatal errors more helpful. Complain sensibly if something is a directory, and mention that read or malloc failed if it did. Signed-off-by: NeilBrown --- diff --git a/load.c b/load.c index 2dfa846..108b644 100644 --- 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)) diff --git a/vpatch.c b/vpatch.c index f93b522..bca7533 100644 --- 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); diff --git a/wiggle.c b/wiggle.c index 39907a9..ad89929 100644 --- a/wiggle.c +++ b/wiggle.c @@ -89,17 +89,30 @@ #include #include #include +#include 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) { diff --git a/wiggle.h b/wiggle.h index 1a7afb9..87f4c4d 100644 --- 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;