diff options
author | Otto Moerbeek <otto@cvs.openbsd.org> | 2003-08-01 20:30:50 +0000 |
---|---|---|
committer | Otto Moerbeek <otto@cvs.openbsd.org> | 2003-08-01 20:30:50 +0000 |
commit | 3a733e7b12e7cfd67fef00cb6a1cf6933cd35f5a (patch) | |
tree | 71d15095a05edd773743a87b9a9d8b8165c81bf1 /usr.bin | |
parent | 661359573c5f673aeeab727dbe5289b78d2637af (diff) |
- use stdbool.h instead of roll-your-own booleans
- fix some -Wall warnings
- fix asserts: in some cases remove them, in other cases they have
become Internal errors or detection of malformed patch files.
- fix some free() related code
ok millert@ tedu@
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/patch/backupfile.c | 6 | ||||
-rw-r--r-- | usr.bin/patch/common.h | 8 | ||||
-rw-r--r-- | usr.bin/patch/inp.c | 38 | ||||
-rw-r--r-- | usr.bin/patch/patch.c | 117 | ||||
-rw-r--r-- | usr.bin/patch/pch.c | 188 | ||||
-rw-r--r-- | usr.bin/patch/util.c | 8 |
6 files changed, 184 insertions, 181 deletions
diff --git a/usr.bin/patch/backupfile.c b/usr.bin/patch/backupfile.c index a332ada3a65..5863e9b1b14 100644 --- a/usr.bin/patch/backupfile.c +++ b/usr.bin/patch/backupfile.c @@ -1,4 +1,4 @@ -/* $OpenBSD: backupfile.c,v 1.16 2003/07/28 18:35:36 otto Exp $ */ +/* $OpenBSD: backupfile.c,v 1.17 2003/08/01 20:30:48 otto Exp $ */ /* * backupfile.c -- make Emacs style backup file names Copyright (C) 1990 Free @@ -17,7 +17,7 @@ */ #ifndef lint -static const char rcsid[] = "$OpenBSD: backupfile.c,v 1.16 2003/07/28 18:35:36 otto Exp $"; +static const char rcsid[] = "$OpenBSD: backupfile.c,v 1.17 2003/08/01 20:30:48 otto Exp $"; #endif /* not lint */ #include <ctype.h> @@ -168,7 +168,7 @@ static int argmatch(const char *arg, const char **optlist) { int i; /* Temporary index in OPTLIST. */ - int arglen; /* Length of ARG. */ + size_t arglen; /* Length of ARG. */ int matchind = -1; /* Index of first nonexact match. */ int ambiguous = 0; /* If nonzero, multiple nonexact match(es). */ diff --git a/usr.bin/patch/common.h b/usr.bin/patch/common.h index 0de1fc67a08..9e49c26ad18 100644 --- a/usr.bin/patch/common.h +++ b/usr.bin/patch/common.h @@ -1,12 +1,11 @@ -/* $OpenBSD: common.h,v 1.21 2003/07/31 14:10:21 otto Exp $ */ +/* $OpenBSD: common.h,v 1.22 2003/08/01 20:30:48 otto Exp $ */ + +#include <stdbool.h> #define DEBUGGING /* constants */ -#define TRUE 1 -#define FALSE 0 - #define MAXHUNKSIZE 100000 /* is this enough lines? */ #define INITHUNKMAX 125 /* initial dynamic allocation size */ #define MAXLINELEN 8192 @@ -32,7 +31,6 @@ /* typedefs */ -typedef char bool; typedef long LINENUM; /* must be signed */ /* globals */ diff --git a/usr.bin/patch/inp.c b/usr.bin/patch/inp.c index 77ad4b66cb3..7f5135ffd4f 100644 --- a/usr.bin/patch/inp.c +++ b/usr.bin/patch/inp.c @@ -1,7 +1,7 @@ -/* $OpenBSD: inp.c,v 1.21 2003/07/31 14:10:21 otto Exp $ */ +/* $OpenBSD: inp.c,v 1.22 2003/08/01 20:30:48 otto Exp $ */ #ifndef lint -static const char rcsid[] = "$OpenBSD: inp.c,v 1.21 2003/07/31 14:10:21 otto Exp $"; +static const char rcsid[] = "$OpenBSD: inp.c,v 1.22 2003/08/01 20:30:48 otto Exp $"; #endif /* not lint */ #include <sys/types.h> @@ -53,7 +53,7 @@ re_input(void) i_womp = NULL; i_ptr = NULL; } else { - using_plan_a = TRUE; /* maybe the next one is smaller */ + using_plan_a = true; /* maybe the next one is smaller */ close(tifd); tifd = -1; free(tibuf[0]); @@ -88,7 +88,7 @@ plan_a(const char *filename) struct stat filestat; if (filename == NULL || *filename == '\0') - return FALSE; + return false; statfailed = stat(filename, &filestat); if (statfailed && ok_to_create_file) { @@ -101,8 +101,8 @@ plan_a(const char *filename) * to normal patch behavior as possible */ if (check_only) - return TRUE; - makedirs(filename, TRUE); + return true; + makedirs(filename, true); close(creat(filename, 0666)); statfailed = stat(filename, &filestat); } @@ -178,21 +178,21 @@ plan_a(const char *filename) i_size = filestat.st_size; if (out_of_mem) { set_hunkmax(); /* make sure dynamic arrays are allocated */ - out_of_mem = FALSE; - return FALSE; /* force plan b because plan a bombed */ + out_of_mem = false; + return false; /* force plan b because plan a bombed */ } if (i_size > SIZE_MAX - 2) fatal("block too large to allocate"); i_womp = malloc((size_t)(i_size + 2)); if (i_womp == NULL) - return FALSE; + return false; if ((ifd = open(filename, O_RDONLY)) < 0) pfatal("can't open file %s", filename); if (read(ifd, i_womp, (size_t) i_size) != i_size) { close(ifd); /* probably means i_size > 15 or 16 bits worth */ free(i_womp); /* at this point it doesn't matter if i_womp was */ - return FALSE; /* undersized. */ + return false; /* undersized. */ } close(ifd); @@ -212,7 +212,7 @@ plan_a(const char *filename) if (i_ptr == NULL) { /* shucks, it was a near thing */ free(i_womp); - return FALSE; + return false; } /* now scan the buffer and build pointer array */ @@ -248,7 +248,7 @@ plan_a(const char *filename) say("Good. This file appears to be the %s version.\n", revision); } - return TRUE; /* plan a will work */ + return true; /* plan a will work */ } /* Keep (virtually) nothing in memory. */ @@ -260,7 +260,7 @@ plan_b(const char *filename) int i = 0, maxlen = 1; bool found_revision = (revision == NULL); - using_plan_a = FALSE; + using_plan_a = false; if ((ifp = fopen(filename, "r")) == NULL) pfatal("can't open file %s", filename); (void) unlink(TMPINNAME); @@ -268,7 +268,7 @@ plan_b(const char *filename) pfatal("can't open file %s", TMPINNAME); while (fgets(buf, sizeof buf, ifp) != NULL) { if (revision != NULL && !found_revision && rev_in_string(buf)) - found_revision = TRUE; + found_revision = true; if ((i = strlen(buf)) > maxlen) maxlen = i; /* find longest line */ } @@ -331,7 +331,7 @@ ifetch(LINENUM line, int whichbuf) if (line < 1 || line > input_lines) { if (warn_on_invalid_line) { say("No such line %ld in input file, ignoring\n", line); - warn_on_invalid_line = FALSE; + warn_on_invalid_line = false; } return NULL; } @@ -368,15 +368,15 @@ rev_in_string(const char *string) int patlen; if (revision == NULL) - return TRUE; + return true; patlen = strlen(revision); if (strnEQ(string, revision, patlen) && isspace(string[patlen])) - return TRUE; + return true; for (s = string; *s; s++) { if (isspace(*s) && strnEQ(s + 1, revision, patlen) && isspace(s[patlen + 1])) { - return TRUE; + return true; } } - return FALSE; + return false; } diff --git a/usr.bin/patch/patch.c b/usr.bin/patch/patch.c index 002c6328b8a..3d514c8f877 100644 --- a/usr.bin/patch/patch.c +++ b/usr.bin/patch/patch.c @@ -1,4 +1,4 @@ -/* $OpenBSD: patch.c,v 1.34 2003/07/31 21:07:35 millert Exp $ */ +/* $OpenBSD: patch.c,v 1.35 2003/08/01 20:30:48 otto Exp $ */ /* * patch - a program to apply diffs to original files @@ -27,14 +27,13 @@ */ #ifndef lint -static const char rcsid[] = "$OpenBSD: patch.c,v 1.34 2003/07/31 21:07:35 millert Exp $"; +static const char rcsid[] = "$OpenBSD: patch.c,v 1.35 2003/08/01 20:30:48 otto Exp $"; #endif /* not lint */ #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> -#include <assert.h> #include <ctype.h> #include <getopt.h> #include <limits.h> @@ -53,36 +52,36 @@ int filemode = 0644; char buf[MAXLINELEN]; /* general purpose buffer */ -bool using_plan_a = TRUE; /* try to keep everything in memory */ -bool out_of_mem = FALSE; /* ran out of memory in plan a */ +bool using_plan_a = true; /* try to keep everything in memory */ +bool out_of_mem = false; /* ran out of memory in plan a */ #define MAXFILEC 2 char *filearg[MAXFILEC]; -bool ok_to_create_file = FALSE; +bool ok_to_create_file = false; char *outname = NULL; char *origprae = NULL; char *TMPOUTNAME; char *TMPINNAME; char *TMPREJNAME; char *TMPPATNAME; -bool toutkeep = FALSE; -bool trejkeep = FALSE; +bool toutkeep = false; +bool trejkeep = false; bool warn_on_invalid_line; #ifdef DEBUGGING int debug = 0; #endif -bool force = FALSE; -bool batch = FALSE; -bool verbose = TRUE; -bool reverse = FALSE; -bool noreverse = FALSE; -bool skip_rest_of_patch = FALSE; +bool force = false; +bool batch = false; +bool verbose = true; +bool reverse = false; +bool noreverse = false; +bool skip_rest_of_patch = false; int strippath = 957; -bool canonicalize = FALSE; -bool check_only = FALSE; +bool canonicalize = false; +bool check_only = false; int diff_type = 0; char *revision = NULL; /* prerequisite revision, if any */ LINENUM input_lines = 0; /* how long is input file in lines */ @@ -101,11 +100,11 @@ static bool patch_match(LINENUM, LINENUM, LINENUM); static bool similar(const char *, const char *, int); static __dead void usage(void); -/* TRUE if -E was specified on command line. */ -static int remove_empty_files = FALSE; +/* true if -E was specified on command line. */ +static bool remove_empty_files = false; -/* TRUE if -R was specified on command line. */ -static int reverse_flag_specified = FALSE; +/* true if -R was specified on command line. */ +static bool reverse_flag_specified = false; /* buffer holding the name of the rejected patch file. */ static char rejname[NAME_MAX + 1]; @@ -129,7 +128,7 @@ static LINENUM last_offset = 0; static LINENUM maxfuzz = 2; /* patch using ifdef, ifndef, etc. */ -static bool do_defines = FALSE; +static bool do_defines = false; /* #ifdef xyzzy */ static char if_defined[128]; /* #ifndef xyzzy */ @@ -146,7 +145,7 @@ int main(int argc, char *argv[]) { int error = 0, hunk, failed, patch_seen = 0, i, fd; - LINENUM where, newwhere, fuzz, mymaxfuzz; + LINENUM where = 0, newwhere, fuzz, mymaxfuzz; const char *tmpdir; char *v; @@ -210,8 +209,8 @@ main(int argc, char *argv[]) reinitialize_almost_everything()) { /* for each patch in patch file */ - patch_seen = TRUE; - warn_on_invalid_line = TRUE; + patch_seen = true; + warn_on_invalid_line = true; if (outname == NULL) outname = savestr(filearg[0]); @@ -238,7 +237,7 @@ main(int argc, char *argv[]) /* apply each hunk of patch */ hunk = 0; failed = 0; - out_of_mem = FALSE; + out_of_mem = false; while (another_hunk()) { hunk++; fuzz = 0; @@ -270,7 +269,7 @@ main(int argc, char *argv[]) fatal("lost hunk on alloc error!\n"); reverse = !reverse; say("Ignoring previously applied (or reversed) patch.\n"); - skip_rest_of_patch = TRUE; + skip_rest_of_patch = true; } else if (batch) { if (verbose) say("%seversed (or previously applied) patch detected! %s -R.", @@ -283,7 +282,7 @@ main(int argc, char *argv[]) if (*buf == 'n') { ask("Apply anyway? [n] "); if (*buf != 'y') - skip_rest_of_patch = TRUE; + skip_rest_of_patch = true; where = 0; reverse = !reverse; if (!pch_swap()) @@ -341,7 +340,8 @@ main(int argc, char *argv[]) rejfp = NULL; continue; } - assert(hunk); + if (hunk == 0) + fatal("Internal error: hunk should not be 0\n"); /* finish spewing out the new file */ if (!skip_rest_of_patch) @@ -355,7 +355,7 @@ main(int argc, char *argv[]) if (!check_only) { if (move_file(TMPOUTNAME, outname) < 0) { - toutkeep = TRUE; + toutkeep = true; realout = TMPOUTNAME; chmod(TMPOUTNAME, filemode); } else @@ -391,7 +391,7 @@ main(int argc, char *argv[]) failed, hunk, rejname); } if (!check_only && move_file(TMPREJNAME, rejname) < 0) - trejkeep = TRUE; + trejkeep = true; } set_signals(1); } @@ -411,24 +411,22 @@ reinitialize_almost_everything(void) last_frozen_line = 0; filec = 0; - if (filearg[0] != NULL && !out_of_mem) { + if (!out_of_mem) { free(filearg[0]); filearg[0] = NULL; } - if (outname != NULL) { - free(outname); - outname = NULL; - } - last_offset = 0; + free(outname); + outname = NULL; + + last_offset = 0; diff_type = 0; - if (revision != NULL) { - free(revision); - revision = NULL; - } + free(revision); + revision = NULL; + reverse = reverse_flag_specified; - skip_rest_of_patch = FALSE; + skip_rest_of_patch = false; get_some_switches(); } @@ -500,14 +498,14 @@ get_some_switches(void) diff_type = CONTEXT_DIFF; break; case 'C': - check_only = TRUE; + check_only = true; break; case 'd': if (chdir(optarg) < 0) pfatal("can't cd to %s", optarg); break; case 'D': - do_defines = TRUE; + do_defines = true; if (!isalpha(*optarg) && *optarg != '_') fatal("argument to -D is not an identifier\n"); snprintf(if_defined, sizeof if_defined, @@ -521,10 +519,10 @@ get_some_switches(void) diff_type = ED_DIFF; break; case 'E': - remove_empty_files = TRUE; + remove_empty_files = true; break; case 'f': - force = TRUE; + force = true; break; case 'F': maxfuzz = atoi(optarg); @@ -535,13 +533,13 @@ get_some_switches(void) filearg[filec] = savestr(optarg); break; case 'l': - canonicalize = TRUE; + canonicalize = true; break; case 'n': diff_type = NORMAL_DIFF; break; case 'N': - noreverse = TRUE; + noreverse = true; break; case 'o': outname = savestr(optarg); @@ -555,14 +553,14 @@ get_some_switches(void) fatal("argument for -r is too long\n"); break; case 'R': - reverse = TRUE; - reverse_flag_specified = TRUE; + reverse = true; + reverse_flag_specified = true; break; case 's': - verbose = FALSE; + verbose = false; break; case 't': - batch = TRUE; + batch = true; break; case 'u': diff_type = UNI_DIFF; @@ -786,7 +784,8 @@ apply_hunk(LINENUM where) new++; } } else { - assert(pch_char(new) == ' '); + if (pch_char(new) != ' ') + fatal("Internal error: expected ' '\n"); old++; new++; if (do_defines && def_state != OUTSIDE) { @@ -898,16 +897,16 @@ patch_match(LINENUM base, LINENUM offset, LINENUM fuzz) for (iline = base + offset + fuzz; pline <= pat_lines; pline++, iline++) { ilineptr = ifetch(iline, offset >= 0); if (ilineptr == NULL) - return FALSE; + return false; plineptr = pfetch(pline); plinelen = pch_line_len(pline); if (canonicalize) { if (!similar(ilineptr, plineptr, plinelen)) - return FALSE; + return false; } else if (strnNE(ilineptr, plineptr, plinelen)) - return FALSE; + return false; } - return TRUE; + return true; } /* @@ -919,7 +918,7 @@ similar(const char *a, const char *b, int len) while (len) { if (isspace(*b)) { /* whitespace (or \n) to match? */ if (!isspace(*a)) /* no corresponding whitespace? */ - return FALSE; + return false; while (len && isspace(*b) && *b != '\n') b++, len--; /* skip pattern whitespace */ while (isspace(*a) && *a != '\n') @@ -927,10 +926,10 @@ similar(const char *a, const char *b, int len) if (*a == '\n' || *b == '\n') return (*a == *b); /* should end in sync */ } else if (*a++ != *b++) /* match non-whitespace chars */ - return FALSE; + return false; else len--; /* probably not necessary */ } - return TRUE; /* actually, this is not reached */ + return true; /* actually, this is not reached */ /* since there is always a \n */ } diff --git a/usr.bin/patch/pch.c b/usr.bin/patch/pch.c index 79fb6696dcf..5f5f976b3a1 100644 --- a/usr.bin/patch/pch.c +++ b/usr.bin/patch/pch.c @@ -1,13 +1,12 @@ -/* $OpenBSD: pch.c,v 1.27 2003/07/29 20:10:17 millert Exp $ */ +/* $OpenBSD: pch.c,v 1.28 2003/08/01 20:30:48 otto Exp $ */ #ifndef lint -static const char rcsid[] = "$OpenBSD: pch.c,v 1.27 2003/07/29 20:10:17 millert Exp $"; +static const char rcsid[] = "$OpenBSD: pch.c,v 1.28 2003/08/01 20:30:48 otto Exp $"; #endif /* not lint */ #include <sys/types.h> #include <sys/stat.h> -#include <assert.h> #include <ctype.h> #include <stdio.h> #include <stdlib.h> @@ -115,12 +114,8 @@ grow_hunkmax(void) { hunkmax *= 2; - /* - * Note that on most systems, only the p_line array ever gets fresh memory - * since p_len can move into p_line's old space, and p_char can move into - * p_len's old space. Not on PDP-11's however. But it doesn't matter. - */ - assert(p_line != NULL && p_len != NULL && p_char != NULL); + if (p_line == NULL || p_len == NULL || p_char == NULL) + fatal("Internal memory allocation error\n"); p_line = realloc(p_line, hunkmax * sizeof(char *)); p_len = realloc(p_len, hunkmax * sizeof(short)); @@ -130,8 +125,8 @@ grow_hunkmax(void) return; if (!using_plan_a) fatal("out of memory\n"); - out_of_mem = TRUE; /* whatever is null will be allocated again */ - /* from within plan_a(), of all places */ + out_of_mem = true; /* whatever is null will be allocated again */ + /* from within plan_a(), of all places */ } /* True if the remainder of the patch file contains a diff of some sort. */ @@ -142,7 +137,7 @@ there_is_another_patch(void) if (p_base != 0L && p_base >= p_filesize) { if (verbose) say("done\n"); - return FALSE; + return false; } if (verbose) say("Hmm..."); @@ -153,7 +148,7 @@ there_is_another_patch(void) say(" Ignoring the trailing garbage.\ndone\n"); } else say(" I can't seem to find a patch in there anywhere.\n"); - return FALSE; + return false; } if (verbose) say(" %sooks like %s to me...\n", @@ -171,14 +166,14 @@ there_is_another_patch(void) if (force || batch) { say("No file to patch. Skipping...\n"); filearg[0] = savestr(bestguess); - skip_rest_of_patch = TRUE; - return TRUE; + skip_rest_of_patch = true; + return true; } ask("File to patch: "); if (*buf != '\n') { free(bestguess); bestguess = savestr(buf); - filearg[0] = fetchname(buf, 0, FALSE); + filearg[0] = fetchname(buf, 0, false); } if (filearg[0] == NULL) { ask("No file found--skip this patch? [n] "); @@ -186,12 +181,12 @@ there_is_another_patch(void) continue; if (verbose) say("Skipping patch...\n"); - filearg[0] = fetchname(bestguess, 0, TRUE); - skip_rest_of_patch = TRUE; - return TRUE; + filearg[0] = fetchname(bestguess, 0, true); + skip_rest_of_patch = true; + return true; } } - return TRUE; + return true; } /* Determine what kind of diff is in the remaining part of the patch file. */ @@ -200,9 +195,10 @@ static int intuit_diff_type(void) { long this_line = 0, previous_line; - long first_command_line = -1, fcl_line; - bool last_line_was_command = FALSE, this_is_a_command = FALSE; - bool stars_last_line = FALSE, stars_this_line = FALSE; + long first_command_line = -1; + LINENUM fcl_line = -1; + bool last_line_was_command = false, this_is_a_command = false; + bool stars_last_line = false, stars_this_line = false; char *s, *t; char *indtmp = NULL; char *oldtmp = NULL; @@ -213,7 +209,7 @@ intuit_diff_type(void) int indent, retval; bool no_filearg = (filearg[0] == NULL); - ok_to_create_file = FALSE; + ok_to_create_file = false; fseek(pfp, p_base, SEEK_SET); p_input_line = p_bline - 1; for (;;) { @@ -283,7 +279,7 @@ intuit_diff_type(void) } if ((!diff_type || diff_type == UNI_DIFF) && strnEQ(s, "@@ -", 4)) { if (!atol(s + 3)) - ok_to_create_file = TRUE; + ok_to_create_file = true; p_indent = indent; p_start = this_line; p_sline = p_input_line; @@ -294,7 +290,7 @@ intuit_diff_type(void) if ((!diff_type || diff_type == CONTEXT_DIFF) && stars_last_line && strnEQ(s, "*** ", 4)) { if (!atol(s + 4)) - ok_to_create_file = TRUE; + ok_to_create_file = true; /* * if this is a new context diff the character just * before @@ -345,12 +341,12 @@ scan_exit: if (filearg[0] != NULL) bestguess = savestr(filearg[0]); else if (indtmp != NULL) - bestguess = fetchname(indtmp, strippath, TRUE); + bestguess = fetchname(indtmp, strippath, true); else { if (oldtmp != NULL) - oldname = fetchname(oldtmp, strippath, TRUE); + oldname = fetchname(oldtmp, strippath, true); if (newtmp != NULL) - newname = fetchname(newtmp, strippath, TRUE); + newname = fetchname(newtmp, strippath, true); if (oldname && newname) { if (strlen(oldname) < strlen(newname)) bestguess = savestr(oldname); @@ -388,13 +384,15 @@ skip_to(LINENUM file_pos, LINENUM file_line) { char *ret; - assert(p_base <= file_pos); + if (p_base > file_pos) + fatal("Internal error: seek %ld>%ld\n", p_base, file_pos); if (verbose && p_base < file_pos) { fseek(pfp, p_base, SEEK_SET); say("The text leading up to this was:\n--------------------------\n"); while (ftell(pfp) < file_pos) { ret = fgets(buf, sizeof buf, pfp); - assert(ret != NULL); + if (ret == NULL) + fatal("Unexpected end of file\n"); say("|%s", buf); } say("--------------------------\n"); @@ -426,12 +424,12 @@ remove_special_line(void) c = fgetc(pfp); } while (c != EOF && c != '\n'); - return TRUE; + return true; } if (c != EOF) fseek(pfp, -1L, SEEK_CUR); - return FALSE; + return false; } /* @@ -461,7 +459,6 @@ another_hunk(void) free(p_line[p_end]); p_end--; } - assert(p_end == -1); p_efake = -1; p_max = hunkmax; /* gets reduced when --- found */ @@ -469,9 +466,9 @@ another_hunk(void) line_beginning = ftell(pfp); repl_beginning = 0; fillcnt = 0; - ptrn_spaces_eaten = FALSE; - repl_could_be_missing = TRUE; - repl_missing = FALSE; + ptrn_spaces_eaten = false; + repl_could_be_missing = true; + repl_missing = false; repl_backtrack_position = 0; ptrn_copiable = 0; @@ -479,7 +476,7 @@ another_hunk(void) p_input_line++; if (ret == NULL || strnNE(buf, "********", 8)) { next_intuit_at(line_beginning, p_input_line); - return FALSE; + return false; } p_context = 100; p_hunk_beg = p_input_line + 1; @@ -493,21 +490,23 @@ another_hunk(void) strlcpy(buf, " \n", sizeof buf); } else { if (repl_beginning && repl_could_be_missing) { - repl_missing = TRUE; + repl_missing = true; goto hunk_done; } fatal("unexpected end of file in patch\n"); } } p_end++; - assert(p_end < hunkmax); + if (p_end >= hunkmax) + fatal("Internal error: hunk larger than hunk " + "buffer size"); p_char[p_end] = *buf; p_line[p_end] = NULL; switch (*buf) { case '*': if (strnEQ(buf, "********", 8)) { if (repl_beginning && repl_could_be_missing) { - repl_missing = TRUE; + repl_missing = true; goto hunk_done; } else fatal("unexpected end of hunk " @@ -516,7 +515,7 @@ another_hunk(void) } if (p_end != 0) { if (repl_beginning && repl_could_be_missing) { - repl_missing = TRUE; + repl_missing = true; goto hunk_done; } fatal("unexpected *** at line %ld: %s", @@ -526,7 +525,7 @@ another_hunk(void) p_line[p_end] = savestr(buf); if (out_of_mem) { p_end--; - return FALSE; + return false; } for (s = buf; *s && !isdigit(*s); s++) ; @@ -574,7 +573,7 @@ another_hunk(void) } else { if (repl_beginning) { if (repl_could_be_missing) { - repl_missing = TRUE; + repl_missing = true; goto hunk_done; } fatal("duplicate \"---\" at line %ld--check line numbers at line %ld\n", @@ -594,7 +593,7 @@ another_hunk(void) p_line[p_end] = savestr(buf); if (out_of_mem) { p_end--; - return FALSE; + return false; } p_char[p_end] = '='; for (s = buf; *s && !isdigit(*s); s++) @@ -625,20 +624,20 @@ another_hunk(void) grow_hunkmax(); if (p_repl_lines != ptrn_copiable && (p_context != 0 || p_repl_lines != 1)) - repl_could_be_missing = FALSE; + repl_could_be_missing = false; break; } goto change_line; case '+': case '!': - repl_could_be_missing = FALSE; + repl_could_be_missing = false; change_line: if (buf[1] == '\n' && canonicalize) strlcpy(buf + 1, " \n", sizeof buf - 1); if (!isspace(buf[1]) && buf[1] != '>' && buf[1] != '<' && repl_beginning && repl_could_be_missing) { - repl_missing = TRUE; + repl_missing = true; goto hunk_done; } if (context >= 0) { @@ -649,7 +648,7 @@ another_hunk(void) p_line[p_end] = savestr(buf + 2); if (out_of_mem) { p_end--; - return FALSE; + return false; } if (p_end == p_ptrn_lines) { if (remove_special_line()) { @@ -665,13 +664,13 @@ another_hunk(void) if (repl_beginning && repl_could_be_missing && (!ptrn_spaces_eaten || diff_type == NEW_CONTEXT_DIFF)) { - repl_missing = TRUE; + repl_missing = true; goto hunk_done; } p_line[p_end] = savestr(buf); if (out_of_mem) { p_end--; - return FALSE; + return false; } if (p_end != p_ptrn_lines + 1) { ptrn_spaces_eaten |= (repl_beginning != 0); @@ -684,7 +683,7 @@ another_hunk(void) case ' ': if (!isspace(buf[1]) && repl_beginning && repl_could_be_missing) { - repl_missing = TRUE; + repl_missing = true; goto hunk_done; } context++; @@ -693,12 +692,12 @@ another_hunk(void) p_line[p_end] = savestr(buf + 2); if (out_of_mem) { p_end--; - return FALSE; + return false; } break; default: if (repl_beginning && repl_could_be_missing) { - repl_missing = TRUE; + repl_missing = true; goto hunk_done; } malformed(); @@ -783,8 +782,10 @@ hunk_done: printf("fillsrc %ld, filldst %ld, rb %ld, e+1 %ld\n", fillsrc, filldst, repl_beginning, p_end + 1); #endif - assert(fillsrc == p_end + 1 || fillsrc == repl_beginning); - assert(filldst == p_end + 1 || filldst == repl_beginning); + if (fillsrc != p_end + 1 && fillsrc != repl_beginning) + malformed(); + if (filldst != p_end + 1 && filldst != repl_beginning) + malformed(); } if (p_line[p_end] != NULL) { if (remove_special_line()) { @@ -802,7 +803,7 @@ hunk_done: p_input_line++; if (ret == NULL || strnNE(buf, "@@ -", 4)) { next_intuit_at(line_beginning, p_input_line); - return FALSE; + return false; } s = buf + 4; if (!*s) @@ -846,7 +847,7 @@ hunk_done: p_line[0] = savestr(buf); if (out_of_mem) { p_end = -1; - return FALSE; + return false; } p_char[0] = '*'; snprintf(buf, sizeof buf, "--- %ld,%ld ----\n", p_newfirst, @@ -854,7 +855,7 @@ hunk_done: p_line[filldst] = savestr(buf); if (out_of_mem) { p_end = 0; - return FALSE; + return false; } p_char[filldst++] = '='; p_context = 100; @@ -883,7 +884,7 @@ hunk_done: while (--filldst > p_ptrn_lines) free(p_line[filldst]); p_end = fillsrc - 1; - return FALSE; + return false; } switch (ch) { case '-': @@ -922,7 +923,7 @@ hunk_done: while (--filldst > p_ptrn_lines) free(p_line[filldst]); p_end = fillsrc - 1; - return FALSE; + return false; } /* FALL THROUGH */ case '+': @@ -964,7 +965,7 @@ hunk_done: p_input_line++; if (ret == NULL || !isdigit(*buf)) { next_intuit_at(line_beginning, p_input_line); - return FALSE; + return false; } p_first = (LINENUM) atol(buf); for (s = buf; isdigit(*s); s++) @@ -1000,7 +1001,7 @@ hunk_done: p_line[0] = savestr(buf); if (out_of_mem) { p_end = -1; - return FALSE; + return false; } p_char[0] = '*'; for (i = 1; i <= p_ptrn_lines; i++) { @@ -1015,7 +1016,7 @@ hunk_done: p_line[i] = savestr(buf + 2); if (out_of_mem) { p_end = i - 1; - return FALSE; + return false; } p_len[i] = strlen(p_line[i]); p_char[i] = '-'; @@ -1039,7 +1040,7 @@ hunk_done: p_line[i] = savestr(buf); if (out_of_mem) { p_end = i - 1; - return FALSE; + return false; } p_char[i] = '='; for (i++; i <= p_end; i++) { @@ -1054,7 +1055,7 @@ hunk_done: p_line[i] = savestr(buf + 2); if (out_of_mem) { p_end = i - 1; - return FALSE; + return false; } p_len[i] = strlen(p_line[i]); p_char[i] = '+'; @@ -1086,7 +1087,7 @@ hunk_done: #endif if (p_end + 1 < hunkmax)/* paranoia reigns supreme... */ p_char[p_end + 1] = '^'; /* add a stopper for apply_hunk */ - return TRUE; + return true; } /* @@ -1124,7 +1125,7 @@ pch_swap(void) char *tp_char; /* +, -, and ! */ LINENUM i; LINENUM n; - bool blankline = FALSE; + bool blankline = false; char *s; i = p_first; @@ -1140,24 +1141,21 @@ pch_swap(void) p_len = NULL; p_char = NULL; set_hunkmax(); - if (p_line == NULL ||p_len == NULL ||p_char == NULL) { + if (p_line == NULL || p_len == NULL || p_char == NULL) { - if (p_line == NULL) /* XXX */ - free(p_line); + free(p_line); p_line = tp_line; - if (p_len == NULL) /* XXX */ - free(p_len); + free(p_len); p_len = tp_len; - if (p_char == NULL) /* XXX */ - free(p_char); + free(p_char); p_char = tp_char; - return FALSE; /* not enough memory to swap hunk! */ + return false; /* not enough memory to swap hunk! */ } /* now turn the new into the old */ i = p_ptrn_lines + 1; if (tp_char[i] == '\n') { /* account for possible blank line */ - blankline = TRUE; + blankline = true; i++; } if (p_efake >= 0) { /* fix non-freeable ptr range */ @@ -1182,7 +1180,9 @@ pch_swap(void) p_len[n] = tp_len[i]; n++; } - assert(p_char[0] == '='); + if (p_char[0] != '=') + fatal("Malformed patch at line %ld: expected '=' found '%c'\n", + p_input_line, p_char[0]); p_char[0] = '*'; for (s = p_line[0]; *s; s++) if (*s == '-') @@ -1190,7 +1190,9 @@ pch_swap(void) /* now turn the old into the new */ - assert(tp_char[0] == '*'); + if (p_char[0] != '*') + fatal("Malformed patch at line %ld: expected '*' found '%c'\n", + p_input_line, p_char[0]); tp_char[0] = '='; for (s = tp_line[0]; *s; s++) if (*s == '*') @@ -1202,18 +1204,21 @@ pch_swap(void) p_char[n] = '+'; p_len[n] = tp_len[i]; } - assert(i == p_ptrn_lines + 1); + + if (i != p_ptrn_lines + 1) + fatal("Malformed patch at line %ld: expected %ld lines, " + "got %ld\n", + p_input_line, p_ptrn_lines + 1, i); + i = p_ptrn_lines; p_ptrn_lines = p_repl_lines; p_repl_lines = i; - if (tp_line == NULL) /* XXX */ - free(tp_line); - if (tp_len == NULL) /* XXX */ - free(tp_len); - if (tp_char == NULL) /* XXX */ - free(tp_char); - return TRUE; + free(tp_line); + free(tp_len); + free(tp_char); + + return true; } /* @@ -1316,6 +1321,7 @@ do_ed_script(void) long beginning_of_this_line; FILE *pipefp; + pipefp = NULL; if (!skip_rest_of_patch) { if (copy_file(filearg[0], TMPOUTNAME) < 0) { unlink(TMPOUTNAME); @@ -1337,12 +1343,12 @@ do_ed_script(void) /* POSIX defines allowed commands as {a,c,d,i,s} */ if (isdigit(*buf) && (*t == 'a' || *t == 'c' || *t == 'd' || *t == 'i' || *t == 's')) { - if (!skip_rest_of_patch) + if (pipefp != NULL) fputs(buf, pipefp); if (*t != 'd') { while (pgets(buf, sizeof buf, pfp) != NULL) { p_input_line++; - if (!skip_rest_of_patch) + if (pipefp != NULL) fputs(buf, pipefp); if (strEQ(buf, ".\n")) break; @@ -1353,7 +1359,7 @@ do_ed_script(void) break; } } - if (skip_rest_of_patch) + if (pipefp == NULL) return; fprintf(pipefp, "w\n"); fprintf(pipefp, "q\n"); @@ -1362,7 +1368,7 @@ do_ed_script(void) ignore_signals(); if (!check_only) { if (move_file(TMPOUTNAME, outname) < 0) { - toutkeep = TRUE; + toutkeep = true; chmod(TMPOUTNAME, filemode); } else chmod(outname, filemode); diff --git a/usr.bin/patch/util.c b/usr.bin/patch/util.c index 8b6014f47f3..e295178cc06 100644 --- a/usr.bin/patch/util.c +++ b/usr.bin/patch/util.c @@ -1,7 +1,7 @@ -/* $OpenBSD: util.c,v 1.24 2003/07/31 20:51:43 otto Exp $ */ +/* $OpenBSD: util.c,v 1.25 2003/08/01 20:30:49 otto Exp $ */ #ifndef lint -static const char rcsid[] = "$OpenBSD: util.c,v 1.24 2003/07/31 20:51:43 otto Exp $"; +static const char rcsid[] = "$OpenBSD: util.c,v 1.25 2003/08/01 20:30:49 otto Exp $"; #endif /* not lint */ #include <sys/param.h> @@ -162,7 +162,7 @@ savestr(const char *s) rv = strdup(s); if (rv == NULL) { if (using_plan_a) - out_of_mem = TRUE; + out_of_mem = true; else fatal("out of memory\n"); } @@ -275,7 +275,7 @@ ignore_signals(void) /* * Make sure we'll have the directories to create a file. If `striplast' is - * TRUE, ignore the last element of `filename'. + * true, ignore the last element of `filename'. */ void |