Buffer overflow?

Static analysis tool (klocwork) report error on this:
memmove(
&list.id[ count1 + 1 ],
&list.id[ count1 ],
sizeof(list.id[0]) * (count2 - count1)
);

But below has no error:
memmove(
&list.id[ count2 ],
&list.id[ count2 + 1 ],
sizeof(list.id[0]) * (count1 - count2));

Error: The index value 1..495 may be used for array 'id' of size 400.
I am not sure the regarding the values suggested by the tool.
Is the first really has a chance of overflow.
I think the +1 on destination causes this error.
How should I fix it.

Is the list.id array large enough?

For the first code count1 needs to be smaller than count2 and the size of the array must be at least count2 + 1.

For the second code count1 needs to be greater than count2 and the size of the array must be at least count1 + 1.
Last edited on
That is right.
If those were not satisfied, it will cause lock.
But those codes work. It is just that the analysis tool reported an error.
Is there other way to modify this just to erradicate the error on analysis tool?

memmove(
&list.id[ count1 + 1 ],
&list.id[ count1 ],
sizeof(list.id[0]) * (count2 - count1)
);
We can't see the code that manipulates the values that count1 and count2 hold, so it is impossible to give any intelligent advice concerning them except to caution you as Peter87 has done.
I found it.
For the fix, you have to include that code inside a condition.
A condition to check that count1 + 1 is less than the maximum index of the array id.
Topic archived. No new replies allowed.