Operator assignment and overflow check

I have a question when you look at the KVM-QEMU source code.

ram_size = sz;
if (ram_size != sz) {
    fprintf(stderr, "qemu: ram size too large\n");
    exit(1);
}

sz uint64_t, and ram_size is ram_addr_t, which is also defined as uint64_t.

What are the above codes used for (check integer overflow)? How it works?

Thank.

+4
source share
1 answer

If you look closer to the definition ram_addr_t, you will see something like:

/* address in the RAM (different from a physical address) */
#if defined(CONFIG_XEN_BACKEND)
typedef uint64_t ram_addr_t;
#  define RAM_ADDR_MAX UINT64_MAX
#  define RAM_ADDR_FMT "%" PRIx64
#else
typedef uintptr_t ram_addr_t;
#  define RAM_ADDR_MAX UINTPTR_MAX
#  define RAM_ADDR_FMT "%" PRIxPTR
#endif

Please note that this may also be uintptr_t, which may not be 64-bit. In this case, there will be a problem with this appointment, if szmore than UINTPTR_MAX.

+3
source

All Articles