commit dc43cdc9c619cfe64774791cffda578d9c0f6e8d from: Omar Polo via: Thomas Adam date: Tue Feb 07 20:14:55 2023 UTC introduce got_error_checksum ok stsp@ commit - 01986ce9001e81cf428f546081a6888c518a54cc commit + dc43cdc9c619cfe64774791cffda578d9c0f6e8d blob - 3f1873d64f1c63ef2dc44534ddb9874a56c23447 blob + afcfed5fde3a472bae8c325446833512579d1125 --- include/got_error.h +++ include/got_error.h @@ -261,6 +261,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 - 31a8aca5dcebc80f345a950a87d260a721e8e729 blob + fbfae58d5ac8823e5199dccfa6a0178dcb39965c --- lib/error.c +++ lib/error.c @@ -388,6 +388,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 - 6fc646eb18e40a9ccd26d08703df008856322e4e blob + 7ddc0d0f970d44918913e45dc16467a1281efd26 --- lib/object_parse.c +++ lib/object_parse.c @@ -342,11 +342,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; } @@ -765,11 +761,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; } @@ -938,11 +930,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; } @@ -1182,11 +1170,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 - 5fa0499e9e024bcf6a7a8371640bb25e6032e5c5 blob + 278ffbba28800315d824bf942dd2a401bd045cb6 --- 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; @@ -184,11 +183,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; }