From 141aa9e5798562fc8802ba0d8bd016d972b256a9 Mon Sep 17 00:00:00 2001 From: sommerfe Date: Tue, 9 Feb 1999 05:15:45 +0000 Subject: [PATCH] Fix PR2429: catch doubly-applied patches which create new files. --- usr.bin/patch/common.h | 4 +++- usr.bin/patch/inp.c | 9 +++++++-- usr.bin/patch/pch.c | 13 +++++++++---- usr.bin/patch/util.c | 9 ++++++--- 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/usr.bin/patch/common.h b/usr.bin/patch/common.h index b1487794b7ea..9b1cfaa371ce 100644 --- a/usr.bin/patch/common.h +++ b/usr.bin/patch/common.h @@ -1,4 +1,4 @@ -/* $NetBSD: common.h,v 1.8 1998/02/22 13:33:48 christos Exp $ */ +/* $NetBSD: common.h,v 1.9 1999/02/09 05:15:45 sommerfe Exp $ */ #define DEBUGGING @@ -110,6 +110,8 @@ EXT bool out_of_mem INIT(FALSE); /* ran out of memory in plan a */ EXT int filec INIT(0); /* how many file arguments? */ EXT char *filearg[MAXFILEC]; EXT bool ok_to_create_file INIT(FALSE); +EXT bool filename_is_dev_null INIT(FALSE); +EXT bool old_file_is_dev_null INIT(FALSE); EXT char *bestguess INIT(Nullch); /* guess at correct filename */ EXT char *outname INIT(Nullch); diff --git a/usr.bin/patch/inp.c b/usr.bin/patch/inp.c index 8b977dc2b4ba..2273b3b6e8d6 100644 --- a/usr.bin/patch/inp.c +++ b/usr.bin/patch/inp.c @@ -1,7 +1,7 @@ -/* $NetBSD: inp.c,v 1.5 1998/11/06 22:40:13 christos Exp $ */ +/* $NetBSD: inp.c,v 1.6 1999/02/09 05:15:45 sommerfe Exp $ */ #include #ifndef lint -__RCSID("$NetBSD: inp.c,v 1.5 1998/11/06 22:40:13 christos Exp $"); +__RCSID("$NetBSD: inp.c,v 1.6 1999/02/09 05:15:45 sommerfe Exp $"); #endif /* not lint */ #include "EXTERN.h" @@ -145,6 +145,11 @@ char *filename; fatal3("can't check out file %s from %s\n", filename, cs); } } + if (old_file_is_dev_null && ok_to_create_file && (filestat.st_size != 0)) { + fatal2("patch creates new file but existing file %s not empty\n", + filename); + } + filemode = filestat.st_mode; if (!S_ISREG(filemode)) fatal2("%s is not a normal file--can't patch\n", filename); diff --git a/usr.bin/patch/pch.c b/usr.bin/patch/pch.c index 24bdf23646b4..ebec9a0708ef 100644 --- a/usr.bin/patch/pch.c +++ b/usr.bin/patch/pch.c @@ -1,7 +1,7 @@ -/* $NetBSD: pch.c,v 1.6 1998/11/06 22:40:13 christos Exp $ */ +/* $NetBSD: pch.c,v 1.7 1999/02/09 05:15:45 sommerfe Exp $ */ #include #ifndef lint -__RCSID("$NetBSD: pch.c,v 1.6 1998/11/06 22:40:13 christos Exp $"); +__RCSID("$NetBSD: pch.c,v 1.7 1999/02/09 05:15:45 sommerfe Exp $"); #endif /* not lint */ #include "EXTERN.h" @@ -204,6 +204,7 @@ intuit_diff_type() bool no_filearg = (filearg[0] == Nullch); ok_to_create_file = FALSE; + old_file_is_dev_null = FALSE; Fseek(pfp, p_base, 0); p_input_line = p_bline - 1; for (;;) { @@ -307,8 +308,10 @@ intuit_diff_type() if (no_filearg) { if (indtmp != Nullch) indname = fetchname(indtmp, strippath, ok_to_create_file); - if (oldtmp != Nullch) + if (oldtmp != Nullch) { oldname = fetchname(oldtmp, strippath, ok_to_create_file); + old_file_is_dev_null = filename_is_dev_null; + } if (newtmp != Nullch) newname = fetchname(newtmp, strippath, ok_to_create_file); if (oldname && newname) { @@ -333,8 +336,10 @@ intuit_diff_type() else if (indtmp != Nullch) bestguess = fetchname(indtmp, strippath, TRUE); else { - if (oldtmp != Nullch) + if (oldtmp != Nullch) { oldname = fetchname(oldtmp, strippath, TRUE); + old_file_is_dev_null = filename_is_dev_null; + } if (newtmp != Nullch) newname = fetchname(newtmp, strippath, TRUE); if (oldname && newname) { diff --git a/usr.bin/patch/util.c b/usr.bin/patch/util.c index f51c59f93cd9..5c15ffcc3c78 100644 --- a/usr.bin/patch/util.c +++ b/usr.bin/patch/util.c @@ -1,7 +1,7 @@ -/* $NetBSD: util.c,v 1.6 1998/11/06 22:40:13 christos Exp $ */ +/* $NetBSD: util.c,v 1.7 1999/02/09 05:15:45 sommerfe Exp $ */ #include #ifndef lint -__RCSID("$NetBSD: util.c,v 1.6 1998/11/06 22:40:13 christos Exp $"); +__RCSID("$NetBSD: util.c,v 1.7 1999/02/09 05:15:45 sommerfe Exp $"); #endif /* not lint */ #include "EXTERN.h" @@ -455,8 +455,11 @@ int assume_exists; if (debug & 128) say4("fetchname %s %d %d\n",at,strip_leading,assume_exists); #endif - if (strnEQ(at, "/dev/null", 9)) /* so files can be created by diffing */ + filename_is_dev_null = FALSE; + if (strnEQ(at, "/dev/null", 9)) { /* so files can be created by diffing */ + filename_is_dev_null = TRUE; return Nullch; /* against /dev/null. */ + } name = fullname = t = savestr(at); /* Strip off up to `sleading' leading slashes and null terminate. */