Commit df5ca4f7 authored by Paul Eggert's avatar Paul Eggert

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.
parent a99a3542
Pipeline #1338 failed with stage
in 53 minutes and 31 seconds
...@@ -80,24 +80,26 @@ main (int argc, char **argv) ...@@ -80,24 +80,26 @@ main (int argc, char **argv)
struct sha256_ctx ctx; struct sha256_ctx ctx;
sha256_init_ctx (&ctx); sha256_init_ctx (&ctx);
char *prog = prog;
char *file = argv[optind];
if (argc - optind != 1) 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; 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; struct stat st;
if (!f || fstat (fileno (f), &st) != 0) if (!f || fstat (fileno (f), &st) != 0)
{ {
perror (argv[1]); perror (file);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if (!S_ISREG (st.st_mode)) if (!S_ISREG (st.st_mode))
{ {
fprintf (stderr, "%s: Error: %s is not a regular file\n", fprintf (stderr, "%s: Error: %s is not a regular file\n",
argv[0], argv[1]); prog, file);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
...@@ -105,7 +107,7 @@ main (int argc, char **argv) ...@@ -105,7 +107,7 @@ main (int argc, char **argv)
min (SIZE_MAX, SSIZE_MAX)); min (SIZE_MAX, SSIZE_MAX));
if (maxlen <= st.st_size) 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; return EXIT_FAILURE;
} }
...@@ -119,8 +121,7 @@ main (int argc, char **argv) ...@@ -119,8 +121,7 @@ main (int argc, char **argv)
size_t chunksz = fread (buf, 1, st.st_size + 1, f); size_t chunksz = fread (buf, 1, st.st_size + 1, f);
if (ferror (f) || chunksz != st.st_size) if (ferror (f) || chunksz != st.st_size)
{ {
fprintf (stderr, "%s: Error: could not read %s\n", fprintf (stderr, "%s: Error: could not read %s\n", prog, file);
argv[0], argv[1]);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
...@@ -139,33 +140,33 @@ main (int argc, char **argv) ...@@ -139,33 +140,33 @@ main (int argc, char **argv)
char *finger = memmem (buf, chunksz, fingerprint, sizeof fingerprint); char *finger = memmem (buf, chunksz, fingerprint, sizeof fingerprint);
if (!finger) 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; return EXIT_FAILURE;
} }
else if (memmem (finger + 1, buf + chunksz - (finger + 1), else if (memmem (finger + 1, buf + chunksz - (finger + 1),
fingerprint, sizeof fingerprint)) fingerprint, sizeof fingerprint))
{ {
fprintf (stderr, "%s: %s: two occurrences of fingerprint\n", fprintf (stderr, "%s: %s: two occurrences of fingerprint\n",
argv[0], argv[1]); prog, file);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if (fseeko (f, finger - buf, SEEK_SET) != 0) if (fseeko (f, finger - buf, SEEK_SET) != 0)
{ {
perror (argv[1]); perror (file);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if (fwrite (digest, 1, sizeof digest, f) != sizeof digest) if (fwrite (digest, 1, sizeof digest, f) != sizeof digest)
{ {
perror (argv[1]); perror (file);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
} }
if (fclose (f) != 0) if (fclose (f) != 0)
{ {
perror (argv[1]); perror (file);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment