[sheepdog] [PATCH] farm: fix comparison of empty sha1
Liu Yuan
namei.unix at gmail.com
Wed Aug 8 08:24:59 CEST 2012
On 08/08/2012 01:21 PM, MORITA Kazutaka wrote:
> At Wed, 08 Aug 2012 12:45:13 +0800,
> Liu Yuan wrote:
>>
>> On 08/07/2012 08:49 PM, MORITA Kazutaka wrote:
>>> I've not ever met any problems yet. I just noticed that the usage of
>>> strlen is wrong.
>>>
>>> I think the old code doesn't cause fatal problems like segfault, but
>>> causes a redundant write to the farm backend store when the sha1 value
>>> starts with zero.
>>
>> I guess you might be confused of '\0' against '0', I don't think there
>> is any problem to check sha1 *string* by strlen(). I think the patch
>> will cost extra cycles than old one also.
>
> entry->raw.sha1 is not a string but 20 bytes array of sha1 hash, isn't it?
>
> Here is a sample program to show the problem.
>
> ==
> #include <stdio.h>
> #include "sha1.h"
>
> #define SHA1_LEN 20
>
> int main(int argc, char *argv[])
> {
> unsigned char sha1[SHA1_LEN];
> struct sha1_ctx c;
> unsigned char *input = "127";
> int i;
>
> sha1_init(&c);
> sha1_update(&c, input, strlen(input));
> sha1_final(&c, sha1);
>
> printf("sha1 hash:\t");
> for (i = 0; i < SHA1_LEN; i++)
> printf("%02x", sha1[i]);
> printf("\n");
>
> printf("strlen(sha1):\t%d\n", strlen((char *)sha1));
>
> return 0;
> }
>
> /*
> * Result:
> *
> * sha1 hash: 008451a05e1e7aa32c75119df950d405265e0904
> * strlen(sha1): 0
> */
> ==
>
> Is this what you expect, or am I missing something?
>
> Thanks,
>
> Kazutaka
>
Oops, it was my misunderstanding of sha1 value, I thought it was string,
sorry.
Thanks,
Yuan
More information about the sheepdog
mailing list