# [PATCH v2 0/3] Fix LLP64 `(size_t)1` compatibility VS C4334 warnings

6 messages from 2021-12-01 to 2021-12-02. Participants: Philip Oakley, Junio C Hamano, Derrick Stolee.
Thread: https://gitlist.dev/t/57006

## Philip Oakley, 2021-12-01 00:28

Subject: [PATCH v2 0/3] Fix LLP64 `(size_t)1` compatibility VS C4334 warnings
Message-ID: <20211201002902.1042-1-philipoakley@iee.email>
URL: https://gitlist.dev/e/20211201002902.1042-1-philipoakley%40iee.email

```
Since V1 (gitster/po/size-t-for-vs)
https://lore.kernel.org/git/20211126113614.709-1-philipoakley@iee.email/

Former patch 1/4 was dropped as it was already in Junio's tree.

Patch 1/3 corrects my spelling mistake.

Patch 2/3 has added extra spacing around the << operator as suggested by
Stollee[1].

Patch 3/3 removes the superceded commit message comment regarding
backporting the patch onto maint.

The Visual Studio MSVC compilation reports a number of C4334 "was 64-bit
shift intended" size mismatch warnings. In most of these cases a size_t
is ANDed (masked) with a bit shift of 1, or 1U. On LLP64 systems the unity
value is 32 bits, while size_t is 64 bits. 

The fix is to upcast the unity value to size_t.   

The first [dropped] patch had been reported [2] by René Scharfe as an extra patch
to the rs/mergesort series.

These fixes clear all the current C4334 warnings.

[1] https://lore.kernel.org/git/?q=%3Cf721bc99-6d79-e2f2-7810-dd77b777161f%40gmail.com%3E
[2] https://lore.kernel.org/git/7fbd4cf4-5f66-a4cd-0c41-e5b12d14d761@iee.email/

Philip Oakley (3):
  repack.c: LLP64 compatibility, upcast unity for left shift
  diffcore-delta.c: LLP64 compatibility, upcast unity for left shift
  object-file.c: LLP64 compatibility, upcast unity for left shift

 builtin/repack.c | 2 +-
 diffcore-delta.c | 6 +++---
 object-file.c    | 2 +-
 3 files changed, 5 insertions(+), 5 deletions(-)

Range-diff against v1:
1:  c00e082ed8 < -:  ---------- mergesort.c: LLP64 compatibility, upcast unity for left shift
2:  85506c7e77 ! 1:  13d9b3fd6d repack.c: LLP64 compatibility, upcast unity for left shift
    @@ Commit message
         repack.c: LLP64 compatibility, upcast unity for left shift
     
         Visual Studio reports C4334 "was 64-bit shift intended" warning
    -    because of size miss-match.
    +    because of size mismatch.
     
         Promote unity to the matching type to fit with the `&` operator.
     
3:  2072852f61 ! 2:  b6c7ad9177 diffcore-delta.c: LLP64 compatibility, upcast unity for left shift
    @@ diffcore-delta.c: static struct spanhash_top *hash_chars(struct repository *r,
      	i = INITIAL_HASH_SIZE;
      	hash = xmalloc(st_add(sizeof(*hash),
     -			      st_mult(sizeof(struct spanhash), 1<<i)));
    -+			      st_mult(sizeof(struct spanhash), (size_t)1<<i)));
    ++			      st_mult(sizeof(struct spanhash), (size_t)1 << i)));
      	hash->alloc_log2 = i;
      	hash->free = INITIAL_FREE(i);
     -	memset(hash->data, 0, sizeof(struct spanhash) * (1<<i));
4:  3b8f33fb28 ! 3:  0fa0d0a8c6 object-file.c: LLP64 compatibility, upcast unity for left shift
    @@ Commit message
     
         Signed-off-by: Philip Oakley <philipoakley@iee.email>
     
    -    ---
    -
    -    This cannot be applied to the maint-2.32 branch as the earlier René Scharfe
    -    patch had been, because the original sha1-file.c, to which the backport
    -    would apply, has been renamed in e5afd4449d (object-file.c: rename
    -    from sha1-file.c, 2020-12-31) which was merged in 8b327f1784
    -    (Merge branch 'ma/sha1-is-a-hash', 2021-01-15)
    -
      ## object-file.c ##
     @@ object-file.c: struct oidtree *odb_loose_cache(struct object_directory *odb,
      	struct strbuf buf = STRBUF_INIT;
-- 
2.34.0.rc1.windows.1.4.ga126985b17


```

