CHECK_INT32_ADD(3)

πŸ“¦ version 85.100.6, in nixpkgs unstable

πŸ’… CHECK_INT32_ADD(3) β€” the overflow-detection girlies have arrived πŸŽ€

okay bestie, so this is a Library Functions Manual page, and it's literally the whole check_<type>_<operation> squad: adding, subtracting, multiplying and dividing without letting your integers sneak past their limits and cause chaos. no cap, this one's a keeper. ✨


🌸 NAME 🌸

check_int32_add, check_uint32_add, check_int64_add, check_uint64_add, check_int32_sub, check_uint32_sub, check_int64_sub, check_uint64_sub, check_int32_mul, check_uint32_mul, check_int64_mul, check_uint64_mul, check_int32_div, check_uint32_div, check_int64_div, check_uint64_div β€” detect overflow in arithmetic 🫢

like, all sixteen of them. one big happy family. iconic behaviour honestly.


✨ SYNOPSIS ✨

#include <checkint.h>
int32_t
check_int32_add(int x, int y, int *err);

uint32_t
check_uint32_add(int x, int y, int *err);

int64_t
check_int64_add(int x, int y, int *err);

uint64_t
check_uint64_add(int x, int y, int *err);

int32_t
check_int32_sub(int x, int y, int *err);

uint32_t
check_uint32_sub(int x, int y, int *err);

int64_t
check_int64_sub(int x, int y, int *err);

uint64_t
check_uint64_sub(int x, int y, int *err);

int32_t
check_int32_mul(int x, int y, int *err);

uint32_t
check_uint32_mul(int x, int y, int *err);

int64_t
check_int64_mul(int x, int y, int *err);

uint64_t
check_uint64_mul(int x, int y, int *err);

int32_t
check_int32_div(int x, int y, int *err);

uint32_t
check_uint32_div(int x, int y, int *err);

int64_t
check_int64_div(int x, int y, int *err);

uint64_t
check_uint64_div(int x, int y, int *err);

sixteen whole functions. she really said "cover every combination" and honestly? we stan. πŸ‘‘


πŸ¦‹ DESCRIPTION πŸ¦‹

so, the check_<type>_<operation>(x, y, err) family does the little arithmetic moment you asked for β€” addition, subtraction, multiplication, or division β€” with the left operand being x and the right operand being y, and it hands you back the arithmetic result with the specified type. literally the math, but with a bodyguard. πŸ’ͺ

Either operand x or y (or both!) can be of any type that is compatible to signed or unsigned 8-bit, 16-bit, 32-bit, or 64-bit integers. mix and match, bestie, it's not gonna be weird about it.

The err argument is or'ed by flags in the function to indicate if an overflow has occurred. and the possible flag values are, in all their glory:

  • CHECKINT_NO_ERROR β€” no overflow has occurred. the vibe is immaculate. ✨
  • CHECKINT_OVERFLOW_ERROR β€” overflow has occurred. uh oh. 😭
  • CHECKINT_TYPE_ERROR β€” operand is of an incompatible type. not the type mismatch. πŸ’€
⚠️ ok important one, pay attention: the err argument is not cleared in calls to the check_<type>_<operation>(x, y, err) functions. Detected overflow persists in the err argument until err is reset to CHECKINT_NO_ERROR. so like, it remembers. you have to manually move on. no letting go for free.

πŸŽ€ RETURN VALUES πŸŽ€

If successful, the check_<type>_<operation>() functions will return the arithmetic result of performing the operation with left operand x and right operand y (even when overflow error occurs). yup, even then. it still gives you the number β€” it's just also screaming about it in err. multitasking queen. πŸ’…

If any other error occurs, the return value is -1 and the argument err will be set to indicate the error. not the -1. πŸ™ˆ


🌈 EXAMPLES 🌈

this little baddie right here, look at her go:

/* Create a variable to store overflow flag */
int32_t err = CHECKINT_NO_ERROR;
/* Use checkint API to perform an arithmetic operation and
 * store result in variable. */
int32_t arithmetic_result = check_int32_add(UINT_MAX, 1, &err);
/* Check status of overflow flag */
if (err & CHECKINT_OVERFLOW_ERROR) {
    /* Perform overflow resolution code */
    fprintf(stderr, "Overflow detected!\n");
}
/* Check for type error */
else if (err & CHECKINT_TYPE_ERROR) {
    /* Deal with incompatible types error */
    fprintf(stderr, "Incompatible types!\n");
}
/* Reset overflow flag for next operation */
err = CHECKINT_NO_ERROR;

see how she sets err, checks it, and then resets it? that last line is the main character move. don't skip it bestie. πŸ”₯


πŸ’œ ERRORS πŸ’œ

The check_<type>_<operation>() functions may fail if:

  • [CHECKINT_TYPE_ERROR] β€” operand is of an incompatible type. yeah. that. 🫠

⭐ HISTORY ⭐

The checkint() API was introduced in Mac OS X 10.5. vintage! she's been serving since day one. πŸ•°οΈ


and that's the page, bestie β€” go forth and never overflow silently again πŸ’–

this lil number comes to you from Debian, dated April 20, 2007, and it's the CHECK_INT32_ADD(3) page of the Library Functions Manual. mwah, love u, bye! πŸŒΈπŸŽ€πŸ¦‹

πŸ“¦ where this page comes from