apply: replace hard-coded constants
Replace several 40-based constants with references to GIT_MAX_HEXSZ or the_hash_algo, as appropriate. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
brian m. carlson committed
Oct 15, 2018 at 00:01 UTC
93eb00f719a2d36d31a4ba7b6eeae9f6302314e9
1 file changed
+10
-8
apply.c
+10
-8
@@ -223,8 +223,8 @@ struct patch {
223
struct fragment *fragments;
224
char *result;
225
size_t resultsize;
226
- char old_sha1_prefix[41];
227
- char new_sha1_prefix[41];
226
+ char old_sha1_prefix[GIT_MAX_HEXSZ + 1];
227
+ char new_sha1_prefix[GIT_MAX_HEXSZ + 1];
228
struct patch *next;
229
230
/* three-way fallback result */
@@ -1093,9 +1093,10 @@ static int gitdiff_index(struct apply_state *state,
1093
*/
1094
const char *ptr, *eol;
1095
int len;
1096
+ const unsigned hexsz = the_hash_algo->hexsz;
1097
1098
ptr = strchr(line, '.');
1098
- if (!ptr || ptr[1] != '.' || 40 < ptr - line)
1099
+ if (!ptr || ptr[1] != '.' || hexsz < ptr - line)
1100
return 0;
1101
len = ptr - line;
1102
memcpy(patch->old_sha1_prefix, line, len);
@@ -1109,7 +1110,7 @@ static int gitdiff_index(struct apply_state *state,
1110
ptr = eol;
1111
len = ptr - line;
1112
1112
- if (40 < len)
1113
+ if (hexsz < len)
1114
return 0;
1115
memcpy(patch->new_sha1_prefix, line, len);
1116
patch->new_sha1_prefix[len] = 0;
@@ -3142,13 +3143,14 @@ static int apply_binary(struct apply_state *state,
3143
{
3144
const char *name = patch->old_name ? patch->old_name : patch->new_name;
3145
struct object_id oid;
3146
+ const unsigned hexsz = the_hash_algo->hexsz;
3147
3148
/*
3149
* For safety, we require patch index line to contain
3148
- * full 40-byte textual SHA1 for old and new, at least for now.
3150
+ * full hex textual object ID for old and new, at least for now.
3151
*/
3150
- if (strlen(patch->old_sha1_prefix) != 40 ||
3151
- strlen(patch->new_sha1_prefix) != 40 ||
3152
+ if (strlen(patch->old_sha1_prefix) != hexsz ||
3153
+ strlen(patch->new_sha1_prefix) != hexsz ||
3154
get_oid_hex(patch->old_sha1_prefix, &oid) ||
3155
get_oid_hex(patch->new_sha1_prefix, &oid))
3156
return error(_("cannot apply binary patch to '%s' "
@@ -4055,7 +4057,7 @@ static int preimage_oid_in_gitlink_patch(struct patch *p, struct object_id *oid)
4057
starts_with(++preimage, heading) &&
4058
/* does it record full SHA-1? */
4059
!get_oid_hex(preimage + sizeof(heading) - 1, oid) &&
4058
- preimage[sizeof(heading) + GIT_SHA1_HEXSZ - 1] == '\n' &&
4060
+ preimage[sizeof(heading) + the_hash_algo->hexsz - 1] == '\n' &&
4061
/* does the abbreviated name on the index line agree with it? */
4062
starts_with(preimage + sizeof(heading) - 1, p->old_sha1_prefix))
4063
return 0; /* it all looks fine */