View | Details | Raw Unified | Return to bug 195436 | Differences between
and this patch

Collapse All | Expand All

(-)usr.bin/patch/common.h (+1 lines)
Lines 40-45 Link Here
40
#define	INITHUNKMAX 125		/* initial dynamic allocation size */
40
#define	INITHUNKMAX 125		/* initial dynamic allocation size */
41
#define	INITLINELEN 4096
41
#define	INITLINELEN 4096
42
#define	BUFFERSIZE 4096
42
#define	BUFFERSIZE 4096
43
#define	LINENUM_MAX LONG_MAX
43
44
44
#define	SCCSPREFIX "s."
45
#define	SCCSPREFIX "s."
45
#define	GET "get -e %s"
46
#define	GET "get -e %s"
(-)usr.bin/patch/pch.c (-34 / +73 lines)
Lines 78-83 Link Here
78
static char	*best_name(const struct file_name *, bool);
78
static char	*best_name(const struct file_name *, bool);
79
static char	*posix_name(const struct file_name *, bool);
79
static char	*posix_name(const struct file_name *, bool);
80
static size_t	num_components(const char *);
80
static size_t	num_components(const char *);
81
static LINENUM	strtolinenum(char *, char **);
81
82
82
/*
83
/*
83
 * Prepare to look for the next patch in the patch file.
84
 * Prepare to look for the next patch in the patch file.
Lines 353-359 Link Here
353
		stars_this_line = strnEQ(s, "********", 8);
354
		stars_this_line = strnEQ(s, "********", 8);
354
		if ((!diff_type || diff_type == CONTEXT_DIFF) && stars_last_line &&
355
		if ((!diff_type || diff_type == CONTEXT_DIFF) && stars_last_line &&
355
		    strnEQ(s, "*** ", 4)) {
356
		    strnEQ(s, "*** ", 4)) {
356
			if (atol(s + 4) == 0)
357
			if (strtolinenum(s + 4, &s) == 0)
357
				ok_to_create_file = true;
358
				ok_to_create_file = true;
358
			/*
359
			/*
359
			 * If this is a new context diff the character just
360
			 * If this is a new context diff the character just
Lines 591-605 Link Here
591
					malformed();
592
					malformed();
592
				if (strnEQ(s, "0,0", 3))
593
				if (strnEQ(s, "0,0", 3))
593
					memmove(s, s + 2, strlen(s + 2) + 1);
594
					memmove(s, s + 2, strlen(s + 2) + 1);
594
				p_first = (LINENUM) atol(s);
595
				p_first = strtolinenum(s, &s);
595
				while (isdigit((unsigned char)*s))
596
					s++;
597
				if (*s == ',') {
596
				if (*s == ',') {
598
					for (; *s && !isdigit((unsigned char)*s); s++)
597
					for (; *s && !isdigit((unsigned char)*s); s++)
599
						;
598
						;
600
					if (!*s)
599
					if (!*s)
601
						malformed();
600
						malformed();
602
					p_ptrn_lines = ((LINENUM) atol(s)) - p_first + 1;
601
					p_ptrn_lines = strtolinenum(s, &s) - p_first + 1;
602
					if (p_ptrn_lines < 0)
603
						malformed();
603
				} else if (p_first)
604
				} else if (p_first)
604
					p_ptrn_lines = 1;
605
					p_ptrn_lines = 1;
605
				else {
606
				else {
Lines 606-611 Link Here
606
					p_ptrn_lines = 0;
607
					p_ptrn_lines = 0;
607
					p_first = 1;
608
					p_first = 1;
608
				}
609
				}
610
				if (p_first >= LINENUM_MAX - p_ptrn_lines ||
611
				    p_ptrn_lines >= LINENUM_MAX - 6)
612
					malformed();
609
613
610
				/* we need this much at least */
614
				/* we need this much at least */
611
				p_max = p_ptrn_lines + 6;
615
				p_max = p_ptrn_lines + 6;
Lines 658-673 Link Here
658
						;
662
						;
659
					if (!*s)
663
					if (!*s)
660
						malformed();
664
						malformed();
661
					p_newfirst = (LINENUM) atol(s);
665
					p_newfirst = strtolinenum(s, &s);
662
					while (isdigit((unsigned char)*s))
663
						s++;
