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

Collapse All | Expand All

(-)b/usr.bin/patch/common.h (-1 / +2 lines)
Lines 23-29 Link Here
23
 * -C option added in 1998, original code by Marc Espie, based on FreeBSD
23
 * -C option added in 1998, original code by Marc Espie, based on FreeBSD
24
 * behaviour
24
 * behaviour
25
 *
25
 *
26
 * $OpenBSD: common.h,v 1.26 2006/03/11 19:41:30 otto Exp $
26
 * $OpenBSD: common.h,v 1.28 2014/11/25 10:26:07 tobias Exp $
27
 * $FreeBSD$
27
 * $FreeBSD$
28
 */
28
 */
29
29
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"
(-)b/usr.bin/patch/pch.c (-19 / +73 lines)
Lines 24-30 Link Here
24
 * -C option added in 1998, original code by Marc Espie, based on FreeBSD
24
 * -C option added in 1998, original code by Marc Espie, based on FreeBSD
25
 * behaviour
25
 * behaviour
26
 *
26
 *
27
 * $OpenBSD: pch.c,v 1.39 2012/04/11 08:07:13 ajacoutot Exp $
27
 * $OpenBSD: pch.c,v 1.46 2014/11/26 10:11:21 tobias Exp $
28
 * $FreeBSD$
28
 * $FreeBSD$
29
 */
29
 */
30
30
Lines 38-43 Link Here
38
#include <stdlib.h>
38
#include <stdlib.h>
39
#include <string.h>
39
#include <string.h>
40
#include <unistd.h>
40
#include <unistd.h>
41
#include <errno.h>
41
42
42
#include "common.h"
43
#include "common.h"
43
#include "util.h"
44
#include "util.h"
Lines 589-595 another_hunk(void) Link Here
589
					malformed();
590
					malformed();
590
				if (strnEQ(s, "0,0", 3))
591
				if (strnEQ(s, "0,0", 3))
591
					memmove(s, s + 2, strlen(s + 2) + 1);
592
					memmove(s, s + 2, strlen(s + 2) + 1);
592
				p_first = (LINENUM) atol(s);
593
				p_first = (LINENUM) strtol(s, 0, 0);
594
				if (errno == ERANGE)
595
					fatal("at line %ld: bad line number %s",
596
						p_input_line, s);
593
				while (isdigit((unsigned char)*s))
597
				while (isdigit((unsigned char)*s))
594
					s++;
598
					s++;
595
				if (*s == ',') {
599
				if (*s == ',') {
Lines 597-603 another_hunk(void) Link Here
597
						;
601
						;
598
					if (!*s)
602
					if (!*s)
599
						malformed();
603
						malformed();
600
					p_ptrn_lines = ((LINENUM) atol(s)) - p_first + 1;
604
					p_ptrn_lines = ((LINENUM) strtol(s, 0, 
605
						0)) - p_first + 1;
606
					if (errno == ERANGE)
607
						fatal("at line %ld: bad line number %s",
608
							p_input_line, s);
601
				} else if (p_first)
609
				} else if (p_first)
602
					p_ptrn_lines = 1;
610
					p_ptrn_lines = 1;
603
				else {
611
				else {
Lines 605-610 another_hunk(void) Link Here
605
					p_first = 1;
613
					p_first = 1;
606
				}
614
				}
607
615
616
				if (p_first >= LINENUM_MAX - p_ptrn_lines ||
617
					p_ptrn_lines >= LINENUM_MAX - 6)
618
					malformed();
608
				/* we need this much at least */
619
				/* we need this much at least */
609
				p_max = p_ptrn_lines + 6;
620
				p_max = p_ptrn_lines + 6;
610
				while (p_max >= hunkmax)
621
				while (p_max >= hunkmax)
Lines 656-662 another_hunk(void) Link Here
656
						;
667
						;
657
					if (!*s)
668
					if (!*s)
658
						malformed();
669
						malformed();
659
					p_newfirst = (LINENUM) atol(s);
670
					p_newfirst = (LINENUM) strtol(s, 0, 0);
671
					if (errno == ERANGE)
672
						fatal("at line %ld: bad line number %s",
673
							p_input_line, s);
660
					while (isdigit((unsigned char)*s))
674
					while (isdigit((unsigned char)*s))
