mirror of
https://github.com/git/git.git
synced 2025-02-06 09:26:16 +00:00
62e745ced2
The alloc and nr fields of a prio-queue tell us how much memory is allocated and used in the array. So the natural type for them is size_t, which prevents overflow on 64-bit systems where "int" is still 32 bits. This is unlikely to happen in practice, as we typically use it for storing commits, and having 2^31 of those is rather a lot. But it's good to keep our generic data structures as flexible as possible. And as we start to enforce -Wsign-compare, it means that callers need to use "int", too, and the problem proliferates. Let's fix it at the source. The changes here can be put into a few groups: 1. Changing the alloc/nr fields in the struct to size_t. This requires swapping out int for size_t in negotiator/skipping.c, as well as in prio_queue_get(), because those all iterate over the array. Building with -Wsign-compare complains about these. 2. Other code that assigns or passes around indexes into the array (e.g., the swap() and compare() functions) won't trigger -Wsign-compare because we are simply truncating the values. These are caught by -Wconversion, but I've adjusted them here to future-proof us. 3. In prio_queue_reverse() we compute "queue->nr - 1" without checking if anything is in the queue, which underflows now that nr is unsigned. We can fix that by returning early when the queue is empty (there is nothing to reverse). 4. The insertion_ctr variable is currently unsigned, but can likewise grow (it is actually worse, because adding and removing an element many times will keep increasing the counter, even though "nr" does not). I've bumped that to size_t here, as well. But -Wconversion notes that computing the "cmp" result by subtracting the counters and assigning to "int" is a potential problem. And that's true even before this patch, since we use an unsigned counter (imagine comparing "2^32-1" and "0", which should be a high positive value, but instead is "-1" as a signed int). Since we only care about the sign (and not the magnitude) of the result, we could fix this by swapping out the subtraction for a ternary comparison. Probably the performance impact would be negligible, since we just called into a custom compare function and branched on its result anyway. But it's easy enough to do a branchless version by subtracting the comparison results. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
100 lines
2.1 KiB
C
100 lines
2.1 KiB
C
#include "git-compat-util.h"
|
|
#include "prio-queue.h"
|
|
|
|
static inline int compare(struct prio_queue *queue, size_t i, size_t j)
|
|
{
|
|
int cmp = queue->compare(queue->array[i].data, queue->array[j].data,
|
|
queue->cb_data);
|
|
if (!cmp)
|
|
cmp = (queue->array[i].ctr > queue->array[j].ctr) -
|
|
(queue->array[i].ctr < queue->array[j].ctr);
|
|
return cmp;
|
|
}
|
|
|
|
static inline void swap(struct prio_queue *queue, size_t i, size_t j)
|
|
{
|
|
SWAP(queue->array[i], queue->array[j]);
|
|
}
|
|
|
|
void prio_queue_reverse(struct prio_queue *queue)
|
|
{
|
|
size_t i, j;
|
|
|
|
if (queue->compare)
|
|
BUG("prio_queue_reverse() on non-LIFO queue");
|
|
if (!queue->nr)
|
|
return;
|
|
for (i = 0; i < (j = (queue->nr - 1) - i); i++)
|
|
swap(queue, i, j);
|
|
}
|
|
|
|
void clear_prio_queue(struct prio_queue *queue)
|
|
{
|
|
FREE_AND_NULL(queue->array);
|
|
queue->nr = 0;
|
|
queue->alloc = 0;
|
|
queue->insertion_ctr = 0;
|
|
}
|
|
|
|
void prio_queue_put(struct prio_queue *queue, void *thing)
|
|
{
|
|
size_t ix, parent;
|
|
|
|
/* Append at the end */
|
|
ALLOC_GROW(queue->array, queue->nr + 1, queue->alloc);
|
|
queue->array[queue->nr].ctr = queue->insertion_ctr++;
|
|
queue->array[queue->nr].data = thing;
|
|
queue->nr++;
|
|
if (!queue->compare)
|
|
return; /* LIFO */
|
|
|
|
/* Bubble up the new one */
|
|
for (ix = queue->nr - 1; ix; ix = parent) {
|
|
parent = (ix - 1) / 2;
|
|
if (compare(queue, parent, ix) <= 0)
|
|
break;
|
|
|
|
swap(queue, parent, ix);
|
|
}
|
|
}
|
|
|
|
void *prio_queue_get(struct prio_queue *queue)
|
|
{
|
|
void *result;
|
|
size_t ix, child;
|
|
|
|
if (!queue->nr)
|
|
return NULL;
|
|
if (!queue->compare)
|
|
return queue->array[--queue->nr].data; /* LIFO */
|
|
|
|
result = queue->array[0].data;
|
|
if (!--queue->nr)
|
|
return result;
|
|
|
|
queue->array[0] = queue->array[queue->nr];
|
|
|
|
/* Push down the one at the root */
|
|
for (ix = 0; ix * 2 + 1 < queue->nr; ix = child) {
|
|
child = ix * 2 + 1; /* left */
|
|
if (child + 1 < queue->nr &&
|
|
compare(queue, child, child + 1) >= 0)
|
|
child++; /* use right child */
|
|
|
|
if (compare(queue, ix, child) <= 0)
|
|
break;
|
|
|
|
swap(queue, child, ix);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
void *prio_queue_peek(struct prio_queue *queue)
|
|
{
|
|
if (!queue->nr)
|
|
return NULL;
|
|
if (!queue->compare)
|
|
return queue->array[queue->nr - 1].data;
|
|
return queue->array[0].data;
|
|
}
|