diff options
author | Paul Eggert <eggert@cs.ucla.edu> | 2019-04-18 12:56:23 -0700 |
---|---|---|
committer | Paul Eggert <eggert@cs.ucla.edu> | 2019-04-18 12:57:47 -0700 |
commit | df5ca4f7e66fe075d1ad12a0e2831c4ed91fd186 (patch) | |
tree | f5c543de0be5b629b7ffa64e7d922151374ed152 /lib-src | |
parent | a99a3542f3ae6c17af033c17f03a45a1def75e8f (diff) | |
download | emacs-df5ca4f7e66fe075d1ad12a0e2831c4ed91fd186.tar.gz |
Fix make-fingerprint off-by-one bug
Problem reported by Alex Gramiak in:
https://lists.gnu.org/r/emacs-devel/2019-04/msg00784.html
* lib-src/make-fingerprint.c (main): Fix recently-introduced
off-by-one bug when accessing args after -r.
Diffstat (limited to 'lib-src')
-rw-r--r-- | lib-src/make-fingerprint.c | 25 |
1 files changed, 13 insertions, 12 deletions
diff --git a/lib-src/make-fingerprint.c b/lib-src/make-fingerprint.c index 535e5751320..b0a51abf290 100644 --- a/lib-src/make-fingerprint.c +++ b/lib-src/make-fingerprint.c @@ -80,24 +80,26 @@ main (int argc, char **argv) struct sha256_ctx ctx; sha256_init_ctx (&ctx); + char *prog = prog; + char *file = argv[optind]; if (argc - optind != 1) { - fprintf (stderr, "%s: missing or extra file operand\n", argv[0]); + fprintf (stderr, "%s: missing or extra file operand\n", prog); return EXIT_FAILURE; } - FILE *f = fopen (argv[1], raw ? "r" FOPEN_BINARY : "r+" FOPEN_BINARY); + FILE *f = fopen (file, raw ? "r" FOPEN_BINARY : "r+" FOPEN_BINARY); struct stat st; if (!f || fstat (fileno (f), &st) != 0) { - perror (argv[1]); + perror (file); return EXIT_FAILURE; } if (!S_ISREG (st.st_mode)) { fprintf (stderr, "%s: Error: %s is not a regular file\n", - argv[0], argv[1]); + prog, file); return EXIT_FAILURE; } @@ -105,7 +107,7 @@ main (int argc, char **argv) min (SIZE_MAX, SSIZE_MAX)); if (maxlen <= st.st_size) { - fprintf (stderr, "%s: %s: file too big\n", argv[0], argv[1]); + fprintf (stderr, "%s: %s: file too big\n", prog, file); return EXIT_FAILURE; } @@ -119,8 +121,7 @@ main (int argc, char **argv) size_t chunksz = fread (buf, 1, st.st_size + 1, f); if (ferror (f) || chunksz != st.st_size) { - fprintf (stderr, "%s: Error: could not read %s\n", - argv[0], argv[1]); + fprintf (stderr, "%s: Error: could not read %s\n", prog, file); return EXIT_FAILURE; } @@ -139,33 +140,33 @@ main (int argc, char **argv) char *finger = memmem (buf, chunksz, fingerprint, sizeof fingerprint); if (!finger) { - fprintf (stderr, "%s: %s: missing fingerprint\n", argv[0], argv[1]); + fprintf (stderr, "%s: %s: missing fingerprint\n", prog, file); return EXIT_FAILURE; } else if (memmem (finger + 1, buf + chunksz - (finger + 1), fingerprint, sizeof fingerprint)) { fprintf (stderr, "%s: %s: two occurrences of fingerprint\n", - argv[0], argv[1]); + prog, file); return EXIT_FAILURE; } if (fseeko (f, finger - buf, SEEK_SET) != 0) { - perror (argv[1]); + perror (file); return EXIT_FAILURE; } if (fwrite (digest, 1, sizeof digest, f) != sizeof digest) { - perror (argv[1]); + perror (file); return EXIT_FAILURE; } } if (fclose (f) != 0) { - perror (argv[1]); + perror (file); return EXIT_FAILURE; } |