661
						s++;
675
						s++;
662
					if (*s == ',') {
676
					if (*s == ',') {
Lines 664-677 another_hunk(void) Link Here
664
							;
678
							;
665
						if (!*s)
679
						if (!*s)
666
							malformed();
680
							malformed();
667
						p_repl_lines = ((LINENUM) atol(s)) -
681
						p_repl_lines = ((LINENUM) strtol(s, 0, 
668
						    p_newfirst + 1;
682
							0)) - p_newfirst + 1;
683
						if (errno == ERANGE)
684
								fatal("at line %ld: bad line number %s", 
685
									p_input_line, s);
669
					} else if (p_newfirst)
686
					} else if (p_newfirst)
670
						p_repl_lines = 1;
687
						p_repl_lines = 1;
671
					else {
688
					else {
672
						p_repl_lines = 0;
689
						p_repl_lines = 0;
673
						p_newfirst = 1;
690
						p_newfirst = 1;
674
					}
691
					}
692
693
					if (p_newfirst >= LINENUM_MAX - p_repl_lines ||
694
						p_repl_lines >= LINENUM_MAX - p_end)
695
						malformed();
675
					p_max = p_repl_lines + p_end;
696
					p_max = p_repl_lines + p_end;
676
					if (p_max > MAXHUNKSIZE)
697
					if (p_max > MAXHUNKSIZE)
677
						fatal("hunk too large (%ld lines) at line %ld: %s",
698
						fatal("hunk too large (%ld lines) at line %ld: %s",
Lines 864-874 hunk_done: Link Here
864
		s = buf + 4;
885
		s = buf + 4;
865
		if (!*s)
886
		if (!*s)
866
			malformed();
887
			malformed();
867
		p_first = (LINENUM) atol(s);
888
		p_first = (LINENUM) strtol(s, 0, 0);
889
		if (errno == ERANGE)
890
				fatal("at line %ld: bad line number %s",
891
					p_input_line, s);
868
		while (isdigit((unsigned char)*s))
892
		while (isdigit((unsigned char)*s))
869
			s++;
893
			s++;
870
		if (*s == ',') {
894
		if (*s == ',') {
871
			p_ptrn_lines = (LINENUM) atol(++s);
895
			p_ptrn_lines = (LINENUM) strtol(++s, 0, 0);
896
			if (errno == ERANGE)
897
				fatal("at line %ld: bad line number %s",
898
					p_input_line, s);
872
			while (isdigit((unsigned char)*s))
899
			while (isdigit((unsigned char)*s))
873
				s++;
900
				s++;
874
		} else
901
		} else
Lines 877-887 hunk_done: Link Here
877
			s++;
904
			s++;
878
		if (*s != '+' || !*++s)
905
		if (*s != '+' || !*++s)
879
			malformed();
906
			malformed();
880
		p_newfirst = (LINENUM) atol(s);
907
		p_newfirst = (LINENUM) strtol(s, 0, 0);
908
		if (errno == ERANGE)
909
			fatal("at line %ld: bad line number %s",
910
				p_input_line, s);
881
		while (isdigit((unsigned char)*s))
911
		while (isdigit((unsigned char)*s))
882
			s++;
912
			s++;
883
		if (*s == ',') {
913
		if (*s == ',') {
884
			p_repl_lines = (LINENUM) atol(++s);
914
			p_repl_lines = (LINENUM) strtol(++s, 0, 0);
915
			if (errno == ERANGE)
916
				fatal("at line %ld: bad line number %s",
917
					p_input_line, s);
885
			while (isdigit((unsigned char)*s))
918
			while (isdigit((unsigned char)*s))
886
				s++;
919
				s++;
887
		} else
920
		} else
Lines 890-895 hunk_done: Link Here
890
			s++;
923
			s++;
891
		if (*s != '@')
924
		if (*s != '@')
892
			malformed();
925
			malformed();
926
		if (p_first >= LINENUM_MAX - p_ptrn_lines ||
927
			p_newfirst > LINENUM_MAX - p_repl_lines ||
928
			p_ptrn_lines >= LINENUM_MAX - p_repl_lines - 1)
929
			malformed();
893
		if (!p_ptrn_lines)
930
		if (!p_ptrn_lines)
894
			p_first++;	/* do append rather than insert */
931
			p_first++;	/* do append rather than insert */
895
		p_max = p_ptrn_lines + p_repl_lines + 1;
932
		p_max = p_ptrn_lines + p_repl_lines + 1;
Lines 1029-1063 hunk_done: Link Here
1029
			next_intuit_at(line_beginning, p_input_line);
1066
			next_intuit_at(line_beginning, p_input_line);
1030
			return false;
1067
			return false;
1031
		}
1068
		}
