Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C URGENT - Array bound error

Akash

Coder
Hi ,

I'm working on a code, and I'm using klocwork for static analysis, which is suspicious of array bound error.

ptr->array_temps[ptr->numberInPool++] = primary->array_temp : loop numberInPool is increased regardless of the loop , so if this should be protected, if yes, how?

C:
void Handler(Data_t *instance)
{
    structanother *primary;
    Pool_t *ptr;
    Pool_t *prev;

    primary = (structanother *) instance->recvMsgP;

#ifndef X
    if (instance->myAparray_temp == value1)
#endif
    {
        ptr = instance->instance_internal;
        prev = NULL;

        while((ptr) && (ptr->numberInPool == POOL_SIZE)) // The value of poolsize is 10
        {
            prev = ptr;
            ptr = ptr->next;
        }

        if (ptr)
        {
            /* Do nothing */
        }
        else
        {
            ptr = memalloc(sizeof(Pool_t));

            if (prev)
            {
                prev->next = ptr;
            }
            else
            {
                instance->instance_internal = ptr;
            }
        }

        ptr->array_temps[ptr->numberInPool++] = primary->array_temp; // Error line - Is there possiblity of array bound error?
        instance->numberOfinstance_internal++;
    }
 
Last edited by a moderator:
I was going to write "Please post your code in proper code tags" but I see a moderator has already kindly done that for you. Next time, please first consult this page https://codeforum.org/threads/how-to-post-your-code-into-threads.183/ for how to post code.

This line

ptr->array_temps[ptr->numberInPool++] = primary->array_temp;

of course runs the risk of buffer overrun (or array bound error, as you call it). I have no clue what your code does, and I can't see from this snippet how array_temps is defined, but you need to make sure that its index ptr->numberInPool will not exceed the # of elements in this array minus 1.
 
I was going to write "Please post your code in proper code tags" but I see a moderator has already kindly done that for you. Next time, please first consult this page https://codeforum.org/threads/how-to-post-your-code-into-threads.183/ for how to post code.

This line

ptr->array_temps[ptr->numberInPool++] = primary->array_temp;

of course runs the risk of buffer overrun (or array bound error, as you call it). I have no clue what your code does, and I can't see from this snippet how array_temps is defined, but you need to make sure that its index ptr->numberInPool will not exceed the # of elements in this array minus 1.
Thanks for your help! did it!
 

Latest posts

Buy us a coffee!

Back
Top Bottom