Jump to content

How to check for variable overflow?

Gat Pelsinger
Go to solution Solved by BobVonBob,

Most string to numeric converting functions have bounds checking, and the ones that don't shouldn't be used on values that could be outside of the acceptable range. In the case of C with an unsigned integer you would generally use strtoul(), which sets errno if the value is out of bounds. You can check that as follows:

 

#include <errno.h>
#include <stdlib.h>
 
int main(void) {
    const char *strNum = "-40";
    char *end = NULL;
    unsigned int num = strtoul(strNum, &end, 10); // overflows
    if (errno == ERANGE) {
        // You've just had a range error
        errno = 0;
    }
}

I most programming languages (C in my case) how do you check if a value being assigned to a variable doesn't overflow it and truncates its value? For example, if I have an unsigned integer called num, and I want to add a safety check that it is not less than 0, there is no way to do that is the value just wraps around and the conditional statement will never result in true (not). Maybe checking the number in string format?

Microsoft owns my soul.

 

Also, Dell is evil, but HP kinda nice.

Link to comment
Share on other sites

Link to post
Share on other sites

I don't think you can in C. Once the value has overflowed or underflowed its impossible to tell the current value is not correct.

 

The only way to prevent overflows is to check for them before running the operation or assignment.

Instead of this:

unsigned int a;

//some code here

a -= 1;

do this:

unsigned int a;

//some code here

if (a > 0) {
  a -= 1;
}

 

C will not prevent you from writing bad code. Some languages, like Rust, have built-in utilities to avoid this problem: https://doc.rust-lang.org/std/primitive.i32.html#method.checked_add but fundamentally the logic is the same; check that the operation would not cause an overflow before running it.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

Most string to numeric converting functions have bounds checking, and the ones that don't shouldn't be used on values that could be outside of the acceptable range. In the case of C with an unsigned integer you would generally use strtoul(), which sets errno if the value is out of bounds. You can check that as follows:

 

#include <errno.h>
#include <stdlib.h>
 
int main(void) {
    const char *strNum = "-40";
    char *end = NULL;
    unsigned int num = strtoul(strNum, &end, 10); // overflows
    if (errno == ERANGE) {
        // You've just had a range error
        errno = 0;
    }
}

¯\_(ツ)_/¯

 

 

Desktop:

Intel Core i7-11700K | Noctua NH-D15S chromax.black | ASUS ROG Strix Z590-E Gaming WiFi  | 32 GB G.SKILL TridentZ 3200 MHz | ASUS TUF Gaming RTX 3080 | 1TB Samsung 980 Pro M.2 PCIe 4.0 SSD | 2TB WD Blue M.2 SATA SSD | Seasonic Focus GX-850 Fractal Design Meshify C Windows 10 Pro

 

Laptop:

HP Omen 15 | AMD Ryzen 7 5800H | 16 GB 3200 MHz | Nvidia RTX 3060 | 1 TB WD Black PCIe 3.0 SSD | 512 GB Micron PCIe 3.0 SSD | Windows 11

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×