commit 656c2baa8669bb412dbff580940fa9ccd8bd1f1f from: Omar Polo via: Thomas Adam date: Sat Apr 23 20:32:50 2022 UTC got-read-patch: preserve all \ lines as a cheap optimization got-read-patch was sending only the "\ No newline at end of file" lines that follows an addition (a "+" line). To be able to reverse patches in the future got_patch needs to know about all of these lines instead. No functional changes intended. ok stsp@ commit - cfbf55314def56a649722267c3ced6d7a2217cc5 commit + 656c2baa8669bb412dbff580940fa9ccd8bd1f1f blob - 9e90ab3973d15779dd8386d2fbf98d75958d17a2 blob + dc6334a1be3f413bfb06cfe4baceb43f78f17d9b --- lib/patch.c +++ lib/patch.c @@ -52,7 +52,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; @@ -149,6 +150,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); @@ -212,10 +214,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); @@ -249,14 +252,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); @@ -396,7 +405,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]) { @@ -408,9 +417,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 - a02d83bf9c79d0605368317e57b9019b5bf59ee6 blob + fc2a6b831e2b155e6d48a9bd8de4bf8787c1ff3f --- libexec/got-read-patch/got-read-patch.c +++ libexec/got-read-patch/got-read-patch.c @@ -287,7 +287,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; @@ -298,7 +298,7 @@ peek_special_line(FILE *fp, int send) return NULL; } - if (ch == '\\' && send) { + if (ch == '\\') { err = send_line("\\"); if (err) return err; @@ -395,7 +395,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; }