"GOT", but the "O" is a cute, smiling pufferfish. Index | Thread | Search

From:
Omar Polo <op@omarpolo.com>
Subject:
introduce got_error_checksum
To:
gameoftrees@openbsd.org
Date:
Fri, 03 Feb 2023 11:53:36 +0100

Download raw body.

Thread
we have a few instance of got_error_fmt(GOT_ERR_OBJ_CSUM) with the
same error string so it'd made sense to add an helper function.

diffstat /home/op/w/got
 M  include/got_error.h                    |   8+   0-
 M  lib/error.c                            |  18+   0-
 M  lib/object_parse.c                     |   4+  20-
 M  libexec/got-read-blob/got-read-blob.c  |   1+   5-

4 files changed, 31 insertions(+), 25 deletions(-)

diff /home/op/w/got
commit - 09cbb981df84d9c12f0fa371cb6b85d3f1b615c0
path + /home/op/w/got
blob - 3fd06efcdadb789fef14e683086013ed87d5bfd9
file + include/got_error.h
--- include/got_error.h
+++ include/got_error.h
@@ -261,6 +261,14 @@ const struct got_error *got_error_no_obj(struct got_ob
 const struct got_error *got_error_no_obj(struct got_object_id *);
 
 /*
+ * Obtain an error with code GOT_ERR_OBJ_CSUM and an error message which
+ * contains the specified object ID. The message buffer is statically
+ * allocated; future invocations of this function will overwrite the
+ * message set during earlier invocations.
+ */
+const struct got_error *got_error_checksum(struct got_object_id *);
+
+/*
  * Obtain an error with code GOT_ERR_NOT_REF and an error message which
  * contains the specified reference name. The message buffer is statically
  * allocated; future invocations of this function will overwrite the
blob - a63dabdbb6ea9f180683f4d148b335902323042d (staged)
file + lib/error.c
--- lib/error.c
+++ lib/error.c
@@ -382,6 +382,24 @@ got_error_not_ref(const char *refname)
 }
 
 const struct got_error *
+got_error_checksum(struct got_object_id *id)
+{
+	char id_str[SHA1_DIGEST_STRING_LENGTH];
+	char msg[sizeof("checksum failure for object ") + sizeof(id_str)];
+	int ret;
+
+	if (!got_object_id_serialize(id, id_str, sizeof(id_str)))
+		return got_error(GOT_ERR_OBJ_CSUM);
+
+	ret = snprintf(msg, sizeof(msg), "checksum failure for object %s",
+	    id_str);
+	if (ret < 0 || (size_t)ret >= sizeof(msg))
+		return got_error(GOT_ERR_OBJ_CSUM);
+
+	return got_error_msg(GOT_ERR_OBJ_CSUM, msg);
+}
+
+const struct got_error *
 got_error_not_ref(const char *refname)
 {
 	char msg[sizeof("reference   not found") + 1004];
blob - 27d66bb8f8798c76dc4f448a3c48249d762a5f98 (staged)
file + lib/object_parse.c
--- lib/object_parse.c
+++ lib/object_parse.c
@@ -343,11 +343,7 @@ got_object_read_raw(uint8_t **outbuf, off_t *size, siz
 
 	SHA1Final(sha1, &sha1_ctx);
 	if (memcmp(expected_id->sha1, sha1, SHA1_DIGEST_LENGTH) != 0) {
-		char buf[SHA1_DIGEST_STRING_LENGTH];
-		err = got_error_fmt(GOT_ERR_OBJ_CSUM,
-		    "checksum failure for object %s",
-		    got_sha1_digest_to_str(expected_id->sha1, buf,
-		    sizeof(buf)));
+		err = got_error_checksum(expected_id);
 		goto done;
 	}
 
@@ -766,11 +762,7 @@ got_object_read_commit(struct got_commit_object **comm
 
 	SHA1Final(id.sha1, &sha1_ctx);
 	if (got_object_id_cmp(expected_id, &id) != 0) {
-		char buf[SHA1_DIGEST_STRING_LENGTH];
-		err = got_error_fmt(GOT_ERR_OBJ_CSUM,
-		    "checksum failure for object %s",
-		    got_sha1_digest_to_str(expected_id->sha1, buf,
-		    sizeof(buf)));
+		err = got_error_checksum(expected_id);
 		goto done;
 	}
 
@@ -939,11 +931,7 @@ got_object_read_tree(struct got_parsed_tree_entry **en
 
 	SHA1Final(id.sha1, &sha1_ctx);
 	if (got_object_id_cmp(expected_id, &id) != 0) {
-		char buf[SHA1_DIGEST_STRING_LENGTH];
-		err = got_error_fmt(GOT_ERR_OBJ_CSUM,
-		    "checksum failure for object %s",
-		    got_sha1_digest_to_str(expected_id->sha1, buf,
-		    sizeof(buf)));
+		err = got_error_checksum(expected_id);
 		goto done;
 	}
 
@@ -1183,11 +1171,7 @@ got_object_read_tag(struct got_tag_object **tag, int f
 
 	SHA1Final(id.sha1, &sha1_ctx);
 	if (got_object_id_cmp(expected_id, &id) != 0) {
-		char buf[SHA1_DIGEST_STRING_LENGTH];
-		err = got_error_fmt(GOT_ERR_OBJ_CSUM,
-		    "checksum failure for object %s",
-		    got_sha1_digest_to_str(expected_id->sha1, buf,
-		    sizeof(buf)));
+		err = got_error_checksum(expected_id);
 		goto done;
 	}
 
blob - 0f7e7ecca20145a10ab4146b77499313ccdd89ed
file + libexec/got-read-blob/got-read-blob.c
--- libexec/got-read-blob/got-read-blob.c
+++ libexec/got-read-blob/got-read-blob.c
@@ -172,11 +172,7 @@ main(int argc, char *argv[])
 		}
 		SHA1Final(id.sha1, &sha1_ctx);
 		if (got_object_id_cmp(&expected_id, &id) != 0) {
-			char buf[SHA1_DIGEST_STRING_LENGTH];
-			err = got_error_fmt(GOT_ERR_OBJ_CSUM,
-			    "checksum failure for object %s",
-			    got_sha1_digest_to_str(expected_id.sha1, buf,
-			    sizeof(buf)));
+			err = got_error_checksum(&expected_id);
 			goto done;
 		}