summaryrefslogtreecommitdiffstats
path: root/usr.bin/patch/patch.c
diff options
context:
space:
mode:
authorpfg <pfg@FreeBSD.org>2014-06-22 20:24:17 +0000
committerpfg <pfg@FreeBSD.org>2014-06-22 20:24:17 +0000
commit031b23ab3197da29793c53cf3524652fba44ddfc (patch)
tree7416d09f80eb3d69c3293f368c3e02920e384d19 /usr.bin/patch/patch.c
parent0d8805deb240bfb4e7242c264b24ce5065fb4e27 (diff)
downloadFreeBSD-src-031b23ab3197da29793c53cf3524652fba44ddfc.zip
FreeBSD-src-031b23ab3197da29793c53cf3524652fba44ddfc.tar.gz
MFC r267490:
patch: unsign the line length to avoid overflows. Patch(1) uses a short int for the line length, which is usually sufficient for regular diffs, but makes no effort to signal when there is an overflow. Change the line length to an unsigned short int to better use the fact that a length is never negative. The change is loosely inspired on a related change in DragonFly, but we avoid spending more memory than necessary. While here adjust the messages to be clearer on what is happening.
Diffstat (limited to 'usr.bin/patch/patch.c')
-rw-r--r--usr.bin/patch/patch.c14
1 files changed, 9 insertions, 5 deletions
diff --git a/usr.bin/patch/patch.c b/usr.bin/patch/patch.c
index 1743678..7a53284 100644
--- a/usr.bin/patch/patch.c
+++ b/usr.bin/patch/patch.c
@@ -742,14 +742,18 @@ abort_context_hunk(void)
static void
rej_line(int ch, LINENUM i)
{
- size_t len;
+ unsigned short len;
const char *line = pfetch(i);
- len = strlen(line);
+ len = strnlen(line, USHRT_MAX);
fprintf(rejfp, "%c%s", ch, line);
- if (len == 0 || line[len-1] != '\n')
- fprintf(rejfp, "\n\\ No newline at end of file\n");
+ if (len == 0 || line[len-1] != '\n') {
+ if (len >= USHRT_MAX)
+ fprintf(rejfp, "\n\\ Line too long\n");
+ else
+ fprintf(rejfp, "\n\\ No newline at end of line\n");
+ }
}
static void
@@ -1016,7 +1020,7 @@ patch_match(LINENUM base, LINENUM offset, LINENUM fuzz)
LINENUM pat_lines = pch_ptrn_lines() - fuzz;
const char *ilineptr;
const char *plineptr;
- short plinelen;
+ unsigned short plinelen;
for (iline = base + offset + fuzz; pline <= pat_lines; pline++, iline++) {
ilineptr = ifetch(iline, offset >= 0);
OpenPOWER on IntegriCloud