commit 3c23f6cdb61cc63d6fd9ecbcc015cd1347793eb1 from: Omar Polo date: Sat Feb 04 07:41:11 2023 UTC introduce got_error_checksum ok stsp@ commit - bbc740ac4905ed0d4ba6334f6225c8f8c9f3c394 commit + 3c23f6cdb61cc63d6fd9ecbcc015cd1347793eb1 blob - 3fd06efcdadb789fef14e683086013ed87d5bfd9 blob + f6291e8407d22bd8dea75ff94f8f7b88ebafcb78 --- include/got_error.h +++ include/got_error.h @@ -259,6 +259,14 @@ const struct got_error *got_ferror(FILE *, int); */ struct got_object_id; /* forward declaration */ 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 blob - 95d7ae9cb011916744442c02343dafa9e4901bb1 blob + 041a41fbd209cbb7d7131b52327e59668eea4f41 --- lib/error.c +++ lib/error.c @@ -379,6 +379,24 @@ got_error_no_obj(struct got_object_id *id) return got_error(GOT_ERR_NO_OBJ); return got_error_msg(GOT_ERR_NO_OBJ, msg); +} + +const struct got_error * +got_error_checksum(struct got_object_id *id) +{ + char id_str[GOT_OBJECT_ID_HEX_MAXLEN]; + char msg[sizeof("checksum failure for object ") + sizeof(id_str)]; + int ret; + + if (!got_object_id_hex(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 * blob - b17d5a9b339c525ac55deed48e604e2b049d96ca blob + 52fd48d81fd6b40f63ea1b5b40edbb3c8e40b3a2 --- 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 blob + 839a953c4f820616b747cbd3fcca9350a558fc68 --- libexec/got-read-blob/got-read-blob.c +++ libexec/got-read-blob/got-read-blob.c @@ -38,7 +38,6 @@ #include "got_lib_object.h" #include "got_lib_object_parse.h" #include "got_lib_privsep.h" -#include "got_lib_sha1.h" static volatile sig_atomic_t sigint_received; @@ -172,11 +171,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; }