## Philip Oakley, 2021-12-01 00:29

Subject: [PATCH v2 1/3] repack.c: LLP64 compatibility, upcast unity for left shift
Message-ID: <20211201002902.1042-2-philipoakley@iee.email>
URL: https://gitlist.dev/e/20211201002902.1042-2-philipoakley%40iee.email
In-Reply-To: <20211201002902.1042-1-philipoakley@iee.email>

```
Visual Studio reports C4334 "was 64-bit shift intended" warning
because of size mismatch.

Promote unity to the matching type to fit with the `&` operator.

Signed-off-by: Philip Oakley <philipoakley@iee.email>
---
 builtin/repack.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/builtin/repack.c b/builtin/repack.c
index 0b2d1e5d82..6da66474fd 100644
--- a/builtin/repack.c
+++ b/builtin/repack.c
@@ -842,7 +842,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
 			fname_old = mkpathdup("%s-%s%s",
 					packtmp, item->string, exts[ext].name);
 
-			if (((uintptr_t)item->util) & (1 << ext)) {
+			if (((uintptr_t)item->util) & ((uintptr_t)1 << ext)) {
 				struct stat statbuffer;
 				if (!stat(fname_old, &statbuffer)) {
 					statbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
-- 
2.34.0.rc1.windows.1.4.ga126985b17


```

## Philip Oakley, 2021-12-01 00:29

Subject: [PATCH v2 2/3] diffcore-delta.c: LLP64 compatibility, upcast unity for left shift
Message-ID: <20211201002902.1042-3-philipoakley@iee.email>
URL: https://gitlist.dev/e/20211201002902.1042-3-philipoakley%40iee.email
In-Reply-To: <20211201002902.1042-1-philipoakley@iee.email>

```
Visual Studio reports C4334 "was 64-bit shift intended" warning
because of size miss-match.

Promote unity to the matching type to fit with its subsequent operation.

Signed-off-by: Philip Oakley <philipoakley@iee.email>
---
 diffcore-delta.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/diffcore-delta.c b/diffcore-delta.c
index 5668ace60d..18d8f766d7 100644
--- a/diffcore-delta.c
+++ b/diffcore-delta.c
@@ -133,10 +133,10 @@ static struct spanhash_top *hash_chars(struct repository *r,
 
 	i = INITIAL_HASH_SIZE;
 	hash = xmalloc(st_add(sizeof(*hash),
-			      st_mult(sizeof(struct spanhash), 1<<i)));
+			      st_mult(sizeof(struct spanhash), (size_t)1 << i)));
 	hash->alloc_log2 = i;
 	hash->free = INITIAL_FREE(i);
-	memset(hash->data, 0, sizeof(struct spanhash) * (1<<i));
+	memset(hash->data, 0, sizeof(struct spanhash) * ((size_t)1 << i));
 
 	n = 0;
 	accum1 = accum2 = 0;
@@ -159,7 +159,7 @@ static struct spanhash_top *hash_chars(struct repository *r,
 		n = 0;
 		accum1 = accum2 = 0;
 	}
-	QSORT(hash->data, 1ul << hash->alloc_log2, spanhash_cmp);
+	QSORT(hash->data, (size_t)1ul << hash->alloc_log2, spanhash_cmp);
 	return hash;
 }
 
-- 
2.34.0.rc1.windows.1.4.ga126985b17


```

## Philip Oakley, 2021-12-01 00:29

Subject: [PATCH v2 3/3] object-file.c: LLP64 compatibility, upcast unity for left shift
Message-ID: <20211201002902.1042-4-philipoakley@iee.email>
URL: https://gitlist.dev/e/20211201002902.1042-4-philipoakley%40iee.email
In-Reply-To: <20211201002902.1042-1-philipoakley@iee.email>

