threads / patch / 46800

patch, 4 partsgit-compat-util.h: xsize_t() - avoid -Wsign-compare warnings

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

## tl;dr

4 messages between Sep 21, 2017 and Sep 21, 2017. Diffs are folded; open one to read it.

replies: 3people: 3as markdown or json

Ramsay Jones· Sep 21, 2017, 16:46 UTC · lore
Signed-off-by: Ramsay Jones <ramsay@ramsayjones.plus.com>
---
 git-compat-util.h | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
Show changes to git-compat-util.h +4 −2
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· Sep 21, 2017, 18:49 UTC · re: Ramsay Jones · lore

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

On 2017-09-21 18:46, Ramsay Jones wrote:
Show 21 quoted lines
> 
> 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· Sep 21, 2017, 18:57 UTC · re: Torsten Bögershausen · lore

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

On Thu, Sep 21, 2017 at 11:49 AM, Torsten Bögershausen <tboegi@web.de> wrote:
Show 25 quoted lines
> 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.
Show 6 quoted lines
>
>>  }
>>
>>  __attribute__((format (printf, 3, 4)))
>>
>
Ramsay Jones· Sep 21, 2017, 19:02 UTC · re: Torsten Bögershausen · lore

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

On 21/09/17 19:49, Torsten Bögershausen wrote:
Show 25 quoted lines
> 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

← back to recent threads