-
Notifications
You must be signed in to change notification settings - Fork 15.5k
Open
Labels
Description
I found an issue where TaintedAlloc flags this code
#include <limits.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
int groups = getgroups(0, NULL);
if (groups < 0) {
return -1;
}
if (groups > NGROUPS_MAX) {
return -1;
}
malloc(groups * sizeof(gid_t));
return 0;
}However, the following code resolves the warning
#include <limits.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
int groups = getgroups(0, NULL);
if (groups < 0) {
return -1;
}
if (groups * sizeof(gid_t) > NGROUPS_MAX * sizeof(gid_t)) {
return -1;
}
malloc(groups * sizeof(gid_t));
return 0;
}In the second example I essentially have x * c > y * c, which is equivalent to x > y. I'm just implicitly bounds checking groups * sizeof(gid_t) by checking groups, knowing that sizeof(gid_t) is a constant.