notes: make get_note return pointer to struct object_id

Make get_note return a pointer to a const struct object_id. Add a defensive check to ensure we don't accidentally dereference a NULL pointer. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Brandon Williams <bmwill@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

brian m. carlson committed May 30, 2017 at 10:30 UTC 9ef7223058a44990dc4650ecb1209c97ceb636b3
5 files changed +28 -28
builtin/notes.c
+11 -11
@@ -351,7 +351,7 @@ static int list(int argc, const char **argv, const char *prefix)
351 {
352 struct notes_tree *t;
353 unsigned char object[20];
354 - const unsigned char *note;
354 + const struct object_id *note;
355 int retval = -1;
356 struct option options[] = {
357 OPT_END()
@@ -372,7 +372,7 @@ static int list(int argc, const char **argv, const char *prefix)
372 die(_("failed to resolve '%s' as a valid ref."), argv[0]);
373 note = get_note(t, object);
374 if (note) {
375 - puts(sha1_to_hex(note));
375 + puts(oid_to_hex(note));
376 retval = 0;
377 } else
378 retval = error(_("no note found for object %s."),
@@ -392,7 +392,7 @@ static int add(int argc, const char **argv, const char *prefix)
392 const char *object_ref;
393 struct notes_tree *t;
394 unsigned char object[20], new_note[20];
395 - const unsigned char *note;
395 + const struct object_id *note;
396 struct note_data d = { 0, 0, NULL, STRBUF_INIT };
397 struct option options[] = {
398 { OPTION_CALLBACK, 'm', "message", &d, N_("message"),
@@ -453,7 +453,7 @@ static int add(int argc, const char **argv, const char *prefix)
453 sha1_to_hex(object));
454 }
455
456 - prepare_note_data(object, &d, note);
456 + prepare_note_data(object, &d, note->hash);
457 if (d.buf.len || allow_empty) {
458 write_note_data(&d, new_note);
459 if (add_note(t, object, new_note, combine_notes_overwrite))
@@ -474,7 +474,7 @@ static int add(int argc, const char **argv, const char *prefix)
474 static int copy(int argc, const char **argv, const char *prefix)
475 {
476 int retval = 0, force = 0, from_stdin = 0;
477 - const unsigned char *from_note, *note;
477 + const struct object_id *from_note, *note;
478 const char *object_ref;
479 unsigned char object[20], from_obj[20];
480 struct notes_tree *t;
@@ -539,7 +539,7 @@ static int copy(int argc, const char **argv, const char *prefix)
539 goto out;
540 }
541
542 - if (add_note(t, object, from_note, combine_notes_overwrite))
542 + if (add_note(t, object, from_note->hash, combine_notes_overwrite))
543 die("BUG: combine_notes_overwrite failed");
544 commit_notes(t, "Notes added by 'git notes copy'");
545 out:
@@ -553,7 +553,7 @@ static int append_edit(int argc, const char **argv, const char *prefix)
553 const char *object_ref;
554 struct notes_tree *t;
555 unsigned char object[20], new_note[20];
556 - const unsigned char *note;
556 + const struct object_id *note;
557 char *logmsg;
558 const char * const *usage;
559 struct note_data d = { 0, 0, NULL, STRBUF_INIT };
@@ -598,13 +598,13 @@ static int append_edit(int argc, const char **argv, const char *prefix)
598 t = init_notes_check(argv[0], NOTES_INIT_WRITABLE);
599 note = get_note(t, object);
600
601 - prepare_note_data(object, &d, edit ? note : NULL);
601 + prepare_note_data(object, &d, edit && note ? note->hash : NULL);
602
603 if (note && !edit) {
604 /* Append buf to previous note contents */
605 unsigned long size;
606 enum object_type type;
607 - char *prev_buf = read_sha1_file(note, &type, &size);
607 + char *prev_buf = read_sha1_file(note->hash, &type, &size);
608
609 strbuf_grow(&d.buf, size + 1);
610 if (d.buf.len && prev_buf && size)
@@ -638,7 +638,7 @@ static int show(int argc, const char **argv, const char *prefix)
638 const char *object_ref;
639 struct notes_tree *t;
640 unsigned char object[20];
641 - const unsigned char *note;
641 + const struct object_id *note;
642 int retval;
643 struct option options[] = {
644 OPT_END()
@@ -664,7 +664,7 @@ static int show(int argc, const char **argv, const char *prefix)
664 retval = error(_("no note found for object %s."),
665 sha1_to_hex(object));
666 else {
667 - const char *show_args[3] = {"show", sha1_to_hex(note), NULL};
667 + const char *show_args[3] = {"show", oid_to_hex(note), NULL};
668 retval = execv_git_cmd(show_args);
669 }
670 free_notes(t);
notes-cache.c
+4 -4
@@ -69,15 +69,15 @@ int notes_cache_write(struct notes_cache *c)
69 char *notes_cache_get(struct notes_cache *c, struct object_id *key_oid,
70 size_t *outsize)
71 {
72 - const unsigned char *value_sha1;
72 + const struct object_id *value_oid;
73 enum object_type type;
74 char *value;
75 unsigned long size;
76
77 - value_sha1 = get_note(&c->tree, key_oid->hash);
78 - if (!value_sha1)
77 + value_oid = get_note(&c->tree, key_oid->hash);
78 + if (!value_oid)
79 return NULL;
80 - value = read_sha1_file(value_sha1, &type, &size);
80 + value = read_sha1_file(value_oid->hash, &type, &size);
81
82 *outsize = size;
83 return value;
notes.c
+9 -9
@@ -1119,7 +1119,7 @@ int remove_note(struct notes_tree *t, const unsigned char *object_sha1)
1119 return 0;
1120 }
1121
1122 -const unsigned char *get_note(struct notes_tree *t,
1122 +const struct object_id *get_note(struct notes_tree *t,
1123 const unsigned char *object_sha1)
1124 {
1125 struct leaf_node *found;
@@ -1128,7 +1128,7 @@ const unsigned char *get_note(struct notes_tree *t,
1128 t = &default_notes_tree;
1129 assert(t->initialized);
1130 found = note_tree_find(t, t->root, 0, object_sha1);
1131 - return found ? found->val_oid.hash : NULL;
1131 + return found ? &found->val_oid : NULL;
1132 }
1133
1134 int for_each_note(struct notes_tree *t, int flags, each_note_fn fn,
@@ -1219,7 +1219,7 @@ static void format_note(struct notes_tree *t, const unsigned char *object_sha1,
1219 struct strbuf *sb, const char *output_encoding, int raw)
1220 {
1221 static const char utf8[] = "utf-8";
1222 - const unsigned char *sha1;
1222 + const struct object_id *oid;
1223 char *msg, *msg_p;
1224 unsigned long linelen, msglen;
1225 enum object_type type;
@@ -1229,11 +1229,11 @@ static void format_note(struct notes_tree *t, const unsigned char *object_sha1,
1229 if (!t->initialized)
1230 init_notes(t, NULL, NULL, 0);
1231
1232 - sha1 = get_note(t, object_sha1);
1233 - if (!sha1)
1232 + oid = get_note(t, object_sha1);
1233 + if (!oid)
1234 return;
1235
1236 - if (!(msg = read_sha1_file(sha1, &type, &msglen)) || type != OBJ_BLOB) {
1236 + if (!(msg = read_sha1_file(oid->hash, &type, &msglen)) || type != OBJ_BLOB) {
1237 free(msg);
1238 return;
1239 }
@@ -1291,14 +1291,14 @@ int copy_note(struct notes_tree *t,
1291 const unsigned char *from_obj, const unsigned char *to_obj,
1292 int force, combine_notes_fn combine_notes)
1293 {
1294 - const unsigned char *note = get_note(t, from_obj);
1295 - const unsigned char *existing_note = get_note(t, to_obj);
1294 + const struct object_id *note = get_note(t, from_obj);
1295 + const struct object_id *existing_note = get_note(t, to_obj);
1296
1297 if (!force && existing_note)
1298 return 1;
1299
1300 if (note)
1301 - return add_note(t, to_obj, note, combine_notes);
1301 + return add_note(t, to_obj, note->hash, combine_notes);
1302 else if (existing_note)
1303 return add_note(t, to_obj, null_sha1, combine_notes);
1304
notes.h
+1 -1
@@ -140,7 +140,7 @@ int remove_note(struct notes_tree *t, const unsigned char *object_sha1);
140 *
141 * Return NULL if the given object has no notes.
142 */
143 -const unsigned char *get_note(struct notes_tree *t,
143 +const struct object_id *get_note(struct notes_tree *t,
144 const unsigned char *object_sha1);
145
146 /*
remote-testsvn.c
+3 -3
@@ -53,15 +53,15 @@ static void terminate_batch(void)
53 /* NOTE: 'ref' refers to a git reference, while 'rev' refers to a svn revision. */
54 static char *read_ref_note(const unsigned char sha1[20])
55 {
56 - const unsigned char *note_sha1;
56 + const struct object_id *note_oid;
57 char *msg = NULL;
58 unsigned long msglen;
59 enum object_type type;
60
61 init_notes(NULL, notes_ref, NULL, 0);
62 - if (!(note_sha1 = get_note(NULL, sha1)))
62 + if (!(note_oid = get_note(NULL, sha1)))
63 return NULL; /* note tree not found */
64 - if (!(msg = read_sha1_file(note_sha1, &type, &msglen)))
64 + if (!(msg = read_sha1_file(note_oid->hash, &type, &msglen)))
65 error("Empty notes tree. %s", notes_ref);
66 else if (!msglen || type != OBJ_BLOB) {
67 error("Note contains unusable content. "