Repository: valkey-io/valkey
File: src/bitops.c (C)

Code before:
            } else if (owtype == BFOVERFLOW_SAT) {
                *limit = 0;
            }
        }
        return -1;
    }
    return 0;

handle_wrap: {
    uint64_t mask = ((uint64_t)-1) << bits;
    uint64_t res = value + incr;

    res &= ~mask;
    *limit = res;
}
    return 1;
}

int checkSignedBitfieldOverflow(int64_t value, int64_t incr, uint64_t bits, int owtype, int64_t *limit) {
    int64_t max = (bits == 64) ? INT64_MAX : (((int64_t)1 << (bits - 1)) - 1);
    int64_t min = (-max) - 1;

    /* Note that maxincr and minincr could overflow, but we use the values
     * only after checking 'value' range, so when we use it no overflow
     * happens. 'uint64_t' cast is there just to prevent undefined behavior on
     * overflow */
    int64_t maxincr = (uint64_t)max - value;
    int64_t minincr = min - value;

    if (value > max || (bits != 64 && incr > maxincr) || (value >= 0 && incr > 0 && incr > maxincr)) {
        if (limit) {
            if (owtype == BFOVERFLOW_WRAP) {
                goto handle_wrap;
            } else if (owtype == BFOVERFLOW_SAT) {
                *limit = max;
            }
        }
        return 1;
    } else if (value < min || (bits != 64 && incr < minincr) || (value < 0 && incr < 0 && incr < minincr)) {
        if (limit) {
            if (owtype == BFOVERFLOW_WRAP) {
                goto handle_wrap;
            } else if (owtype == BFOVERFLOW_SAT) {
                *limit = min;
            }
        }
        return -1;
    }
    return 0;

handle_wrap: {

Review comment (suggestion):
The cast to signed can be implicit, for brevity?

```suggestion
    int64_t maxincr = (uint64_t)max - (uint64_t)value;
    int64_t minincr = (uint64_t)min - (uint64_t)value;
```

Code after:
            } else if (owtype == BFOVERFLOW_SAT) {
                *limit = 0;
            }
        }
        return -1;
    }
    return 0;

handle_wrap: {
    uint64_t mask = ((uint64_t)-1) << bits;
    uint64_t res = value + incr;

    res &= ~mask;
    *limit = res;
}
    return 1;
}

int checkSignedBitfieldOverflow(int64_t value, int64_t incr, uint64_t bits, int owtype, int64_t *limit) {
    int64_t max = (bits == 64) ? INT64_MAX : (((int64_t)1 << (bits - 1)) - 1);
    int64_t min = (-max) - 1;

    /* max/min and value are signed integers but to avoid undefined behavior
     * we temporarily cast them to unsigned integers before subtracting. */
    int64_t maxincr = (int64_t)((uint64_t)max - (uint64_t)value);
    int64_t minincr = (int64_t)((uint64_t)min - (uint64_t)value);

    if (value > max || (bits != 64 && incr > maxincr) || (value >= 0 && incr > 0 && incr > maxincr)) {
        if (limit) {
            if (owtype == BFOVERFLOW_WRAP) {
                goto handle_wrap;
            } else if (owtype == BFOVERFLOW_SAT) {
                *limit = max;
            }
        }
        return 1;
    } else if (value < min || (bits != 64 && incr < minincr) || (value < 0 && incr < 0 && incr < minincr)) {
        if (limit) {
            if (owtype == BFOVERFLOW_WRAP) {
                goto handle_wrap;
            } else if (owtype == BFOVERFLOW_SAT) {
                *limit = min;
            }
        }
        return -1;
    }
    return 0;

handle_wrap: {
    uint64_t msb = (uint64_t)1 << (bits - 1);
    uint64_t a = value, b = incr, c;