664
					if (*s == ',') {
666
					if (*s == ',') {
665
						for (; *s && !isdigit((unsigned char)*s); s++)
667
						for (; *s && !isdigit((unsigned char)*s); s++)
666
							;
668
							;
667
						if (!*s)
669
						if (!*s)
668
							malformed();
670
							malformed();
669
						p_repl_lines = ((LINENUM) atol(s)) -
671
						p_repl_lines = strtolinenum(s, &s) -
670
						    p_newfirst + 1;
672
						    p_newfirst + 1;
673
						if (p_repl_lines < 0)
674
							malformed();
671
					} else if (p_newfirst)
675
					} else if (p_newfirst)
672
						p_repl_lines = 1;
676
						p_repl_lines = 1;
673
					else {
677
					else {
Lines 674-679 Link Here
674
						p_repl_lines = 0;
678
						p_repl_lines = 0;
675
						p_newfirst = 1;
679
						p_newfirst = 1;
676
					}
680
					}
681
					if (p_newfirst >= LINENUM_MAX - p_repl_lines ||
682
					    p_repl_lines >= LINENUM_MAX - p_end)
683
						malformed();
677
					p_max = p_repl_lines + p_end;
684
					p_max = p_repl_lines + p_end;
678
					if (p_max > MAXHUNKSIZE)
685
					if (p_max > MAXHUNKSIZE)
679
						fatal("hunk too large (%ld lines) at line %ld: %s",
686
						fatal("hunk too large (%ld lines) at line %ld: %s",
Lines 866-878 Link Here
866
		s = buf + 4;
873
		s = buf + 4;
867
		if (!*s)
874
		if (!*s)
868
			malformed();
875
			malformed();
869
		p_first = (LINENUM) atol(s);
876
		p_first = strtolinenum(s, &s);
870
		while (isdigit((unsigned char)*s))
871
			s++;
872
		if (*s == ',') {
877
		if (*s == ',') {
873
			p_ptrn_lines = (LINENUM) atol(++s);
878
			p_ptrn_lines = strtolinenum(s + 1, &s);
874
			while (isdigit((unsigned char)*s))
875
				s++;
876
		} else
879
		} else
877
			p_ptrn_lines = 1;
880
			p_ptrn_lines = 1;
878
		if (*s == ' ')
881
		if (*s == ' ')
Lines 879-891 Link Here
879
			s++;
882
			s++;
880
		if (*s != '+' || !*++s)
883
		if (*s != '+' || !*++s)
881
			malformed();
884
			malformed();
882
		p_newfirst = (LINENUM) atol(s);
885
		p_newfirst = strtolinenum(s, &s);
883
		while (isdigit((unsigned char)*s))
884
			s++;
885
		if (*s == ',') {
886
		if (*s == ',') {
886
			p_repl_lines = (LINENUM) atol(++s);
887
			p_repl_lines = strtolinenum(s + 1, &s);
887
			while (isdigit((unsigned char)*s))
888
				s++;
889
		} else
888
		} else
890
			p_repl_lines = 1;
889
			p_repl_lines = 1;
891
		if (*s == ' ')
890
		if (*s == ' ')
Lines 892-897 Link Here
892
			s++;
891
			s++;
893
		if (*s != '@')
892
		if (*s != '@')
894
			malformed();
893
			malformed();
894
		if (p_first >= LINENUM_MAX - p_ptrn_lines ||
895
		    p_newfirst > LINENUM_MAX - p_repl_lines ||
896
		    p_ptrn_lines >= LINENUM_MAX - p_repl_lines - 1)
897
			malformed();
895
		if (!p_ptrn_lines)
898
		if (!p_ptrn_lines)
896
			p_first++;	/* do append rather than insert */
899
			p_first++;	/* do append rather than insert */
897
		p_max = p_ptrn_lines + p_repl_lines + 1;
900
		p_max = p_ptrn_lines + p_repl_lines + 1;
Lines 1031-1065 Link Here
1031
			next_intuit_at(line_beginning, p_input_line);
1034
			next_intuit_at(line_beginning, p_input_line);
1032
			return false;
1035
			return false;
1033
		}
1036
		}
1034
		p_first = (LINENUM) atol(buf);
1037
		p_first = strtolinenum(buf, &s);
