refs: prevent accidental NULL dereference in write_pseudoref
authorbrian m. carlson <sandals@crustytoothpaste.net>
Sun, 15 Oct 2017 22:06:52 +0000 (22:06 +0000)
committerJunio C Hamano <gitster@pobox.com>
Mon, 16 Oct 2017 02:05:50 +0000 (11:05 +0900)
Several of the refs functions take NULL to indicate that the ref is not
to be updated. If refs_update_ref were called with a NULL new object
ID, we could pass that NULL pointer to write_pseudoref, which would then
segfault when it dereferenced it. Instead, simply return successfully,
since if we don't want to update the pseudoref, there's nothing to do.

Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
refs.c
diff --git a/refs.c b/refs.c
index edd20044c638ad1d186be6e935540117a5ed00f2..91c2af78b627ef242bb61f8853c6dd30b2551b66 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -583,6 +583,9 @@ static int write_pseudoref(const char *pseudoref, const struct object_id *oid,
        struct strbuf buf = STRBUF_INIT;
        int ret = -1;
 
+       if (!oid)
+               return 0;
+
        strbuf_addf(&buf, "%s\n", oid_to_hex(oid));
 
        filename = git_path("%s", pseudoref);