1032
		p_first = (LINENUM) atol(buf);
1069
		p_first = (LINENUM) strtol(buf, 0, 0);
1070
		if (errno == ERANGE)
1071
			fatal("at line %ld: bad line number %s",
1072
				p_input_line, buf);
1033
		for (s = buf; isdigit((unsigned char)*s); s++)
1073
		for (s = buf; isdigit((unsigned char)*s); s++)
1034
			;
1074
			;
1035
		if (*s == ',') {
1075
		if (*s == ',') {
1036
			p_ptrn_lines = (LINENUM) atol(++s) - p_first + 1;
1076
			p_ptrn_lines = (LINENUM) strtol(++s, 0,
1077
				0) - p_first + 1;
1078
			if (errno == ERANGE)
1079
				malformed(); 
1037
			while (isdigit((unsigned char)*s))
1080
			while (isdigit((unsigned char)*s))
1038
				s++;
1081
				s++;
1039
		} else
1082
		} else
1040
			p_ptrn_lines = (*s != 'a');
1083
			p_ptrn_lines = (*s != 'a');
1084
		if (p_first >= LINENUM_MAX - p_ptrn_lines)
1085
			malformed();
1041
		hunk_type = *s;
1086
		hunk_type = *s;
1042
		if (hunk_type == 'a')
1087
		if (hunk_type == 'a')
1043
			p_first++;	/* do append rather than insert */
1088
			p_first++;	/* do append rather than insert */
1044
		min = (LINENUM) atol(++s);
1089
		min = (LINENUM) strtol(++s, 0, 0);
1090
		if (errno == ERANGE)
1091
			malformed();
1045
		for (; isdigit((unsigned char)*s); s++)
1092
		for (; isdigit((unsigned char)*s); s++)
1046
			;
1093
			;
1047
		if (*s == ',')
1094
		if (*s == ',') {
1048
			max = (LINENUM) atol(++s);
1095
			max = (LINENUM) strtol(++s, 0, 0);
1049
		else
1096
			if (errno == ERANGE)
1097
				malformed();
1098
		} else
1050
			max = min;
1099
			max = min;
1100
		if (min < 0 || min > max || max - min == LINENUM_MAX)
1101
			malformed();
1051
		if (hunk_type == 'd')
1102
		if (hunk_type == 'd')
1052
			min++;
1103
			min++;
1053
		p_end = p_ptrn_lines + 1 + max - min + 1;
1104
		p_newfirst = min;
1105
		p_repl_lines = max - min + 1;
1106
		if (p_newfirst > LINENUM_MAX - p_repl_lines ||
1107
			p_ptrn_lines >= LINENUM_MAX - p_repl_lines - 1)
1108
			malformed();
1109
		p_end = p_ptrn_lines + p_repl_lines + 1;
1054
		if (p_end > MAXHUNKSIZE)
1110
		if (p_end > MAXHUNKSIZE)
1055
			fatal("hunk too large (%ld lines) at line %ld: %s",
1111
			fatal("hunk too large (%ld lines) at line %ld: %s",
1056
			    p_end, p_input_line, buf);
1112
			    p_end, p_input_line, buf);
1057
		while (p_end >= hunkmax)
1113
		while (p_end >= hunkmax)
1058
			grow_hunkmax();
1114
			grow_hunkmax();
1059
		p_newfirst = min;
1060
		p_repl_lines = max - min + 1;
1061
		snprintf(buf, buf_size, "*** %ld,%ld\n", p_first,
1115
		snprintf(buf, buf_size, "*** %ld,%ld\n", p_first,
1062
		    p_first + p_ptrn_lines - 1);
1116
		    p_first + p_ptrn_lines - 1);
1063
		p_line[0] = savestr(buf);
1117
		p_line[0] = savestr(buf);

Return to bug 195436