Fix failed `i0 != NONE' assertion and fix the create/delete/empty out checking

This is bug 25970.
This commit is contained in:
Andreas Gruenbacher 2009-03-28 23:48:24 +01:00
parent f6f309f44f
commit d85ba027a7
6 changed files with 111 additions and 32 deletions

View File

@ -33,6 +33,13 @@
(ok_to_reverse): Use vsay() here instead of fprintf() to ensure
flushing.
* pch.c (there_is_another_patch): Move the "patch would create" test
here from intuit_diff_type() after asking for the filename to patch.
* inp.c (get_input_file): No need to report when the file is missing;
this is done in there_is_another_patch() already.
* patch.c (main): No need to report when a patch attempts to create
an exiting file, either.
2009-03-25 Andreas Gruenbacher <agruen@suse.de>
* patch.c (main): Avoid replacing files when nothing has changed.

View File

@ -91,12 +91,12 @@ MISC = AUTHORS COPYING ChangeLog INSTALL Makefile.in NEWS README VERSION \
mkinstalldirs patch.man stdbool_.h tests/test-lib.sh \
update-version.sh
TESTS = tests/backup-prefix-suffix tests/corrupt-reject-files \
tests/crlf-handling tests/global-reject-files \
tests/munged-context-format tests/need-filename \
tests/no-newline-triggers-assert tests/preserve-c-function-names \
tests/preserve-mode-and-timestamp tests/reject-format \
tests/remember-backup-files tests/remember-reject-files \
tests/unmodified-files
tests/create-delete tests/crlf-handling \
tests/global-reject-files tests/munged-context-format \
tests/need-filename tests/no-newline-triggers-assert \
tests/preserve-c-function-names tests/preserve-mode-and-timestamp \
tests/reject-format tests/remember-backup-files \
tests/remember-reject-files tests/unmodified-files
DISTFILES = $(MISC) $(SRCS) $(HDRS) $(M4FILES) \
pc/chdirsaf.c pc/djgpp/config.sed pc/djgpp/configure.bat \
pc/djgpp/configure.sed pc/djgpp/README

4
inp.c
View File

@ -145,7 +145,7 @@ get_input_file (char const *filename, char const *outname)
char *getbuf;
if (inerrno == -1)
inerrno = stat (inname, &instat) == 0 ? 0 : errno;
inerrno = stat (filename, &instat) == 0 ? 0 : errno;
/* Perhaps look for RCS or SCCS versions. */
if (patch_get
@ -196,7 +196,7 @@ get_input_file (char const *filename, char const *outname)
if (diffbuf)
free (diffbuf);
} else if (inerrno && !pch_says_nonexistent (reverse))
} else if (inerrno && inerrno != ENOENT)
{
errno = inerrno;
pfatal ("Can't find file %s", quotearg (filename));

View File

@ -301,11 +301,6 @@ main (int argc, char **argv)
else if (!where
|| (where == 1 && pch_says_nonexistent (reverse) == 2
&& instat.st_size)) {
if (where)
say ("Patch attempted to create file %s, which already exists.\n",
quotearg (inname));
abort_hunk (! failed, reverse);
failed++;
if (verbosity != SILENT)

35
pch.c
View File

@ -198,6 +198,8 @@ grow_hunkmax (void)
bool
there_is_another_patch (bool need_header)
{
bool is_empty;
if (p_base != 0 && p_base >= p_filesize) {
if (verbosity == VERBOSE)
say ("done\n");
@ -285,6 +287,20 @@ there_is_another_patch (bool need_header)
}
}
}
is_empty = inerrno || instat.st_size == 0;
if ((! is_empty) < p_says_nonexistent[reverse ^ is_empty])
{
reverse ^= ok_to_reverse
("The next patch%s would %s the file %s,\nwhich %s!",
reverse ? ", when reversed," : "",
(inerrno ? "delete"
: instat.st_size == 0 ? "empty out"
: "create"),
quotearg (inname),
(inerrno ? "does not exist"
: instat.st_size == 0 ? "is already empty"
: "already exists"));
}
return true;
}
@ -600,8 +616,6 @@ intuit_diff_type (bool need_header)
if (! posixly_correct)
{
bool is_empty;
i = best_name (p_name, stat_errno);
if (i == NONE && patch_get)
@ -644,23 +658,6 @@ intuit_diff_type (bool need_header)
}
}
is_empty = i == NONE || st[i].st_size == 0;
if ((! is_empty) < p_says_nonexistent[reverse ^ is_empty])
{
assert (i0 != NONE);
reverse ^=
ok_to_reverse
("The next patch%s would %s the file %s,\nwhich %s!",
reverse ? ", when reversed," : "",
(i == NONE ? "delete"
: st[i].st_size == 0 ? "empty out"
: "create"),
quotearg (p_name[i == NONE || st[i].st_size == 0 ? i0 : i]),
(i == NONE ? "does not exist"
: st[i].st_size == 0 ? "is already empty"
: "already exists"));
}
if (i == NONE && p_says_nonexistent[reverse])
{
int newdirs[3];

80
tests/create-delete Normal file
View File

@ -0,0 +1,80 @@
# Copyright (C) 2009 Free Software Foundation, Inc.
#
# Copying and distribution of this file, with or without modification,
# in any medium, are permitted without royalty provided the copyright
# notice and this notice are preserved.
. $srcdir/test-lib.sh
require_cat
require_diff
use_local_patch
use_tmpdir
# ==============================================================
# Bug #25970: intuit_diff_type: Assertion `i0 != NONE' failed.
cat > f.diff <<EOF
--- f
+++ f
@@ -1 +0,0 @@
-one
EOF
echo one > f
check 'patch -f -p1 < f.diff || echo "Status: $?"' <<EOF
can't find file to patch at input line 3
Perhaps you used the wrong -p or --strip option?
The text leading up to this was:
--------------------------
|--- f
|+++ f
--------------------------
No file to patch. Skipping patch.
1 out of 1 hunk ignored
Status: 1
EOF
# ==============================================================
echo one > f
diff -u /dev/null f > f.diff
check 'patch -f < f.diff || echo "Status: $?"' <<EOF
The next patch would create the file f,
which already exists! Applying it anyway.
patching file f
Hunk #1 FAILED at 1.
1 out of 1 hunk FAILED -- saving rejects to file f.rej
Status: 1
EOF
# ==============================================================
diff -u f /dev/null > f.diff
rm f
check 'patch -f f < f.diff || echo "Status: $?"' <<EOF
The next patch would delete the file f,
which does not exist! Applying it anyway.
patching file f
Hunk #1 FAILED at 1.
1 out of 1 hunk FAILED -- saving rejects to file f.rej
Status: 1
EOF
# ==============================================================
rm -f f.orig
echo f > f.orig
rm -f f
touch f
diff -u f.orig f > f.diff
check 'patch -f < f.diff || echo "Status: $?"' <<EOF
The next patch would empty out the file f,
which is already empty! Applying it anyway.
patching file f
Hunk #1 FAILED at 1.
1 out of 1 hunk FAILED -- saving rejects to file f.rej
Status: 1
EOF