{"thread":{"id":"36416","subject":"[PATCH 0/2] Check for lock failures early","startedAt":"2014-04-15T23:46:46Z","lastAt":"2014-04-16T01:37:29Z","messageCount":4,"participants":["Ronnie Sahlberg","Brandon Casey"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"238910","messageId":"1397605608-12128-1-git-send-email-sahlberg@google.com","threadId":"36416","inReplyTo":null,"subject":"[PATCH 0/2] Check for lock failures early","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-04-15T23:46:46Z","receivedAt":"2014-04-15T23:46:46Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Callers outside of refs.c use either lock_ref_sha1() or \nlock_any_ref_for_update() to lock a ref during an update.\n\nTwo of these places we do not immediately check the lock for failure\nmaking reading the code harder.\n\nOne place we do some unrelated string manipulation fucntions before we\ncheck for failure and the other place we rely on that write_ref_sha1()\nwill check the lock for failure and return an error.\n\n\nThese two patches updates these two places so that we immediately check the\nlock for failure and act on it.\nIt does not change any functionality or logic but makes the code easier to\nread by being more consistent.\n\n\nRonnie Sahlberg (2):\n  sequencer.c: check for lock failure and bail early in fast_forward_to\n  commit.c: check for lock error and return early\n\n builtin/commit.c | 8 ++++----\n sequencer.c      | 7 +++++++\n 2 files changed, 11 insertions(+), 4 deletions(-)\n\n-- \n1.9.1.503.ge4c3920.dirty\n"},{"id":"238911","messageId":"1397605608-12128-2-git-send-email-sahlberg@google.com","threadId":"36416","inReplyTo":"1397605608-12128-1-git-send-email-sahlberg@google.com","subject":"[PATCH 1/2] sequencer.c: check for lock failure and bail early in fast_forward_to","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-04-15T23:46:47Z","receivedAt":"2014-04-15T23:46:47Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Change fast_forward_to() to check if locking the ref failed, print a nice\nerror message and bail out early.\nThe old code did not check if ref_lock was NULL and relied on the fact\nthat the write_ref_sha1() would safely detect this condition and set the\nreturn variable ret to indicate an error.\nWhile that is safe, it makes the code harder to read for two reasons:\n* Inconsistency.  Almost all other places we do check the lock for NULL\n  explicitely, so the naive reader is confused \"why don't we check here\".\n* And relying on write_ref_sha1() to detect and return an error for when\n  a previous lock_any_ref_for_update() feels obfuscated.\n\nThis change should not change any functionality or logic\naside from adding an extra error message when this condition is triggered.\n(write_ref_sha1() returns an error silently for this condition)\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n sequencer.c | 7 +++++++\n 1 file changed, 7 insertions(+)\n\ndiff --git a/sequencer.c b/sequencer.c\nindex bde5f04..6aa3b50 100644\n--- a/sequencer.c\n+++ b/sequencer.c\n@@ -281,8 +281,15 @@ static int fast_forward_to(const unsigned char *to, const unsigned char *from,\n \t\texit(1); /* the callee should have complained already */\n \tref_lock = lock_any_ref_for_update(\"HEAD\", unborn ? null_sha1 : from,\n \t\t\t\t\t   0, NULL);\n+\tif (!ref_lock) {\n+\t\tret = error(_(\"Failed to lock HEAD during fast_forward_to\"));\n+\t\tgoto leave;\n+\t}\n+\n \tstrbuf_addf(&sb, \"%s: fast-forward\", action_name(opts));\n \tret = write_ref_sha1(ref_lock, to, sb.buf);\n+\n+leave:\n \tstrbuf_release(&sb);\n \treturn ret;\n }\n-- \n1.9.1.503.ge4c3920.dirty\n"},{"id":"238912","messageId":"1397605608-12128-3-git-send-email-sahlberg@google.com","threadId":"36416","inReplyTo":"1397605608-12128-1-git-send-email-sahlberg@google.com","subject":"[PATCH 2/2] commit.c: check for lock error and return early","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-04-15T23:46:48Z","receivedAt":"2014-04-15T23:46:48Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Move the check for the lock failure to happen immediately after\nlock_any_ref_for_update().\nPreviously the lock and the check-if-lock-failed was separated by a handful\nof string manipulation statements.\n\nMoving the check to occur immediately after the failed lock makes the\ncode slightly easier to read and makes it follow the pattern of\n try-to-take-a-lock()\n if (check-if-lock-failed){\n    error\n }\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n builtin/commit.c | 8 ++++----\n 1 file changed, 4 insertions(+), 4 deletions(-)\n\ndiff --git a/builtin/commit.c b/builtin/commit.c\nindex d9550c5..c6320f1 100644\n--- a/builtin/commit.c\n+++ b/builtin/commit.c\n@@ -1672,6 +1672,10 @@ int cmd_commit(int argc, const char **argv, const char *prefix)\n \t\t\t\t\t   ? NULL\n \t\t\t\t\t   : current_head->object.sha1,\n \t\t\t\t\t   0, NULL);\n+\tif (!ref_lock) {\n+\t\trollback_index_files();\n+\t\tdie(_(\"cannot lock HEAD ref\"));\n+\t}\n \n \tnl = strchr(sb.buf, '\\n');\n \tif (nl)\n@@ -1681,10 +1685,6 @@ int cmd_commit(int argc, const char **argv, const char *prefix)\n \tstrbuf_insert(&sb, 0, reflog_msg, strlen(reflog_msg));\n \tstrbuf_insert(&sb, strlen(reflog_msg), \": \", 2);\n \n-\tif (!ref_lock) {\n-\t\trollback_index_files();\n-\t\tdie(_(\"cannot lock HEAD ref\"));\n-\t}\n \tif (write_ref_sha1(ref_lock, sha1, sb.buf) < 0) {\n \t\trollback_index_files();\n \t\tdie(_(\"cannot update HEAD ref\"));\n-- \n1.9.1.503.ge4c3920.dirty\n"},{"id":"238914","messageId":"CA+sFfMe1SB_Njpgq5VJQ3B-0Oh5nyJf5uf+WzzT-H6b1Cz-WRg@mail.gmail.com","threadId":"36416","inReplyTo":"1397605608-12128-2-git-send-email-sahlberg@google.com","subject":"Re: [PATCH 1/2] sequencer.c: check for lock failure and bail early in fast_forward_to","fromName":"Brandon Casey","fromEmail":"drafnel@gmail.com","sentAt":"2014-04-16T01:37:29Z","receivedAt":"2014-04-16T01:37:29Z","isPatch":true,"sender":{"key":"drafnel@gmail.com","avatar":"https://avatars.githubusercontent.com/u/921167?v=4"},"body":"On Tue, Apr 15, 2014 at 4:46 PM, Ronnie Sahlberg <sahlberg@google.com> wrote:\n\n<snip well-worded commit message>\n\n> Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>\n> ---\n>  sequencer.c | 7 +++++++\n>  1 file changed, 7 insertions(+)\n>\n> diff --git a/sequencer.c b/sequencer.c\n> index bde5f04..6aa3b50 100644\n> --- a/sequencer.c\n> +++ b/sequencer.c\n> @@ -281,8 +281,15 @@ static int fast_forward_to(const unsigned char *to, const unsigned char *from,\n>                 exit(1); /* the callee should have complained already */\n>         ref_lock = lock_any_ref_for_update(\"HEAD\", unborn ? null_sha1 : from,\n>                                            0, NULL);\n> +       if (!ref_lock) {\n> +               ret = error(_(\"Failed to lock HEAD during fast_forward_to\"));\n> +               goto leave;\n> +       }\n> +\n\nOr just:\n\n   if (!ref_lock)\n       return error(_(\"Failed to lock HEAD ...\"));\n\nWe don't need to strbuf_release() since the strbuf has not been\nmodified at this point.  We've only initialized with a static\ninitializer.\n\n>         strbuf_addf(&sb, \"%s: fast-forward\", action_name(opts));\n>         ret = write_ref_sha1(ref_lock, to, sb.buf);\n> +\n> +leave:\n>         strbuf_release(&sb);\n>         return ret;\n>  }\n> --\n\n-Brandon\n"}]}