```
Visual Studio reports C4334 "was 64-bit shift intended" warning because
of size miss-match.

Promote unity to the matching type to fit with the assignment.

Signed-off-by: Philip Oakley <philipoakley@iee.email>
---
 object-file.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/object-file.c b/object-file.c
index c3d866a287..da8821cb91 100644
--- a/object-file.c
+++ b/object-file.c
@@ -2425,7 +2425,7 @@ struct oidtree *odb_loose_cache(struct object_directory *odb,
 	struct strbuf buf = STRBUF_INIT;
 	size_t word_bits = bitsizeof(odb->loose_objects_subdir_seen[0]);
 	size_t word_index = subdir_nr / word_bits;
-	size_t mask = 1u << (subdir_nr % word_bits);
+	size_t mask = (size_t)1u << (subdir_nr % word_bits);
 	uint32_t *bitmap;
 
 	if (subdir_nr < 0 ||
-- 
2.34.0.rc1.windows.1.4.ga126985b17


```

## Junio C Hamano, 2021-12-01 22:49

Subject: Re: [PATCH v2 0/3] Fix LLP64 `(size_t)1` compatibility VS C4334 warnings
Message-ID: <xmqq5ys8ym8s.fsf@gitster.g>
URL: https://gitlist.dev/e/xmqq5ys8ym8s.fsf%40gitster.g
In-Reply-To: <20211201002902.1042-1-philipoakley@iee.email>

```
Philip Oakley <philipoakley@iee.email> writes:

> Since V1 (gitster/po/size-t-for-vs)
> https://lore.kernel.org/git/20211126113614.709-1-philipoakley@iee.email/
>
> Former patch 1/4 was dropped as it was already in Junio's tree.
>
> Patch 1/3 corrects my spelling mistake.
>
> Patch 2/3 has added extra spacing around the << operator as suggested by
> Stollee[1].
>
> Patch 3/3 removes the superceded commit message comment regarding
> backporting the patch onto maint.
>
> The Visual Studio MSVC compilation reports a number of C4334 "was 64-bit
> shift intended" size mismatch warnings. In most of these cases a size_t
> is ANDed (masked) with a bit shift of 1, or 1U. On LLP64 systems the unity
> value is 32 bits, while size_t is 64 bits. 
>
> The fix is to upcast the unity value to size_t.   
>
> The first [dropped] patch had been reported [2] by René Scharfe as an extra patch
> to the rs/mergesort series.
>
> These fixes clear all the current C4334 warnings.

Thanks.  Will queue; let's have it in 'next'.

```

## Derrick Stolee, 2021-12-02 20:56

Subject: Re: [PATCH v2 0/3] Fix LLP64 `(size_t)1` compatibility VS C4334 warnings
Message-ID: <cbcdcad7-16c5-b14d-5edc-5c91909b13e2@gmail.com>
URL: https://gitlist.dev/e/cbcdcad7-16c5-b14d-5edc-5c91909b13e2%40gmail.com
In-Reply-To: <xmqq5ys8ym8s.fsf@gitster.g>

```
On 12/1/2021 5:49 PM, Junio C Hamano wrote:
> Philip Oakley <philipoakley@iee.email> writes:
> 
>> Since V1 (gitster/po/size-t-for-vs)
>> https://lore.kernel.org/git/20211126113614.709-1-philipoakley@iee.email/
>>
>> Former patch 1/4 was dropped as it was already in Junio's tree.
>>
>> Patch 1/3 corrects my spelling mistake.
>>
>> Patch 2/3 has added extra spacing around the << operator as suggested by
>> Stollee[1].
>>
>> Patch 3/3 removes the superceded commit message comment regarding
>> backporting the patch onto maint.
>>
>> The Visual Studio MSVC compilation reports a number of C4334 "was 64-bit
>> shift intended" size mismatch warnings. In most of these cases a size_t
>> is ANDed (masked) with a bit shift of 1, or 1U. On LLP64 systems the unity
>> value is 32 bits, while size_t is 64 bits. 
>>
>> The fix is to upcast the unity value to size_t.   
>>
>> The first [dropped] patch had been reported [2] by René Scharfe as an extra patch
>> to the rs/mergesort series.
>>
>> These fixes clear all the current C4334 warnings.
> 
> Thanks.  Will queue; let's have it in 'next'.

I agree. This version is excellent.

-Stolee
 

```
