I'm trying to figure out if this could somehow be overflowed

hope your having a nice day; so i have the following code that i want to know if it would somehow overflow at some point ::

static void * myalloc(size_t len) {
    void * p = malloc(len);
    if (p == NULL) {
        fprintf(stderr, "end of memory\n");
        exit(1);
    }
    return p;
}

static void * copy_buf(void * buf, uint32_t count, uint32_t be) {
    uint32_t * p = NULL;
    if (buf != NULL) {
        p = myalloc(count * 4);
        memcpy(p, buf, count * 4);
     }  
// more code
  return p;
}

if it did not overflow, would something else cause it to do so, i mean if the variables changed. or the parameters changed (not the data type of course, but the value of them).

thank you for your help, the full source code is much bigger (more than a one file and more that 2000 lines of code for each one), but this is one of the parts that i am concerned about…

have a nice day.

1 Like

count * 4 might overflow if count was less than SIZE_MAX / 4 (SIZE_MAX is the maximum value a size_t can hold), since memcpy take a size_t as the third argument, but I’m not sure if that would cause any problem

Assuming malloc is from standard library, without that // more code, I don’t think this will overflow, whatever the value of count.

No overflow… not matter what is size of size_t or uint32_t .