1035
		for (s = buf; isdigit((unsigned char)*s); s++)
1036
			;
1037
		if (*s == ',') {
1038
		if (*s == ',') {
1038
			p_ptrn_lines = (LINENUM) atol(++s) - p_first + 1;
1039
			p_ptrn_lines = strtolinenum(s + 1, &s) - p_first + 1;
1039
			while (isdigit((unsigned char)*s))
1040
			if (p_ptrn_lines < 0)
1040
				s++;
1041
				malformed();
1041
		} else
1042
		} else
1042
			p_ptrn_lines = (*s != 'a');
1043
			p_ptrn_lines = (*s != 'a');
1044
		if (p_first >= LINENUM_MAX - p_ptrn_lines)
1045
			malformed();
1043
		hunk_type = *s;
1046
		hunk_type = *s;
1044
		if (hunk_type == 'a')
1047
		if (hunk_type == 'a')
1045
			p_first++;	/* do append rather than insert */
1048
			p_first++;	/* do append rather than insert */
1046
		min = (LINENUM) atol(++s);
1049
		min = strtolinenum(s + 1, &s);
1047
		for (; isdigit((unsigned char)*s); s++)
1048
			;
1049
		if (*s == ',')
1050
		if (*s == ',')
1050
			max = (LINENUM) atol(++s);
1051
			max = strtolinenum(s + 1, &s);
1051
		else
1052
		else
1052
			max = min;
1053
			max = min;
1054
		if (min < 0 || min > max || max - min == LINENUM_MAX)
1055
			malformed();
1053
		if (hunk_type == 'd')
1056
		if (hunk_type == 'd')
1054
			min++;
1057
			min++;
1055
		p_end = p_ptrn_lines + 1 + max - min + 1;
1058
		p_newfirst = min;
1059
		p_repl_lines = max - min + 1;
1060
		if (p_newfirst > LINENUM_MAX - p_repl_lines ||
1061
		    p_ptrn_lines >= LINENUM_MAX - p_repl_lines - 1)
1062
			malformed();
1063
		p_end = p_ptrn_lines + p_repl_lines + 1;
1056
		if (p_end > MAXHUNKSIZE)
1064
		if (p_end > MAXHUNKSIZE)
1057
			fatal("hunk too large (%ld lines) at line %ld: %s",
1065
			fatal("hunk too large (%ld lines) at line %ld: %s",
1058
			    p_end, p_input_line, buf);
1066
			    p_end, p_input_line, buf);
1059
		while (p_end >= hunkmax)
1067
		while (p_end >= hunkmax)
1060
			grow_hunkmax();
1068
			grow_hunkmax();
1061
		p_newfirst = min;
1062
		p_repl_lines = max - min + 1;
1063
		snprintf(buf, buf_size, "*** %ld,%ld\n", p_first,
1069
		snprintf(buf, buf_size, "*** %ld,%ld\n", p_first,
1064
		    p_first + p_ptrn_lines - 1);
1070
		    p_first + p_ptrn_lines - 1);
1065
		p_line[0] = savestr(buf);
1071
		p_line[0] = savestr(buf);
Lines 1579-1581 Link Here
1579
	}
1585
	}
1580
	return n;
1586
	return n;
1581
}
1587
}
1588
1589
/*
1590
 * Convert number at NPTR into LINENUM and save address of first
1591
 * character that is not a digit in ENDPTR.  If conversion is not
1592
 * possible, call fatal.
1593
 */
1594
static LINENUM
1595
strtolinenum(char *nptr, char **endptr)
1596
{
1597
	LINENUM rv;
1598
	char c;
1599
	char *p;
1600
	const char *errstr;
1601
1602
	for (p = nptr; isdigit((unsigned char)*p); p++)
1603
		;
1604
1605
	if (p == nptr)
1606
		malformed();
1607
1608
	c = *p;
1609
	*p = '\0';
1610
1611
	rv = strtonum(nptr, 0, LINENUM_MAX, &errstr);
1612
	if (errstr != NULL)
1613
		fatal("invalid line number at line %ld: `%s' is %s\n",
1614
		    p_input_line, nptr, errstr);
1615
 
1616
	*p = c;
1617
	*endptr = p;
1618
1619
	return rv;
1620
}

Return to bug 195436