From: Omar Polo Subject: Re: two small fixes for got patch To: gameoftrees@openbsd.org Date: Fri, 11 Mar 2022 19:22:03 +0100 thanks thomas and stsp for reviewing! I had another small tweak i'd like to commit before moving into more interesting diffs (dry run, error recovering, no final newline etc) that I forgot to include in previous mail, apologies. The following moves some checks done in apply_patch early in recv_patch so that we bail out early in case of malformed patches and changes the error for mismatching paths from % got patch < diff got: the paths mentioned in the patch are different. to % got patch < diff got: old file is "foo" and new "bar": paths are different OK? ----------------------------------------------- commit a7644c1cb79603b8de3f810911678d752c49bea2 from: Omar Polo date: Fri Mar 11 17:28:10 2022 UTC move some checks from apply_patch to recv_patch and improve error messages diff 59937e062fff6aafbbcb872633fcbd9ca6e2bf1d bbe7d371e6b251355093d9773cea760227d04297 blob - 64f2cb93558b933d2ffdcc0da7dedecf78d8ee52 blob + 996438cb363cfd0114a5dc376b56f1fa1a77a957 --- include/got_error.h +++ include/got_error.h @@ -346,8 +346,7 @@ static const struct got_error { { GOT_ERR_PATCH_MALFORMED, "malformed patch" }, { GOT_ERR_PATCH_TRUNCATED, "patch truncated" }, { GOT_ERR_PATCH_DONT_APPLY, "patch doesn't apply" }, - { GOT_ERR_PATCH_PATHS_DIFFER, "the paths mentioned in the patch " - "are different." }, + { GOT_ERR_PATCH_PATHS_DIFFER, "paths are different" }, { GOT_ERR_NO_PATCH, "no patch found" }, }; blob - adbe4b1cb254d02c8c04017209e50dd8a445f2b3 blob + dbb8084fc9fe60812af9eec1b989894266a6e83a --- lib/patch.c +++ lib/patch.c @@ -173,6 +173,17 @@ recv_patch(struct imsgbuf *ibuf, int *done, struct got goto done; } + if (p->old == NULL && p->new == NULL) { + err = got_error(GOT_ERR_PATCH_MALFORMED); + goto done; + } + if (p->old != NULL && p->new != NULL && strcmp(p->old, p->new)) { + err = got_error_fmt(GOT_ERR_PATCH_PATHS_DIFFER, + "old file is \"%s\" and new \"%s\"", + p->old, p->new); + goto done; + } + imsg_free(&imsg); for (;;) { @@ -411,9 +422,6 @@ apply_patch(struct got_worktree *worktree, struct got_ TAILQ_INIT(&paths); - if (p->old == NULL && p->new == NULL) - return got_error(GOT_ERR_PATCH_MALFORMED); - err = got_worktree_resolve_path(&path, worktree, p->new != NULL ? p->new : p->old); if (err) @@ -439,9 +447,6 @@ apply_patch(struct got_worktree *worktree, struct got_ err = got_worktree_schedule_delete(worktree, &paths, 0, NULL, delete_cb, delete_arg, repo, 0, 0); goto done; - } else if (p->old != NULL && strcmp(p->old, p->new)) { - err = got_error(GOT_ERR_PATCH_PATHS_DIFFER); - goto done; } /* can't add known files or modify unknown ones */