Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/usr.bin/patch As per OpenBSD, use malloc for the line buffer...
details: https://anonhg.NetBSD.org/src/rev/dd8184059cd9
branches: trunk
changeset: 1021307:dd8184059cd9
user: cjep <cjep%NetBSD.org@localhost>
date: Tue May 25 11:25:59 2021 +0000
description:
As per OpenBSD, use malloc for the line buffer. Fixes the known issue
with long lines and makes our ATF test suite pass fully.
Closes PR bin/54620 from coypu who suggested the approach.
Reviewed by christos.
diffstat:
usr.bin/patch/common.h | 8 ++--
usr.bin/patch/inp.c | 17 +++++---
usr.bin/patch/patch.c | 13 ++++--
usr.bin/patch/pch.c | 94 ++++++++++++++++++++++++++-----------------------
usr.bin/patch/util.c | 10 ++--
5 files changed, 78 insertions(+), 64 deletions(-)
diffs (truncated from 487 to 300 lines):
diff -r 21d1b3ef1a3d -r dd8184059cd9 usr.bin/patch/common.h
--- a/usr.bin/patch/common.h Tue May 25 10:58:41 2021 +0000
+++ b/usr.bin/patch/common.h Tue May 25 11:25:59 2021 +0000
@@ -1,7 +1,7 @@
/*
* $OpenBSD: common.h,v 1.26 2006/03/11 19:41:30 otto Exp $
* $DragonFly: src/usr.bin/patch/common.h,v 1.5 2008/08/10 23:50:12 joerg Exp $
- * $NetBSD: common.h,v 1.21 2015/07/24 18:56:44 christos Exp $
+ * $NetBSD: common.h,v 1.22 2021/05/25 11:25:59 cjep Exp $
*/
/*
@@ -41,7 +41,7 @@
#define MAXHUNKSIZE 100000 /* is this enough lines? */
#define INITHUNKMAX 125 /* initial dynamic allocation size */
-#define MAXLINELEN 8192
+#define INITLINELEN 8192
#define BUFFERSIZE 1024
#define LINENUM_MAX LONG_MAX
@@ -71,8 +71,8 @@
extern mode_t filemode;
-extern char buf[MAXLINELEN];/* general purpose buffer */
-extern size_t buf_len;
+extern char *buf; /* general purpose buffer */
+extern size_t bufsz; /* general purpose buffer size */
extern bool using_plan_a; /* try to keep everything in memory */
extern bool out_of_mem; /* ran out of memory in plan a */
diff -r 21d1b3ef1a3d -r dd8184059cd9 usr.bin/patch/inp.c
--- a/usr.bin/patch/inp.c Tue May 25 10:58:41 2021 +0000
+++ b/usr.bin/patch/inp.c Tue May 25 11:25:59 2021 +0000
@@ -1,7 +1,7 @@
/*
* $OpenBSD: inp.c,v 1.34 2006/03/11 19:41:30 otto Exp $
* $DragonFly: src/usr.bin/patch/inp.c,v 1.6 2007/09/29 23:11:10 swildner Exp $
- * $NetBSD: inp.c,v 1.26 2018/06/18 18:33:31 christos Exp $
+ * $NetBSD: inp.c,v 1.27 2021/05/25 11:25:59 cjep Exp $
*/
/*
@@ -31,7 +31,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: inp.c,v 1.26 2018/06/18 18:33:31 christos Exp $");
+__RCSID("$NetBSD: inp.c,v 1.27 2021/05/25 11:25:59 cjep Exp $");
#include <sys/types.h>
#include <sys/file.h>
@@ -142,11 +142,11 @@
plan_a(const char *filename)
{
int ifd, statfailed, devnull, pstat;
- char *p, *s, lbuf[MAXLINELEN];
+ char *p, *s, *lbuf;
struct stat filestat;
off_t i;
ptrdiff_t sz;
- size_t iline, lines_allocated;
+ size_t iline, lines_allocated, lbufsz;
pid_t pid;
char *argp[4] = {NULL};
@@ -193,9 +193,14 @@
filebase = basename(tmp_filename1);
filedir = dirname(tmp_filename2);
+
+ lbufsz = INITLINELEN;
+ if ((lbuf = malloc(bufsz)) == NULL)
+ pfatal("allocating line buffer");
+ lbuf[0] = '\0';
#define try(f, a1, a2, a3) \
- (snprintf(lbuf, sizeof lbuf, f, a1, a2, a3), stat(lbuf, &cstat) == 0)
+ (snprintf(lbuf, lbufsz, f, a1, a2, a3), stat(lbuf, &cstat) == 0)
/*
* else we can't write to it but it's not under a version
@@ -391,7 +396,7 @@
unlink(TMPINNAME);
if ((tifd = open(TMPINNAME, O_EXCL | O_CREAT | O_WRONLY, 0666)) < 0)
pfatal("can't open file %s", TMPINNAME);
- while (fgets(buf, buf_len, ifp) != NULL) {
+ while (getline(&buf, &bufsz, ifp) != -1) {
if (revision != NULL && !found_revision && rev_in_string(buf))
found_revision = true;
if ((i = strlen(buf)) > maxlen)
diff -r 21d1b3ef1a3d -r dd8184059cd9 usr.bin/patch/patch.c
--- a/usr.bin/patch/patch.c Tue May 25 10:58:41 2021 +0000
+++ b/usr.bin/patch/patch.c Tue May 25 11:25:59 2021 +0000
@@ -1,7 +1,7 @@
/*
* $OpenBSD: patch.c,v 1.45 2007/04/18 21:52:24 sobrado Exp $
* $DragonFly: src/usr.bin/patch/patch.c,v 1.10 2008/08/10 23:39:56 joerg Exp $
- * $NetBSD: patch.c,v 1.31 2021/02/20 09:17:13 nia Exp $
+ * $NetBSD: patch.c,v 1.32 2021/05/25 11:25:59 cjep Exp $
*/
/*
@@ -31,7 +31,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: patch.c,v 1.31 2021/02/20 09:17:13 nia Exp $");
+__RCSID("$NetBSD: patch.c,v 1.32 2021/05/25 11:25:59 cjep Exp $");
#include <sys/types.h>
#include <sys/stat.h>
@@ -53,8 +53,8 @@
mode_t filemode = 0644;
-char buf[MAXLINELEN]; /* general purpose buffer */
-size_t buf_len = sizeof(buf);
+char *buf; /* general purpose buffer */
+size_t bufsz; /* general purpose buffer size */
bool using_plan_a = true; /* try to keep everything in memory */
bool out_of_mem = false; /* ran out of memory in plan a */
@@ -157,6 +157,11 @@
const char *tmpdir;
char *v;
+ bufsz = INITLINELEN;
+ if ((buf = malloc(bufsz)) == NULL)
+ pfatal("allocating input buffer");
+ buf[0] = '\0';
+
setbuf(stderr, serrbuf);
for (i = 0; i < MAXFILEC; i++)
filearg[i] = NULL;
diff -r 21d1b3ef1a3d -r dd8184059cd9 usr.bin/patch/pch.c
--- a/usr.bin/patch/pch.c Tue May 25 10:58:41 2021 +0000
+++ b/usr.bin/patch/pch.c Tue May 25 11:25:59 2021 +0000
@@ -1,7 +1,7 @@
/*
* $OpenBSD: pch.c,v 1.37 2007/09/02 15:19:33 deraadt Exp $
* $DragonFly: src/usr.bin/patch/pch.c,v 1.6 2008/08/10 23:35:40 joerg Exp $
- * $NetBSD: pch.c,v 1.31 2020/11/17 20:49:12 rhialto Exp $
+ * $NetBSD: pch.c,v 1.32 2021/05/25 11:25:59 cjep Exp $
*/
/*
@@ -31,7 +31,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: pch.c,v 1.31 2020/11/17 20:49:12 rhialto Exp $");
+__RCSID("$NetBSD: pch.c,v 1.32 2021/05/25 11:25:59 cjep Exp $");
#include <sys/types.h>
#include <sys/stat.h>
@@ -79,7 +79,7 @@
static int intuit_diff_type(void);
static void next_intuit_at(LINENUM, LINENUM);
static void skip_to(LINENUM, LINENUM);
-static char *pgets(char *, int, FILE *);
+static int pgetline(char **, size_t *, FILE *);
static char *best_name(const struct file_name *, bool);
static char *posix_name(const struct file_name *, bool);
static size_t num_components(const char *);
@@ -111,8 +111,8 @@
pfp = fopen(TMPPATNAME, "w");
if (pfp == NULL)
pfatal("can't create %s", TMPPATNAME);
- while (fgets(buf, buf_len, stdin) != NULL)
- fputs(buf, pfp);
+ while (getline(&buf, &bufsz, stdin) != -1)
+ fprintf(pfp, "%s", buf);
if (ferror(pfp) || fclose(pfp))
pfatal("can't write %s", TMPPATNAME);
filename = TMPPATNAME;
@@ -271,7 +271,7 @@
this_line = ftell(pfp);
indent = 0;
p_input_line++;
- if (fgets(buf, buf_len, pfp) == NULL) {
+ if (getline(&buf, &bufsz, pfp) == -1) {
if (first_command_line >= 0L) {
/* nothing but deletes!? */
p_start = first_command_line;
@@ -428,7 +428,7 @@
static void
skip_to(LINENUM file_pos, LINENUM file_line)
{
- char *ret;
+ int ret;
if (p_base > file_pos)
fatal("Internal error: seek %ld>%ld\n", p_base, file_pos);
@@ -436,8 +436,8 @@
fseek(pfp, p_base, SEEK_SET);
say("The text leading up to this was:\n--------------------------\n");
while (ftell(pfp) < file_pos) {
- ret = fgets(buf, buf_len, pfp);
- if (ret == NULL)
+ ret = getline(&buf, &bufsz, pfp);
+ if (ret == -1)
fatal("Unexpected end of file\n");
say("|%s", buf);
}
@@ -517,8 +517,9 @@
long repl_backtrack_position; /* file pos of first repl line */
LINENUM repl_patch_line; /* input line number for same */
LINENUM ptrn_copiable; /* # of copiable lines in ptrn */
- char *s, *ret;
+ char *s;
int context = 0;
+ int ret;
while (p_end >= 0) {
if (p_end == p_efake)
@@ -543,9 +544,9 @@
repl_patch_line = 0;
ptrn_copiable = 0;
- ret = pgets(buf, buf_len, pfp);
+ ret = pgetline(&buf, &bufsz, pfp);
p_input_line++;
- if (ret == NULL || strnNE(buf, "********", 8)) {
+ if (ret == -1 || strnNE(buf, "********", 8)) {
next_intuit_at(line_beginning, p_input_line);
return false;
}
@@ -553,9 +554,9 @@
p_hunk_beg = p_input_line + 1;
while (p_end < p_max) {
line_beginning = ftell(pfp);
- ret = pgets(buf, buf_len, pfp);
+ ret = pgetline(&buf, &bufsz, pfp);
p_input_line++;
- if (ret == NULL) {
+ if (ret == -1) {
if (repl_beginning && repl_could_be_missing) {
repl_missing = true;
goto hunk_done;
@@ -705,7 +706,7 @@
repl_could_be_missing = false;
change_line:
if (buf[1] == '\n' && canonicalize)
- strlcpy(buf + 1, " \n", buf_len - 1);
+ strlcpy(buf + 1, " \n", bufsz - 1);
if (!isspace((unsigned char)buf[1]) && buf[1] != '>' &&
buf[1] != '<' &&
repl_beginning && repl_could_be_missing) {
@@ -871,9 +872,9 @@
char ch;
line_beginning = ftell(pfp); /* file pos of the current line */
- ret = pgets(buf, buf_len, pfp);
+ ret = pgetline(&buf, &bufsz, pfp);
p_input_line++;
- if (ret == NULL || strnNE(buf, "@@ -", 4)) {
+ if (ret == -1 || strnNE(buf, "@@ -", 4)) {
next_intuit_at(line_beginning, p_input_line);
return false;
}
@@ -914,7 +915,7 @@
fillold = 1;
fillnew = fillold + p_ptrn_lines;
p_end = fillnew + p_repl_lines;
- snprintf(buf, buf_len, "*** %ld,%ld ****\n", p_first,
+ snprintf(buf, bufsz, "*** %ld,%ld ****\n", p_first,
p_first + p_ptrn_lines - 1);
p_line[0] = savestr(buf);
if (out_of_mem) {
@@ -922,7 +923,7 @@
return false;
}
p_char[0] = '*';
- snprintf(buf, buf_len, "--- %ld,%ld ----\n", p_newfirst,
+ snprintf(buf, bufsz, "--- %ld,%ld ----\n", p_newfirst,
p_newfirst + p_repl_lines - 1);
p_line[fillnew] = savestr(buf);
if (out_of_mem) {
@@ -935,12 +936,12 @@
p_hunk_beg = p_input_line + 1;
while (fillold <= p_ptrn_lines || fillnew <= p_end) {
line_beginning = ftell(pfp);
- ret = pgets(buf, buf_len, pfp);
+ ret = pgetline(&buf, &bufsz, pfp);
p_input_line++;
- if (ret == NULL) {
+ if (ret == -1) {
if (p_max - fillnew < 3) {
/* assume blank lines got chopped */
- strlcpy(buf, " \n", buf_len);
+ strlcpy(buf, " \n", bufsz);
} else {
fatal("unexpected end of file in patch\n");
}
@@ -1039,9 +1040,9 @@
line_beginning = ftell(pfp);
p_context = 0;
Home |
Main Index |
Thread Index |
Old Index