Skip to content

Commit c097b95

Browse files
dschogitster
authored andcommitted
msvc: avoid using minus operator on unsigned types
MSVC complains about this with `-Wall`, which can be taken as a sign that this is indeed a real bug. The symptom is: C4146: unary minus operator applied to unsigned type, result still unsigned Let's avoid this warning in the minimal way, e.g. writing `-1 - <unsigned value>` instead of `-<unsigned value> - 1`. Note that the change in the `estimate_cache_size()` function is needed because MSVC considers the "return type" of the `sizeof()` operator to be `size_t`, i.e. unsigned, and therefore it cannot be negated using the unary minus operator. Even worse, that arithmetic is doing extra work, in vain. We want to calculate the entry extra cache size as the difference between the size of the `cache_entry` structure minus the size of the `ondisk_cache_entry` structure, padded to the appropriate alignment boundary. To that end, we start by assigning that difference to the `per_entry` variable, and then abuse the `len` parameter of the `align_padding_size()` macro to take the negative size of the ondisk entry size. Essentially, we try to avoid passing the already calculated difference to that macro by passing the operands of that difference instead, when the macro expects operands of an addition: #define align_padding_size(size, len) \ ((size + (len) + 8) & ~7) - (size + len) Currently, we pass A and -B to that macro instead of passing A - B and 0, where A - B is already stored in the `per_entry` variable, ready to be used. This is neither necessary, nor intuitive. Let's fix this, and have code that is both easier to read and that also does not trigger MSVC's warning. While at it, we take care of reporting overflows (which are unlikely, but hey, defensive programming is good!). We _also_ take pains of casting the unsigned value to signed: otherwise, the signed operand (i.e. the `-1`) would be cast to unsigned before doing the arithmetic. Helped-by: Denton Liu <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent dbcd970 commit c097b95

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

cache.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,19 @@ struct cache_entry *index_file_exists(struct index_state *istate, const char *na
725725
*/
726726
int index_name_pos(const struct index_state *, const char *name, int namelen);
727727

728+
/*
729+
* Some functions return the negative complement of an insert position when a
730+
* precise match was not found but a position was found where the entry would
731+
* need to be inserted. This helper protects that logic from any integer
732+
* underflow.
733+
*/
734+
static inline int index_pos_to_insert_pos(uintmax_t pos)
735+
{
736+
if (pos > INT_MAX)
737+
die("overflow: -1 - %"PRIuMAX, pos);
738+
return -1 - (int)pos;
739+
}
740+
728741
#define ADD_CACHE_OK_TO_ADD 1 /* Ok to add */
729742
#define ADD_CACHE_OK_TO_REPLACE 2 /* Ok to replace file/directory */
730743
#define ADD_CACHE_SKIP_DFCHECK 4 /* Ok to skip DF conflict checks */

read-cache.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,7 +1276,7 @@ static int add_index_entry_with_check(struct index_state *istate, struct cache_e
12761276
*/
12771277
if (istate->cache_nr > 0 &&
12781278
strcmp(ce->name, istate->cache[istate->cache_nr - 1]->name) > 0)
1279-
pos = -istate->cache_nr - 1;
1279+
pos = index_pos_to_insert_pos(istate->cache_nr);
12801280
else
12811281
pos = index_name_stage_pos(istate, ce->name, ce_namelen(ce), ce_stage(ce));
12821282

@@ -1894,7 +1894,7 @@ static size_t estimate_cache_size(size_t ondisk_size, unsigned int entries)
18941894
/*
18951895
* Account for potential alignment differences.
18961896
*/
1897-
per_entry += align_padding_size(sizeof(struct cache_entry), -sizeof(struct ondisk_cache_entry));
1897+
per_entry += align_padding_size(per_entry, 0);
18981898
return ondisk_size + entries * per_entry;
18991899
}
19001900

sha1-lookup.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ int sha1_pos(const unsigned char *sha1, void *table, size_t nr,
7070
if (miv < lov)
7171
return -1;
7272
if (hiv < miv)
73-
return -1 - nr;
73+
return index_pos_to_insert_pos(nr);
7474
if (lov != hiv) {
7575
/*
7676
* At this point miv could be equal
@@ -97,7 +97,7 @@ int sha1_pos(const unsigned char *sha1, void *table, size_t nr,
9797
lo = mi + 1;
9898
mi = lo + (hi - lo) / 2;
9999
} while (lo < hi);
100-
return -lo-1;
100+
return index_pos_to_insert_pos(lo);
101101
}
102102

103103
int bsearch_hash(const unsigned char *sha1, const uint32_t *fanout_nbo,

0 commit comments

Comments
 (0)