CHECK_INT32_ADD(3)
π 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: theerrargument is not cleared in calls to thecheck_<type>_<operation>(x, y, err)functions. Detected overflow persists in theerrargument untilerris reset toCHECKINT_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
- nixpkgs unstable: removefile 85.100.6
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms