# [PATCH 1/4] git-compat-util.h: xsize_t() - avoid -Wsign-compare warnings

4 messages from 2017-09-21 to 2017-09-21. Participants: Ramsay Jones, Torsten Bögershausen, Stefan Beller.
Thread: https://gitlist.dev/t/46800

## Ramsay Jones, 2017-09-21 16:46

Subject: [PATCH 1/4] git-compat-util.h: xsize_t() - avoid -Wsign-compare warnings
Message-ID: <15367a04-754c-f6b8-a89b-2c1ff65dedf5@ramsayjones.plus.com>
URL: https://gitlist.dev/e/15367a04-754c-f6b8-a89b-2c1ff65dedf5%40ramsayjones.plus.com

```

Signed-off-by: Ramsay Jones <ramsay@ramsayjones.plus.com>
---
 git-compat-util.h | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/git-compat-util.h b/git-compat-util.h
index 9bc15b036..cedad4d58 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -898,9 +898,11 @@ static inline char *xstrdup_or_null(const char *str)
 
 static inline size_t xsize_t(off_t len)
 {
-	if (len > (size_t) len)
+	size_t size = (size_t) len;
+
+	if (len != (off_t) size)
 		die("Cannot handle files this big");
-	return (size_t)len;
+	return size;
 }
 
 __attribute__((format (printf, 3, 4)))
-- 
2.14.0

```

## Torsten Bögershausen, 2017-09-21 18:49

Subject: Re: [PATCH 1/4] git-compat-util.h: xsize_t() - avoid -Wsign-compare warnings
Message-ID: <75416139-74c5-8a60-22e8-79c53dd062c1@web.de>
URL: https://gitlist.dev/e/75416139-74c5-8a60-22e8-79c53dd062c1%40web.de
In-Reply-To: <15367a04-754c-f6b8-a89b-2c1ff65dedf5@ramsayjones.plus.com>

```
On 2017-09-21 18:46, Ramsay Jones wrote:
> 
> Signed-off-by: Ramsay Jones <ramsay@ramsayjones.plus.com>
> ---
>  git-compat-util.h | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/git-compat-util.h b/git-compat-util.h
> index 9bc15b036..cedad4d58 100644
> --- a/git-compat-util.h
> +++ b/git-compat-util.h
> @@ -898,9 +898,11 @@ static inline char *xstrdup_or_null(const char *str)
>  
>  static inline size_t xsize_t(off_t len)
>  {
> -	if (len > (size_t) len)
> +	size_t size = (size_t) len;
> +
> +	if (len != (off_t) size)
>  		die("Cannot handle files this big");
> -	return (size_t)len;
> +	return size;

Hm, can someone help me out ?
Why is the cast not needed ?

>  }
>  
>  __attribute__((format (printf, 3, 4)))
> 


```

## Stefan Beller, 2017-09-21 18:57

Subject: Re: [PATCH 1/4] git-compat-util.h: xsize_t() - avoid -Wsign-compare warnings
Message-ID: <CAGZ79kYuGpO5Ji9vwqwy_2=ea2Vd9CG2ethzyhXUc709KQSSaQ@mail.gmail.com>
URL: https://gitlist.dev/e/CAGZ79kYuGpO5Ji9vwqwy_2%3Dea2Vd9CG2ethzyhXUc709KQSSaQ%40mail.gmail.com
In-Reply-To: <75416139-74c5-8a60-22e8-79c53dd062c1@web.de>

```
On Thu, Sep 21, 2017 at 11:49 AM, Torsten Bögershausen <tboegi@web.de> wrote:
> On 2017-09-21 18:46, Ramsay Jones wrote:
>>
>> Signed-off-by: Ramsay Jones <ramsay@ramsayjones.plus.com>
>> ---
>>  git-compat-util.h | 6 ++++--
>>  1 file changed, 4 insertions(+), 2 deletions(-)
>>
>> diff --git a/git-compat-util.h b/git-compat-util.h
>> index 9bc15b036..cedad4d58 100644
>> --- a/git-compat-util.h
>> +++ b/git-compat-util.h
>> @@ -898,9 +898,11 @@ static inline char *xstrdup_or_null(const char *str)
>>
>>  static inline size_t xsize_t(off_t len)
>>  {
>> -     if (len > (size_t) len)
>> +     size_t size = (size_t) len;
>> +
>> +     if (len != (off_t) size)
>>               die("Cannot handle files this big");
>> -     return (size_t)len;
>> +     return size;
>
> Hm, can someone help me out ?
> Why is the cast not needed ?

Because 'size' is already size_t,
(cast was done in first line of the function:
    size_t size = (size_t) len;
), previously we cast len multiple times,
now we cast it once and use size thereafter.

>
>>  }
>>
>>  __attribute__((format (printf, 3, 4)))
>>
>

```

## Ramsay Jones, 2017-09-21 19:02

Subject: Re: [PATCH 1/4] git-compat-util.h: xsize_t() - avoid -Wsign-compare warnings
Message-ID: <1b778d3b-56d5-4cda-1b75-b0e81e19874e@ramsayjones.plus.com>
URL: https://gitlist.dev/e/1b778d3b-56d5-4cda-1b75-b0e81e19874e%40ramsayjones.plus.com
In-Reply-To: <75416139-74c5-8a60-22e8-79c53dd062c1@web.de>

```


On 21/09/17 19:49, Torsten Bögershausen wrote:
> On 2017-09-21 18:46, Ramsay Jones wrote:
>>
>> Signed-off-by: Ramsay Jones <ramsay@ramsayjones.plus.com>
>> ---
>>  git-compat-util.h | 6 ++++--
>>  1 file changed, 4 insertions(+), 2 deletions(-)
>>
>> diff --git a/git-compat-util.h b/git-compat-util.h
>> index 9bc15b036..cedad4d58 100644
>> --- a/git-compat-util.h
>> +++ b/git-compat-util.h
>> @@ -898,9 +898,11 @@ static inline char *xstrdup_or_null(const char *str)
>>  
>>  static inline size_t xsize_t(off_t len)
>>  {
>> -	if (len > (size_t) len)
>> +	size_t size = (size_t) len;
>> +
>> +	if (len != (off_t) size)
>>  		die("Cannot handle files this big");
>> -	return (size_t)len;
>> +	return size;
> 
> Hm, can someone help me out ?
> Why is the cast not needed ?

The cast in the return statement? If so, because the 'size' variable
has been declared with the size_t type. (The truncation, if any, would
happen when 'size' is initialised in the declaration).

Hope that helps.

ATB,
Ramsay Jones



```
