Commit Diff


commit - 15e1bda6b8ce20b9acfeeefabf9d3aee2589d0d4
commit + b28327780dc2e32f03051c7d18d57db2e27e2de6
blob - adf9459c88ef262d9070733fe62620364926f5db
blob + f34e5618f478d6c30b6b9e649007396e435e8be2
--- lib/patch.c
+++ lib/patch.c
@@ -55,7 +55,8 @@ struct got_patch_hunk {
 	STAILQ_ENTRY(got_patch_hunk) entries;
 	const struct got_error *err;
 	long	offset;
-	int	nonl;
+	int	old_nonl;
+	int	new_nonl;
 	long	old_from;
 	long	old_lines;
 	long	new_from;
@@ -152,6 +153,7 @@ recv_patch(struct imsgbuf *ibuf, int *done, struct got
 	struct got_imsg_patch patch;
 	struct got_patch_hunk *h = NULL;
 	size_t datalen;
+	int lastmode = -1;
 
 	memset(p, 0, sizeof(*p));
 	STAILQ_INIT(&p->head);
@@ -215,10 +217,11 @@ recv_patch(struct imsgbuf *ibuf, int *done, struct got
 		case GOT_IMSG_PATCH_DONE:
 			goto done;
 		case GOT_IMSG_PATCH_HUNK:
-			if (h != NULL && h->nonl) {
+			if (h != NULL && (h->old_nonl || h->new_nonl)) {
 				err = got_error(GOT_ERR_PATCH_MALFORMED);
 				goto done;
 			}
+			lastmode = -1;
 			datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
 			if (datalen != sizeof(hdr)) {
 				err = got_error(GOT_ERR_PRIVSEP_LEN);
@@ -252,14 +255,20 @@ recv_patch(struct imsgbuf *ibuf, int *done, struct got
 				err = got_error(GOT_ERR_PRIVSEP_MSG);
 				goto done;
 			}
-			if (h->nonl)
-				err = got_error(GOT_ERR_PATCH_MALFORMED);
-			if (*t == '\\')
-				h->nonl = 1;
-			else
+
+			if (*t != '\\')
 				err = pushline(h, t);
+			else if (lastmode == '-')
+				h->old_nonl = 1;
+			else if (lastmode == '+')
+				h->new_nonl = 1;
+			else
+				err = got_error(GOT_ERR_PATCH_MALFORMED);
+
 			if (err)
 				goto done;
+
+			lastmode = *t;
 			break;
 		default:
 			err = got_error(GOT_ERR_PRIVSEP_MSG);
@@ -399,7 +408,7 @@ done:
 static const struct got_error *
 apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
 {
-	size_t i = 0;
+	size_t i, new = 0;
 
 	for (i = 0; i < h->len; ++i) {
 		switch (*h->lines[i]) {
@@ -411,9 +420,10 @@ apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *
 			(*lineno)++;
 			break;
 		case '+':
+			new++;
 			if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
 				return got_error_from_errno("fprintf");
-			if (i != h->len - 1 || !h->nonl) {
+			if (new != h->new_lines || !h->new_nonl) {
 				if (fprintf(tmp, "\n") < 0)
 					return got_error_from_errno(
 					    "fprintf");
blob - b72f7f88829c3931ef203cba5d632edc4dc06195
blob + 879b77f8142d65729f38b9389c48cf5a78299027
--- libexec/got-read-patch/got-read-patch.c
+++ libexec/got-read-patch/got-read-patch.c
@@ -288,7 +288,7 @@ send_line(const char *line)
 }
 
 static const struct got_error *
-peek_special_line(FILE *fp, int send)
+peek_special_line(FILE *fp)
 {
 	const struct got_error *err;
 	int ch;
@@ -299,7 +299,7 @@ peek_special_line(FILE *fp, int send)
 		return NULL;
 	}
 
-	if (ch == '\\' && send) {
+	if (ch == '\\') {
 		err = send_line("\\");
 		if (err)
 			return err;
@@ -396,7 +396,7 @@ parse_hunk(FILE *fp, int *ok)
 
 		if ((ch == '-' && leftold == 0) ||
 		    (ch == '+' && leftnew == 0)) {
-			err = peek_special_line(fp, ch == '+');
+			err = peek_special_line(fp);
 			if (err)
 				goto done;
 		}